VisionPro ToolBlock脚本编写&通过脚本绘制Record显示简单示例

发布时间 2023-08-25 11:45:44作者: 一杯清酒邀明月

1. 导读:
前面有一篇博客分享了如何使用C#调用ToolBlock,以完成一个简单的视觉开发项目。今天主要分享一下在ToolBlock中,如何编写运行脚本,主要包括以下几个方面的演示:

  1. ToolBlock添加输入项,主要用于用户界面参数修改,然后以参数的形式传入ToolBlock,以影响算法工具。
  2. 通过函数绘制自定义图形Graphics,然后将其打印到ToolBlock的某一个图层中(在VP中常说的Record类似于PS中的图层)。
  3. 输出一个不常用的数据类型,PMAlign工具的运行结果对象,也就是CogPMAlignResults类型。

最终效果:

2. 实现步骤:

在ToolBlock中添加一个CogPMAlignTool 工具,然后选择一张边缘对比较好的图像,训练模板。

 选择ToolBlock的输出引脚类型–CogPMAlignResults

为当前ToolBlocl编写脚本,注意:这里选择C#高级脚本模式。

  1 public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
  2 {  
  3   private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  4   private CogPMAlignTool pmaTool = new CogPMAlignTool();
  5   private CogGraphicCollection graphicList = new CogGraphicCollection();
  6   public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  7   {
  8      //if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
  9     graphicList.Clear();
 10    
 11     //初始化PMAlign工具
 12     pmaTool = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
 13     
 14     //将ToolBlock的Inputs[SetCount]引脚设置为PMAlignTool的预期查找数量
 15     pmaTool.RunParams.ApproximateNumberToFind = (int) mToolBlock.Inputs["SetCount"].Value;
 16 
 17      //运行PMAlign工具
 18     pmaTool.Run();
 19     
 20      //获取结果,并将自定义图像添加到画板中
 21     foreach (CogPMAlignResult item in pmaTool.Results)
 22     {
 23       graphicList.Add(CreateCircle(item.GetPose().TranslationX, item.GetPose().TranslationY, 50, CogColorConstants.Green));
 24       graphicList.Add(CreatLabel("Score:" + item.Score.ToString("0.000"), 12, item.GetPose().TranslationX - 30, item.GetPose().TranslationY - 25, CogColorConstants.Green));
 25     }
 26 
 27     //将PMAlign工具的结果集合绑定到ToolBlock的输出
 28     mToolBlock.Outputs["ResultList"].Value = pmaTool.Results;
 29     
 30     //如果ToolBlock内的运行流程是自定义的,则返回false,否则如果按照正常流程执行则返回true
 31     return false;
 32   }
 33     /// <summary>
 34   /// 绘制仿射矩形
 35   /// </summary>
 36    private CogRectangleAffine CreateRectangleAffine(double Postion_X, double Postion_Y, double Weight, double Height, double Rotation, CogColorConstants Color)
 37   {
 38     CogRectangleAffine RectangleAffine = new CogRectangleAffine();
 39     RectangleAffine.SetOriginLengthsRotationSkew(Postion_X, Postion_Y, Weight, Height, Rotation, 0);
 40     RectangleAffine.Color = Color;
 41     RectangleAffine.LineStyle = CogGraphicLineStyleConstants.Solid;
 42     RectangleAffine.LineWidthInScreenPixels = 1;
 43     //RectangleAffine.SelectedSpaceName = affine.Region.SelectedSpaceName;
 44     return RectangleAffine;
 45   }
 46    /// <summary>
 47   /// 绘制矩形
 48   /// </summary>
 49   private CogRectangle CreatRectangle(double Postion_X, double Postion_Y, double Weight, double Height, CogColorConstants Color)
 50   {
 51     CogRectangle Rectangle = new CogRectangle();
 52     Rectangle.X = Postion_X;
 53     Rectangle.Y = Postion_Y;
 54     Rectangle.SetCenterWidthHeight(Rectangle.X, Rectangle.Y, Weight, Height);
 55     Rectangle.Color = Color;
 56     Rectangle.LineStyle = CogGraphicLineStyleConstants.Solid;
 57     Rectangle.LineWidthInScreenPixels = 5;
 58     return Rectangle;
 59   }
 60 
 61    /// <summary>
 62   /// 绘制标签
 63   /// </summary>
 64   private CogGraphicLabel CreatLabel(string text, float size, double x, double y, CogColorConstants Color)
 65   {
 66     CogGraphicLabel label = new CogGraphicLabel();
 67     label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Point);
 68     label.Color = Color;
 69     label.SetXYText(x, y, text);
 70     label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
 71     return label;
 72   }
 73 
 74    /// <summary>
 75   /// 绘制圆形
 76   /// </summary>
 77   private CogCircle CreateCircle(double Center_X,double Center_Y,double Radius,CogColorConstants color)
 78   {
 79     CogCircle circle = new CogCircle();
 80     circle.CenterX = Center_X;
 81     circle.CenterY = Center_Y;
 82     circle.Radius = Radius;
 83     circle.Color = color;
 84     circle.LineStyle=CogGraphicLineStyleConstants.Solid;
 85     circle.LineWidthInScreenPixels = 3;
 86     return circle;
 87   }
 88 
 89    /// <summary>
 90   /// 当上一次的记录发生改变,并正在重构时调用此函数
 91   /// </summary>
 92   /// <param name="lastRecord">新的运行记录</param>
 93   public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
 94   {
 95     // 遍历画板,然后将自定义图形添加到ToolBlock的新的绘制记录上
 96     foreach (ICogGraphic item in graphicList)
 97     {
 98       mToolBlock.AddGraphicToRunRecord(item, lastRecord, "CogPMAlignTool1.InputImage", "Script");
 99     }
100   }
101 }

开发程序中,在ToolBlock的Ran事件回调函数中显示,并获取结果。

 1  private void OnToolBlockRan(object sender, EventArgs e)
 2  {
 3      cogRecordDisplay2.Image = null;
 4      //将当前ToolBlock的Record显示在RecordDisplay控件上
 5      cogRecordDisplay2.Record = (CogRecord)cogToolBlock2.CreateLastRunRecord().SubRecords[0];
 6      cogRecordDisplay2.Fit(true);
 7 
 8      //获取ToolBlock的输出结果,然后显示DataGridView控件中
 9      List<DataObj> dataList = new List<DataObj>();
10      CogPMAlignResults resultList = (CogPMAlignResults)cogToolBlock2.Outputs["ResultList"].Value;
11      foreach (CogPMAlignResult item in resultList)
12      {
13         dataList.Add(new DataObj(item.ID, item.GetPose().TranslationX, item.GetPose().TranslationY, item.GetPose().Rotation, item.GetPose().Scaling, item.Score));
14      }
15      dgv_DataList.DataSource = dataList;
16  }