VisionPro 如何编写工具块(ToolBlock)脚本

在脚本中添加相应代码便可显示相应字符:

VisionPro中添加如下工具并建立链接:

脚本中添加变量Radius、CogFindCircleToolObject、myCircle如下:

1 #region Private Member Variables
2   private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
3   private double                             Radius = 0;
4   private CogFindCircleTool              CogFindCircleToolObject;
5   private CogCircle                      myCircle;
6   #endregion

脚本GroupRun中添加代码如下:

 1  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
 2   {
 3     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
 4     // #if DEBUG
 5     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
 6     // #endif
 7  
 8  
 9     // Run each tool using the RunTool function
10     foreach(ICogTool tool in mToolBlock.Tools)
11       mToolBlock.RunTool(tool, ref message, ref result);
12     
13     CogFindCircleToolObject = (CogFindCircleTool) mToolBlock.Tools["CogFindCircleTool1"];
14     
15     myCircle = CogFindCircleToolObject.Results.GetCircle();
16     
17     Radius = double.Parse(myCircle.Radius.ToString("0.00"));
18  
19     return false;
20   }

脚本ModifyLastRunRecord中添加代码如下:

1 public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
2   {
3     CogGraphicLabel ResultLabel = new CogGraphicLabel();
4     string labelStr = string.Format("Radius={0:F2} pixel", Radius);
5     ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
6     ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
7     mToolBlock.AddGraphicToRunRecord(ResultLabel, lastRecord, "CogImageConvertTool1.OutputImage", "script");
8   }

完整脚本代码:

 1 #region namespace imports
 2 using System;
 3 using System.Collections;
 4 using System.Drawing;
 5 using System.IO;
 6 using System.Windows.Forms;
 7 using Cognex.VisionPro;
 8 using Cognex.VisionPro.ToolBlock;
 9 using Cognex.VisionPro3D;
10 using Cognex.VisionPro.QuickBuild.Implementation.Internal;
11 using Cognex.VisionPro.ImageProcessing;
12 using Cognex.VisionPro.Caliper;
13 #endregion
14  
15 public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
16 {
17   #region Private Member Variables
18   private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
19   private double                             Radius = 0;
20   private CogFindCircleTool              CogFindCircleToolObject;
21   private CogCircle                      myCircle;
22   #endregion
23  
24   /// <summary>
25   /// Called when the parent tool is run.
26   /// Add code here to customize or replace the normal run behavior.
27   /// </summary>
28   /// <param name="message">Sets the Message in the tool's RunStatus.</param>
29   /// <param name="result">Sets the Result in the tool's RunStatus</param>
30   /// <returns>True if the tool should run normally,
31   ///          False if GroupRun customizes run behavior</returns>
32   public override bool GroupRun(ref string message, ref CogToolResultConstants result)
33   {
34     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
35     // #if DEBUG
36     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
37     // #endif
38  
39  
40     // Run each tool using the RunTool function
41     foreach(ICogTool tool in mToolBlock.Tools)
42       mToolBlock.RunTool(tool, ref message, ref result);
43     
44     CogFindCircleToolObject = (CogFindCircleTool) mToolBlock.Tools["CogFindCircleTool1"];
45     
46     myCircle = CogFindCircleToolObject.Results.GetCircle();
47     
48     Radius = double.Parse(myCircle.Radius.ToString("0.00"));
49  
50     return false;
51   }
52  
53   #region When the Current Run Record is Created
54   /// <summary>
55   /// Called when the current record may have changed and is being reconstructed
56   /// </summary>
57   /// <param name="currentRecord">
58   /// The new currentRecord is available to be initialized or customized.</param>
59   public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
60   {
61   }
62   #endregion
63  
64   #region When the Last Run Record is Created
65   /// <summary>
66   /// Called when the last run record may have changed and is being reconstructed
67   /// </summary>
68   /// <param name="lastRecord">
69   /// The new last run record is available to be initialized or customized.</param>
70   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
71   {
72     CogGraphicLabel ResultLabel = new CogGraphicLabel();
73     string labelStr = string.Format("Radius={0:F2} pixel", Radius);
74     ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
75     ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
76     mToolBlock.AddGraphicToRunRecord(ResultLabel, lastRecord, "CogImageConvertTool1.OutputImage", "script");
77   }
78   #endregion
79  
80   #region When the Script is Initialized
81   /// <summary>
82   /// Perform any initialization required by your script here
83   /// </summary>
84   /// <param name="host">The host tool</param>
85   public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
86   {
87     // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
88     base.Initialize(host);
89  
90  
91     // Store a local copy of the script host
92     this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
93   }
94   #endregion
95  
96 }
原文地址:https://www.cnblogs.com/ybqjymy/p/14463705.html