先加入测量定位辅助工具 如图。
求精准度的话加上 相机标定 和 手眼标定 工具
选择需要输入的参数 中心点x.y以及旋转角度参数都在这里
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
CogGraphicCollection GC = new CogGraphicCollection();//文本参数实例化
#endregion
以上第一部分 实例化需要的文本集合
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// To let the execution stop in this script when a debugger is attached, uncomment the following lines.
// #if DEBUG
// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
// #endif
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
CogPMAlignTool P = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;//定位图片工具
CogCaliperTool C = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;//卡尺工具
GC.Clear();//清除集合的内容
foreach(CogPMAlignResult item in P.Results){
//传入需要的参数
C.Region.CenterX = item.GetPose().TranslationX;
C.Region.CenterY = item.GetPose().TranslationY;
C.Region.Rotation = item.GetPose().Rotation;
C.Run();//如果不写,则显示的结果都是一样的
CogGraphicLabel L = new CogGraphicLabel();
L.Color = CogColorConstants.Red;
L.Font = new Font("宋体",12);
L.SetXYText(C.Region.CenterX,C.Region.CenterY,"距离:"+C.Results[0].Width.ToString("F2"));
GC.Add(L);
}
return false;
}
成像层代码 将以上内容传至显示页面
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
CogPMAlignTool P = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
foreach(ICogGraphic L in GC){
mToolBlock.AddGraphicToRunRecord(L,lastRecord,"CogPMAlignTool1.InputImage","");
}
if(P.Results.Count == 0)
{
GC.Clear(); //如果没有读取到则清除屏幕
}
}
效果图