建立如图所示的作业:
在脚本中 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
}
参考: