一、简单绘制标签、矩形
1、简单绘制标签
1、新建一个CogToolBlock
2、添加所需工具、之后编写脚本
我用的是第二个c#的高级脚本
3、编写脚本
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
//成员变量(属性)
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
private CogGraphicLabel labels; //1、创建标签
#endregion
//运行该Group中的工具(返回值为true,所有的属于当前Group的视觉工具都将运行,如果返回值为false,用户可以自定义工具的执行顺序)
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//3、获取所需的工具(两种方法都可以)
CogDistancePointLineTool CogDistancePointLineTool1 = (CogDistancePointLineTool) mToolBlock.Tools["CogDistancePointLineTool1"];
//CogDistancePointLineTool CogDistancePointLineTool1 =mToolBlock.Tools["CogDistancePointLineTool1"] as CogDistancePointLineTool;
//4、编写标签的内容
//CogDistancePointLineTool1.Distance:我们标签所要显示的距离
//"{0:F2}":为我们要输出的格式 0是CogDistancePointLineTool1.Distance F2是小数点后2位
string labelStr = string.Format("{0:F2}", CogDistancePointLineTool1.Distance);
Font f = new Font(labelStr,20); //字体大小为20
labels.Font = f; //将字体大小赋给标签
labels.SetXYText(100, 200, labelStr); //标签的x,y,所显示的文本
labels.Color = CogColorConstants.Red; //标签颜色
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
return false;
}
#region When the Current Run Record is Created
//修改CurrentRecord
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region When the Last Run Record is Created
//修改LastRunRecord 例如:在最终生成图像中添加标签、该表颜色、用不同几何图像标记目标区域。
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//5、将标签labels显示在CogFixtureTool1.OutputImage上
mToolBlock.AddGraphicToRunRecord(labels, lastRecord, "CogFixtureTool1.OutputImage", "");
}
#endregion
#region When the Script is Initialized
//初始化
public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
this.labels = new CogGraphicLabel(); //2、实例化标签
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
4、结果
2、简单绘制矩形
1、脚本(前面步骤参照绘制标签)
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
private CogRectangleAffine rect; //1、创建矩形
#endregion
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//3、获取所需的工具
CogDistancePointLineTool CogDistancePointLineTool1 = (CogDistancePointLineTool) mToolBlock.Tools["CogDistancePointLineTool1"];
//4、编写矩形的内容
//参数: 坐标x为100,坐标y为200,矩形长为300,矩形宽为300,角度为0,倾斜为0
rect.SetCenterLengthsRotationSkew(100,200,300,300,0,0);
rect.Color = CogColorConstants.Red; //标签颜色
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
return false;
}
#region When the Current Run Record is Created
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region When the Last Run Record is Created
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//5、将矩形rect显示在CogFixtureTool1.OutputImage上
mToolBlock.AddGraphicToRunRecord(rect, lastRecord, "CogFixtureTool1.OutputImage", "");
}
#endregion
#region When the Script is Initialized
public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
this.rect = new CogRectangleAffine(); //2、实例化矩形
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}