Visionpro如何编写工具组(ToolGroup)脚本

建立如图所示的作业:

在脚本中 public class UserScript : CogToolGroupBaseScript{ }内添加变量:

 private double                    Distance = 0;
 private CogDistancePointPointTool DistancePointPointToolObject;

在脚本GroupRun构造函数下添加代码:

DistancePointPointToolObject = (CogDistancePointPointTool) toolGroup.Tools["CogDistancePointPointTool1"];
    Distance = DistancePointPointToolObject.Distance;

在脚本中ModifyLastRunRecord构造函数添加代码:

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    CogGraphicLabel ResultLabel = new CogGraphicLabel();
    string labelStr = string.Format("Distance={0:F2} pixel", Distance);
    ResultLabel.SetXYText((DistancePointPointToolObject.StartX + DistancePointPointToolObject.EndX) / 2, DistancePointPointToolObject.EndY, labelStr);
    ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Green;
    toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord, "图像转换_CogImageConvertTool1.OutputImage", "script");
  }

完成代码如下:

using System;
using Cognex.VisionPro;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ToolGroup;
using Cognex.VisionPro.Dimensioning;

public class UserScript : CogToolGroupBaseScript
{
  private double                    Distance = 0;
  private CogDistancePointPointTool DistancePointPointToolObject;

  // The GroupRun function is called when the tool group is run.  The default
  // implementation provided here is equivalent to the normal behavior of the
  // tool group.  Modifying this function will allow you to change the behavior
  // when the tool group is run.
  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 in the tool group using the RunTool function
    for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++)
      toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result);

    // Returning False indicates we ran the tools in script, and they should not be
    // run by VisionPro 
    
    DistancePointPointToolObject = (CogDistancePointPointTool) toolGroup.Tools["CogDistancePointPointTool1"];
    Distance = DistancePointPointToolObject.Distance;
    
    return false;
  }

#region "When the Current Run Record is Created"
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
#endregion

#region "When the Last Run Record is Created"
  // Allows you to add or modify the contents of the last run record when it is
  // created.  For example, you might add custom graphics to the run record here.
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    CogGraphicLabel ResultLabel = new CogGraphicLabel();
    string labelStr = string.Format("Distance={0:F2} pixel", Distance);
    ResultLabel.SetXYText((DistancePointPointToolObject.StartX + DistancePointPointToolObject.EndX) / 2, DistancePointPointToolObject.EndY, labelStr);
    ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Green;
    toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord, "图像转换_CogImageConvertTool1.OutputImage", "script");
  }
#endregion

#region "When the Script is Initialized"
  // Perform any initialization required by your script here
  public override void Initialize(CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);
  }
#endregion

}

参考:

蔚来教育企业店

 

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值