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


建立如图所示的作业:

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

1  private double                    Distance = 0; 2  private CogDistancePointPointTool DistancePointPointToolObject;

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

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

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

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

完成代码如下:

 1 using System;  2 using Cognex.VisionPro;  3 using Cognex.VisionPro3D;  4 using Cognex.VisionPro.ToolGroup;  5 using Cognex.VisionPro.Dimensioning;  6  
 7 public class UserScript : CogToolGroupBaseScript  8 {  9   private double                    Distance = 0; 10   private CogDistancePointPointTool DistancePointPointToolObject; 11  
12   // The GroupRun function is called when the tool group is run. The default 13   // implementation provided here is equivalent to the normal behavior of the 14   // tool group. Modifying this function will allow you to change the behavior 15   // when the tool group is run.
16   public override bool GroupRun(ref string message, ref CogToolResultConstants result) 17  { 18     // To let the execution stop in this script when a debugger is attached, uncomment the following lines. 19     // #if DEBUG 20     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); 21     // #endif 22  
23     // Run each tool in the tool group using the RunTool function
24     for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++) 25       toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result); 26  
27     // Returning False indicates we ran the tools in script, and they should not be 28     // run by VisionPro 
29     
30     DistancePointPointToolObject = (CogDistancePointPointTool) toolGroup.Tools["CogDistancePointPointTool1"]; 31     Distance = DistancePointPointToolObject.Distance; 32     
33     return false; 34  } 35  
36 #region "When the Current Run Record is Created"
37   public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord) 38  { 39  } 40 #endregion
41  
42 #region "When the Last Run Record is Created"
43   // Allows you to add or modify the contents of the last run record when it is 44   // created. For example, you might add custom graphics to the run record here.
45   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord) 46  { 47     CogGraphicLabel ResultLabel = new CogGraphicLabel(); 48     string labelStr = string.Format("Distance={0:F2} pixel", Distance); 49     ResultLabel.SetXYText((DistancePointPointToolObject.StartX + DistancePointPointToolObject.EndX) / 2, DistancePointPointToolObject.EndY, labelStr); 50     ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Green; 51     toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord, "图像转换_CogImageConvertTool1.OutputImage", "script"); 52  } 53 #endregion
54  
55 #region "When the Script is Initialized"
56   // Perform any initialization required by your script here
57   public override void Initialize(CogToolGroup host) 58  { 59     // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
60     base.Initialize(host); 61  } 62 #endregion
63  
64 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM