一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

建立如图所示的作业:

在脚本中 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 }

 

posted on 2021-03-01 15:10  一杯清酒邀明月  阅读(1638)  评论(3编辑  收藏  举报