VisionPro中ToolBlock工具允许用户添加系统默认(bool、int、double等)数据类型以及VisionPro自定义(CogImage8Grey、ICogImage等)数据类型,常见数据类型的添加入下图所示。
但是,有时需要在ToolBlock输出端添加List等相对复杂的数据类型。例如,返回当前匹配工具获取到的多个产品的XY坐标以及角度。首先,每个变量需要存储X、Y、A三个维度的信息。其次,每张图像中可能含有多个产品且产品个数不确定。具体实现方式如下:
- 添加List的程序集以及命名空间
- 创建List 对象
- 在初始化函数中创建CogToolBlockTerminal 对象与创建的List对象进行绑定,用CogToolBlockTerminal类的Add方法添加到ToolBlock的输出终端。
- 在GroupRun中对List实例对象进行数据的修改
1 using System; 2 using System.Collections; 3 using System.Drawing; 4 using System.IO; 5 using System.Windows.Forms; 6 using Cognex.VisionPro; 7 using Cognex.VisionPro.ToolBlock; 8 using System.Collections.Generic; 9 public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase 10 { 11
12 private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; 13 List<Tuple<double,double,double>> MatchedPosition= null; 14 public override bool GroupRun(ref string message, ref CogToolResultConstants result) 15 { 16 foreach(ICogTool tool in mToolBlock.Tools) 17 mToolBlock.RunTool(tool, ref message, ref result); 18 //此处添加对MatchedPosition的访问与修改
19 return false; 20 } 21
22 public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host) 23 { 24 base.Initialize(host); 25 this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host)); 26 if(MatchedPosition== null) 27 { 28 MatchedPosition = new List<Tuple<double,double,double>>(); 29 } 30 if(!mToolBlock.Outputs.Contains("MatchedPosition")) 31 { 32 CogToolBlockTerminal m = new CogToolBlockTerminal("MatchedPosition", MatchedPosition.GetType()); 33 mToolBlock.Outputs.Add(m); 34 } 35
36 } 37 }