visionpro脚本

visionproToolBlock的脚本的优先级优于工具连线的优先级,一般是照着脚本的执行顺序进行执行对应的工具,最常用的是C#的高级脚本,C#的脚本如下分为5部分。

第一部分:主要是一些库的引用,对于有些类型不知道库的时候,可以通过查询帮助文档输入对应的名称,查询到具体信息,其中就包含他所在的库;

第二部分:主要是一些全局变量的声明,包括标签工具、一些数据类型的声明等等,这里还声明了一个ToolBlock工具,用于该Block的工具的定义、声明、获取Record等用途;

第三部分:主要对基类中的函数进行重写;

第四部分:主要对当前的Record进行操作;

第五部分:主要对整个ToolBlock中的工具运行完成之后的最终结果进行操作,主要包括了标签操作、循环运行时中间结果的保留并附着在最终结果上等。

本片文章主要是进行两个ToolBlock的实现,包含计算两个点之间的距离,寻找某个特征点的坐标。

ToolBlock1

ToolBlock1主要是进行三角形的一个顶点到正方形顶点的距离测量,检测效果如下:

对于该任务,首先需要对三角形进行特征匹配,特征匹配完成之后,根据特征匹配建立一个新的坐标系,该坐标系+之前所建立的坐标系可以完成该图旋转,放大和缩小后的对应特征检测,再根据在这两个坐标系下的定位,找到对应矩形的顶点,完成距离的检测,对应的工作流程如图所示:

该过程基本没有连线操作,主要是通过脚本实现对应的功能,脚本代码如下:

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  CogGraphicLabel mylabel = new CogGraphicLabel();
  #endregion

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif


    // Run each tool using the RunTool function
    //foreach(ICogTool tool in mToolBlock.Tools)
    try
    {
      CogPMAlignTool cpmatool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
      cpmatool.InputImage = (CogImage8Grey) mToolBlock.Inputs["OutputImage"].Value;
      cpmatool.Run();
      if(cpmatool.Results.Count != 1)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cpmatool fail";
        return false;
      }
    
    
      CogFixtureTool cfixtool2 = mToolBlock.Tools["CogFixtureTool2"] as CogFixtureTool;
      cfixtool2.RunParams.UnfixturedFromFixturedTransform = cpmatool.Results[0].GetPose();
      cfixtool2.InputImage = cpmatool.InputImage;
      cfixtool2.Run();
      if(cfixtool2.InputImage == null)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cfixtool2 fail";
        return false;
      }
      //找点1
      CogFindLineTool cfindltool1 = mToolBlock.Tools["CogFindLineTool1"] as CogFindLineTool;
      cfindltool1.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cfindltool1.Run();
      if(cfindltool1.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cfindltool1 fail";
        return false;
      }
    
      CogFindLineTool cfindltool2 = mToolBlock.Tools["CogFindLineTool2"] as CogFindLineTool;
      cfindltool2.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cfindltool2.Run();
      if(cfindltool2.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cfindltool2 fail";
        return false;
      }
    
      CogIntersectLineLineTool cinterlinetool1 = mToolBlock.Tools["CogIntersectLineLineTool1"] as CogIntersectLineLineTool;
      cinterlinetool1.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cinterlinetool1.LineA = cfindltool1.Results.GetLine();
      cinterlinetool1.LineB = cfindltool2.Results.GetLine();
      cinterlinetool1.Run();
      if(cinterlinetool1.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cinterlinetool1 fail";
        return false;
      }

    
      //寻找点2
      CogFindLineTool cfindltool3 = mToolBlock.Tools["CogFindLineTool3"] as CogFindLineTool;
      cfindltool3.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cfindltool3.Run();
      if(cfindltool3.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cfindltool3 fail";
        return false;
      }
    
      CogFindLineTool cfindltool4 = mToolBlock.Tools["CogFindLineTool4"] as CogFindLineTool;
      cfindltool4.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cfindltool4.Run();
      if(cfindltool4.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cfindltool4 fail";
        return false;
      }
    
      CogIntersectLineLineTool cinterlinetool2 = mToolBlock.Tools["CogIntersectLineLineTool2"] as CogIntersectLineLineTool;
      cinterlinetool2.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cinterlinetool2.LineA = cfindltool3.Results.GetLine();
      cinterlinetool2.LineB = cfindltool4.Results.GetLine();
      cinterlinetool2.Run();
      if(cinterlinetool2.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cinterlinetool2 fail";
        return false;
      }
    
      //计算点与点之间的距离
      CogDistancePointPointTool cdistpptool = mToolBlock.Tools["CogDistancePointPointTool1"] as CogDistancePointPointTool;
      cdistpptool.InputImage = (CogImage8Grey) cpmatool.InputImage;
      cdistpptool.StartX = cinterlinetool1.X;
      cdistpptool.StartY = cinterlinetool1.Y;
      cdistpptool.EndX = cinterlinetool2.X;
      cdistpptool.EndY = cinterlinetool2.Y;
      cdistpptool.Run();
      if(cdistpptool.RunStatus.Result != CogToolResultConstants.Accept)
      {
        mToolBlock.Outputs["strerr"].Value = "block1 cdistpptool fail";
        return false;
      }
      mToolBlock.Outputs["strerr"].Value = "success";
      mToolBlock.Outputs["distance"].Value = cdistpptool.Distance;
      
      mylabel.SetXYText(50, 20, "宽度" + cdistpptool.Distance.ToString("F2"));
      mylabel.Color = CogColorConstants.Cyan;
      mylabel.Font = new Font("楷体", 15);
    }
    catch(Exception e)
    {
      mToolBlock.Outputs["strerr"].Value = "fail";
    
    }
    return false;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// <summary>
  /// Called when the last run record may have changed and is being reconstructed
  /// </summary>
  /// <param name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    mToolBlock.AddGraphicToRunRecord(mylabel,lastRecord,"CogPMAlignTool1.InputImage"," ");
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}

 对应工具的声明和他所有的成员均可以通过在外面的工具,添加终端中寻找,他所有的成员方法均在此地方能找到。

 ToolBlock2

