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 }