public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明label成员变量
CogGraphicLabel label;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//映射 CogCompositeColorMatchTool 对象
CogCompositeColorMatchTool colorMatch = mToolBlock.Tools["CogCompositeColorMatchTool1"]as CogCompositeColorMatchTool;
//映射 CogBlobTool对象
CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
label = new CogGraphicLabel();
int yellow=0,green = 0,blue = 0,orange = 0,red = 0;
for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{
//创建圆心图形
CogCircle c = new CogCircle();
//使用blob结果的中心位置 (保险丝案例使用的时pma结果的中心位置)
double x = blob.Results.GetBlobs()[i].CenterOfMassX;
double y = blob.Results.GetBlobs()[i].CenterOfMassY;
//设置圆心坐标
c.CenterX = x;
c.CenterY = y;
c.Radius = 5;
//设置颜色匹配工具 的 检测区域
colorMatch.Region = c;
//执行工具
colorMatch.Run();
string colorName = colorMatch.Result.ResultOfBestMatch.Color.Name;
switch (colorName)
{
case "Yellow":
yellow++;
break;
case "Green":
green++;
break;
case "Blue":
blue++;
break;
case "Orange":
orange++;
break;
case "Red":
red++;
break;
}
//使用可变字符串进行拼接
StringBuilder sb = new StringBuilder();
sb.Append("Red: ").Append(red).Append(" Orange: ").Append(orange).
Append(" Green: ").Append(green).Append(" Blue: ").Append(blue).
Append(" Yellow: ").Append(yellow);
//显示label
label.SetXYText(150, 20, sb.ToString());
label.Color = CogColorConstants.Green;
label.Font = new Font("宋体", 20);
}
return false;
}