ToolBlock2实现的主要功能是对三个字符‘R’的左下角进行定位,包含了特征匹配,找线,找交点等操作。结果如下图所示:

对于该任务首先需要特征匹配,匹配完成之后,找线,找交点,对于工具连线操作而言,是无法进行循环操作的,所以此时就必须使用脚本进行操作,才能将三个点均找出。工具和代码如下所示:

脚本代码如下所示:

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
using System.Collections.Generic;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  CogGraphicLabel mylabel = new CogGraphicLabel();
  private List<ICogRecord> records = new List<ICogRecord>();
  #endregion

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif
    mToolBlock.Outputs["strerr"].Value = "";
    string str = "";
    try
    {
      CogPMAlignTool cpmatool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
      cpmatool.InputImage = (ICogImage) mToolBlock.Inputs["InputImage"].Value;
      cpmatool.Run();
      if(cpmatool.Results.Count != 3)
      {
        mToolBlock.Outputs["strerr"].Value = "block2 cpmatool fail";
        return false;
      }
      records.Clear();
      for(int i = 0;i < cpmatool.Results.Count;i++)
      {
        CogFixtureTool cfixtool2 = mToolBlock.Tools["CogFixtureTool2"] as CogFixtureTool;
        cfixtool2.RunParams.UnfixturedFromFixturedTransform = cpmatool.Results[i].GetPose();
        cfixtool2.InputImage = cpmatool.InputImage;
        cfixtool2.Run();
        if(cfixtool2.InputImage == null)
        {
          mToolBlock.Outputs["strerr"].Value = "block2 cfixtool2 fail";
          return false;
        }
    
        CogFindLineTool cfindltool1 = mToolBlock.Tools["CogFindLineTool1"] as CogFindLineTool;
        cfindltool1.InputImage = (CogImage8Grey) cpmatool.InputImage;
        cfindltool1.Run();
        if(cfindltool1.RunStatus.Result != CogToolResultConstants.Accept)
        {
          mToolBlock.Outputs["strerr"].Value = "block2 cfindltool1 fail";
          return false;
        }
    
        CogFindLineTool cfindltool2 = mToolBlock.Tools["CogFindLineTool2"] as CogFindLineTool;
        cfindltool2.InputImage = (CogImage8Grey) cpmatool.InputImage;
        cfindltool2.Run();
        if(cfindltool2.RunStatus.Result != CogToolResultConstants.Accept)
        {
          mToolBlock.Outputs["strerr"].Value = "block2 cfindltool2 fail";
          return false;
        }
    
        CogIntersectLineLineTool cinterlinetool1 = mToolBlock.Tools["CogIntersectLineLineTool1"] as CogIntersectLineLineTool;
        cinterlinetool1.InputImage = cpmatool.InputImage;
        cinterlinetool1.LineA = cfindltool1.Results.GetLine();
        cinterlinetool1.LineB = cfindltool2.Results.GetLine();
        cinterlinetool1.Run();
        if(cinterlinetool1.Intersects == false)
        {
          mToolBlock.Outputs["strerr"].Value = "block2 cinterlinetool1 fail";
          return false;
        }
        PointF p1 = (new PointF(float.Parse(cinterlinetool1.X.ToString("F2")), float.Parse(cinterlinetool1.Y.ToString("F2"))));
        str += p1.ToString();
        mToolBlock.Outputs["strerr"].Value = "success";
        records.Add(cinterlinetool1.CreateLastRunRecord());
      }
      mToolBlock.Outputs["respoint"].Value = str;
      mylabel.SetXYText(50, 5, "坐标" + str);
      mylabel.Color = CogColorConstants.Black;
      mylabel.Font = new Font("楷体", 10);
    }
    catch(Exception e)
    {
      str = "Exception: {e.Message}";
      mToolBlock.Outputs["strerr"].Value = str;
    }
    return false;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// <summary>
  /// Called when the last run record may have changed and is being reconstructed
  /// </summary>
  /// <param name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {     
    
    
    lastRecord.SubRecords[0].SubRecords.Clear();
    for(int i = 0;i < 3;i++)
    {
      lastRecord.SubRecords[0].SubRecords.Add(records[i]);
    }
    
    mToolBlock.AddGraphicToRunRecord(mylabel, lastRecord, "CogPMAlignTool1.InputImage", " ");
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}











 循环主要是针对特征匹配到的R的数量进行循环次数操作的,这里需要注意,因为visionpro在进行操作的时候,会将之前找到的交点的痕迹覆盖掉,因此需要将中间过程的Record记录下来,因此在一开始需要声明 private List<ICogRecord> records = new List<ICogRecord>()对中间过程的Record记录,添加到列表的操作:records.Add(cinterlinetool1.CreateLastRunRecord()),然后需要在最后lastrunrecord上记录:

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {     
    
    
    lastRecord.SubRecords[0].SubRecords.Clear();
    for(int i = 0;i < 3;i++)
    {
      lastRecord.SubRecords[0].SubRecords.Add(records[i]);
    }
    
    mToolBlock.AddGraphicToRunRecord(mylabel, lastRecord, "CogPMAlignTool1.InputImage", " ");
  }

注意这里的SubRecoRecordrds[i]的编号表示需要在第几个工具结果上添加记录下来的Record,还应该注意标签放置操作应该在这个操作之后,在这个操作之前会被clear掉。

OuterBlock

有时候可能一个工程文件中有多个Block模块,此时需要在外层套一个Block,将所有子Block包含,并且可能不同的Block对应不同的照片进行执行,为了便于在最外层查看lastrunrecord,一般将所有子Block的第一个CogFixtureTool挪动到OuterBlock中,再将其OutputImage传入到各个子Block中,如下所示:

 为了更好的控制某个Block的执行,一般需要在外面添加参数并且通过脚本进行控制实现:

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.CalibFix;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif


    // Run each tool using the RunTool function
    //foreach(ICogTool tool in mToolBlock.Tools)
    //mToolBlock.RunTool(tool, ref message, ref result);
    CogFixtureTool cfixtool = mToolBlock.Tools["CogFixtureTool1"] as CogFixtureTool;
    cfixtool.InputImage = (CogImage8Grey) mToolBlock.Inputs["OutputImage"].Value;
    cfixtool.Run();
    if(cfixtool.InputImage == null)
    {
      mToolBlock.Outputs["outtoolblock"].Value = "outtoolblock cfixtool fail ";
      return false;
    }
    
    CogToolBlock block1 = mToolBlock.Tools["CogToolBlock1"] as CogToolBlock;
    CogToolBlock block2 = mToolBlock.Tools["CogToolBlock2"] as CogToolBlock;
    if(cfixtool.OutputImage.Width == 640)
    {
      block1.Run();
    }
    else
    {
      block2.Run();
    }
    mToolBlock.Outputs["outtoolblock"].Value = block1.Outputs["strerr"].Value.ToString() + block2.Outputs["strerr"].Value.ToString();
    
    
    return false;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// <summary>
  /// Called when the last run record may have changed and is being reconstructed
  /// </summary>
  /// <param name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}

 最后就能实现在距离计算图片输入的时候,执行Block1,找字符R的时候,执行Block2,同时旁边的图片均能显示。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值