在
V
i
s
i
o
n
p
r
o
中
编
写
C
S
h
a
r
p
脚
本
在Visionpro中编写CSharp脚本
在Visionpro中编写CSharp脚本
using System;using Cognex.VisionPro;using Cognex.VisionPro3D;using Cognex.VisionPro.ToolGroup;using System.Windows.Forms;using System.Drawing ;using Cognex.VisionPro.ImageProcessing ;using Cognex.VisionPro.Caliper;publicclassUserScript:CogToolGroupBaseScript{privatedouble Radius =0;privateCogCircle myCircle;privateCogFindCircleTool myCF;// 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.publicoverrideboolGroupRun(refstring message,refCogToolResultConstants 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 functionfor(Int32 toolIdx =0; toolIdx < toolGroup.Tools.Count; toolIdx++)
toolGroup.RunTool(toolGroup.Tools[toolIdx],ref message,ref result);
myCF =(CogFindCircleTool)toolGroup.Tools["CogFindCircleTool1"];
myCircle = myCF.Results.GetCircle();
Radius =double.Parse(myCircle.Radius.ToString("0.00"));// Returning False indicates we ran the tools in script, and they should not be// run by VisionPro returnfalse;}#region"When the Current Run Record is Created"publicoverridevoidModifyCurrentRunRecord(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.publicoverridevoidModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){CogGraphicLabel ResultLabel =newCogGraphicLabel();string labelStr =string.Format("Radius={0:F2} pixel", Radius);
ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
ResultLabel.SelectedSpaceName =@"@\Fixture";
toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord,"Image Source.OutputImage","script");
toolGroup.AddGraphicToRunRecord(myCircle, lastRecord,"Image Source.OutputImage","script");/*
Should only be called from tool group script. Adds a graphic to a specified run record
参数一: A CogGraphic object to add
参数二: A tree of CogRecords
参数三: The name of the specific record where the graphic should be added
参数四: A key string used to identify your graphic
*/for(int i =0;i < myCF.Results.Count -1;i++){CogCompositeShape myCS =newCogCompositeShape();//myCS = myCF.Results.Item(i).CreateResultGraphics(Cognex.VisionPro.Caliper.CogCaliperResultGraphicConstants.All);
toolGroup.AddGraphicToRunRecord(myCS, lastRecord,"Image Source.OutputImage","script");}}#endregion#region"When the Script is Initialized"// Perform any initialization required by your script herepublicoverridevoidInitialize(CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
myCF =newCogFindCircleTool();
myCircle =newCogCircle();base.Initialize(host);}#endregion}