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