本案例需要实现如下功能:
1.加载toolBlock
2.加载图片,
3.运行Block
4.VisionPro中添加脚本显示数值。
见下图:详细代码(C#以及visionPro)见下面链接:
https://download.csdn.net/download/qq_34047402/90489941
C#加载运行VisonPro的toolblock资源-CSDN文库
主要代码罗列:
1.加载toolblock
toolBlock = CogSerializer.LoadObjectFromFile(Application.StartupPath + "\\VPP_ToolBlock\\ToolBlock1_卡尺.vpp") as CogToolBlock;
2.加载图片
using (CogImageFileTool imageFileTool = new CogImageFileTool())
{
imageFileTool.Operator.Open(Application.StartupPath + "\\pic\\1.bmp", CogImageFileModeConstants.Read);
imageFileTool.Run();
if(toolBlock!=null)
{
this.toolBlock.Inputs["InputImage"].Value = imageFileTool.OutputImage;
this.cogRecordDisplay1.Image = imageFileTool.OutputImage;
}
}
3.运行
if (toolBlock != null)
{
this.toolBlock.Run();
this.cogRecordDisplay1.Record = this.toolBlock.CreateLastRunRecord().SubRecords[0];
this.textBox1.Text = this.toolBlock.Outputs["Width"].Value.ToString();
}
4.脚本:
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
CogPMAlignTool PMAlignTool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogGraphicLabel label = new CogGraphicLabel();
double x = PMAlignTool.Results[0].GetPose().TranslationX;
double y = PMAlignTool.Results[0].GetPose().TranslationY;
double width = Convert.ToDouble(mToolBlock.Outputs["Width"].Value);
label.SetXYText(x, y,width.ToString("f2"));
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogPMAlignTool1.InputImage", "");
}
【备注】: C#中如果无法添加控件,可以先把dll所在的目录名称修改为:ReferencedAssemblies1. 然后添加控件。然后再改回来:ReferencedAssemblies