VisionPro 编写C#脚本

发布时间 2023-08-25 10:50:41作者: 一杯清酒邀明月

 1 using System;
 2 using Cognex.VisionPro;
 3 using Cognex.VisionPro3D;
 4 using Cognex.VisionPro.ToolGroup;
 5 using System.Windows.Forms;
 6 using System.Drawing ;
 7 using Cognex.VisionPro.ImageProcessing ;
 8 using Cognex.VisionPro.Caliper;
 9 
10 
11 public class UserScript : CogToolGroupBaseScript
12 {
13   private double Radius = 0;
14   private CogCircle myCircle;
15   private CogFindCircleTool myCF;
16   
17   // The GroupRun function is called when the tool group is run.  The default
18   // implementation provided here is equivalent to the normal behavior of the
19   // tool group.  Modifying this function will allow you to change the behavior
20   // when the tool group is run.
21   public override bool GroupRun(ref string message, ref CogToolResultConstants result)
22   {
23     
24     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
25     // #if DEBUG
26     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
27     // #endif
28 
29     // Run each tool in the tool group using the RunTool function
30     for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++)
31       toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result);
32     
33 
34     myCF = (CogFindCircleTool)toolGroup.Tools["CogFindCircleTool1"];
35 
36     myCircle = myCF.Results.GetCircle();
37         
38     Radius = double.Parse(myCircle.Radius.ToString("0.00"));
39     
40     
41     // Returning False indicates we ran the tools in script, and they should not be
42     // run by VisionPro 
43     return false;
44   }
45 
46 #region "When the Current Run Record is Created"
47   public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
48   {
49   }
50 #endregion
51 
52 #region "When the Last Run Record is Created"
53   // Allows you to add or modify the contents of the last run record when it is
54   // created.  For example, you might add custom graphics to the run record here.
55   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
56   {
57     CogGraphicLabel ResultLabel = new CogGraphicLabel();
58     string labelStr = string.Format("Radius={0:F2} pixel", Radius);
59     ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
60     ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
61     ResultLabel.SelectedSpaceName = @"@\Fixture";
62     toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord, "Image Source.OutputImage", "script");
63     toolGroup.AddGraphicToRunRecord(myCircle, lastRecord, "Image Source.OutputImage", "script");
64     /*
65     Should only be called from tool group script. Adds a graphic to a specified run record
66     参数一: A CogGraphic object to add
67     参数二: A tree of CogRecords
68     参数三: The name of the specific record where the graphic should be added
69     参数四: A key string used to identify your graphic 
70     */
71     
72     for( int i = 0 ;i < myCF.Results.Count - 1;i++)
73     {
74       CogCompositeShape myCS = new CogCompositeShape();
75       //myCS = myCF.Results.Item(i).CreateResultGraphics(Cognex.VisionPro.Caliper.CogCaliperResultGraphicConstants.All);
76       toolGroup.AddGraphicToRunRecord(myCS, lastRecord, "Image Source.OutputImage", "script");
77     }
78     
79     
80   }
81 #endregion
82 
83 #region "When the Script is Initialized"
84   // Perform any initialization required by your script here
85   public override void Initialize(CogToolGroup host)
86   {
87     // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
88     myCF = new  CogFindCircleTool();
89     myCircle = new CogCircle();
90     base.Initialize(host);
91   }
92 #endregion
93 
94 }