一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 576万

建立如图所示的作业:

在脚本中 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   一杯清酒邀明月  阅读(1684)  评论(3编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示