一、低级脚本
1、添加所需终端
2、编写低级脚本
#region namespace imports 引用程序集
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.CalibFix;
#endregion
public class CogToolBlockSimpleScript : CogToolBlockAdvancedScript
{
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
List<double> rectangleAffine = null; //矩形
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//获取该组的Outputs中的Output
this.Outputs.Output = "";
this.Outputs.Text = "";
this.Outputs.OutputXYText = "";
double CenterX = (this.Inputs.CenterOfMassX1 + this.Inputs.CenterOfMassX2) / 2;
double CenterY = (this.Inputs.CenterOfMassY1 + this.Inputs.CenterOfMassY3) / 2;
double LengthX = this.Inputs.CenterOfMassX1 - this.Inputs.CenterOfMassX2;
double LengthY = this.Inputs.CenterOfMassY1 - this.Inputs.CenterOfMassY3;
double X = CenterX + LengthX;
double Y = CenterY + LengthY;
this.Outputs.LengthX = LengthX;
this.Outputs.LengthY = LengthY;
//CogFixtureTool坐标需要通过CogTransform2DLinear转化
//将x,y,Rotation赋给CogFixtureTool2
CogTransform2DLinear ct = new CogTransform2DLinear();
ct.TranslationX = X;
ct.TranslationY = Y;
ct.Rotation = this.Inputs.Result_GetLine_Rotation;
this.Tools.CogFixtureTool2.RunParams.UnfixturedFromFixturedTransform = ct;
this.Tools.CogFixtureTool2.Run();
double originX = 0.847365 + this.Inputs.X;
double originY = 7.70912 + this.Inputs.Y;
double lX = 198.194 + this.Inputs.LX;
double lY = 317.618 + this.Inputs.LY;
double rotation = 0 + this.Inputs.Rotation;
double skew = 0 + this.Inputs.Skew;
CogRectangleAffine cra = new CogRectangleAffine();
this.Outputs.X = originX;
this.Outputs.Y = originY;
this.Outputs.LX = lX;
this.Outputs.LY = lY;
this.Outputs.Rotation = rotation;
this.Outputs.Skew = skew;
//循环
for(int j = 0;j <= 5;j++)
{
for(int i = 0;i <= 12;i++)
{
cra.SetCenterLengthsRotationSkew(originX - (i * LengthX), originY - (j * LengthY), lX, lY, rotation, skew); //使用CogRectangleAffine的方法
//向集合列表中加入数据
rectangleAffine.Add(originX - (i * LengthX));
rectangleAffine.Add(originY - (j * LengthY));
rectangleAffine.Add(lX);
rectangleAffine.Add(lY);
rectangleAffine.Add(rotation);
rectangleAffine.Add(skew);
this.Tools.CogBlobTool1.Region = cra;
this.Tools.CogBlobTool1.Run();
if(this.Tools.CogBlobTool1.Results.GetBlobs().Count.Equals(4)) //判断是否有4个结果
{
this.Outputs.OutputXYText = this.Outputs.OutputXYText + (originX - (i * LengthX)).ToString() + "," + (originY - (j * LengthY)).ToString() + "," + "OK";
}else{
this.Outputs.OutputXYText = this.Outputs.OutputXYText + (originX - (i * LengthX)).ToString() + "," + (originY - (j * LengthY)).ToString() + "," + "NG";
}
if(i.Equals(12))
{
this.Outputs.Text = this.Outputs.Text + this.Tools.CogBlobTool1.Results.GetBlobs().Count.ToString();
this.Outputs.Output = this.Outputs.Output + this.Tools.CogBlobTool1.Results.GetBlobs().Count.ToString() + ";";
}else
{
this.Outputs.Text = this.Outputs.Text + this.Tools.CogBlobTool1.Results.GetBlobs().Count.ToString();
this.Outputs.Output = this.Outputs.Output+ this.Tools.CogBlobTool1.Results.GetBlobs().Count.ToString() + ",";
}
}
}
return false;
}
}
注意:添加一次终端的在脚本里点击检查界面终端
二、总结
在低级脚本中只需要
this.工具名.终端名 来获取终端的数据
注意:终端中一般找所需的数据只用(Results:结果、InputImage:输入图片)中找就行