C# 定位,卡尺,C#代码结合,测量宽度。

发布时间:2026/7/29 5:46:06
C# 定位,卡尺,C#代码结合,测量宽度。 E:\DISK1\VisionPro\Images\bracket_std.idb创建两个块第一个做模板匹配和定位第二个是卡尺把8位图给输出为什么不是参数而是图片因为参数绑定图片上了。第二个卡尺块分别设置好给的坐标查看运行正常开始写代码测试代码可跳过我想写个测试代码用来输出内容这一部分可以不用看//测试类的标签定义一下CogGraphicLabel test;//创建标签组件类 - 用来测试GroupRun里面的test new CogGraphicLabel();//设置标签的参数test.Font new Font(微软雅黑,12); //字体test.Color CogColorConstants.Yellow; //设置颜色test.X 20;//x坐标test.Y 20;//y坐标test.Text string.Format(测试);//在ModifyLastRunRecord里面写//输出标签mToolBlock.AddGraphicToRunRecord(test,lastRecord,CogCaliperTool1.InputImage,script);就OJBK了开始写代码代码列表集合包using System.Collections.Generic;//集合的包运行函数里面写的代码//先找到mToolBlock块的类拿到里面的三个卡尺工具//卡尺1CogCaliperTool kc1 mToolBlock.Tools[CogCaliperTool1] as CogCaliperTool;//获取值类型CogCaliperResult kc1zlx kc1.Results[0];//卡尺2CogCaliperTool kc2 mToolBlock.Tools[CogCaliperTool2] as CogCaliperTool;//获取值类型CogCaliperResult kc2zlx kc2.Results[0];//卡尺3CogCaliperTool kc3 mToolBlock.Tools[CogCaliperTool3] as CogCaliperTool;//获取值类型CogCaliperResult kc3zlx kc3.Results[0];//清空每次执行清空防止多次执行。bqlb.Clear();//创建第1个卡尺坐标,获取宽度//设置标签的参数CogGraphicLabel test1 new CogGraphicLabel();//创建标签组件类test1.Font new Font(微软雅黑, 12); //字体test1.Color CogColorConstants.Yellow; //设置颜色test1.X kc1zlx.PositionX;//x坐标test1.Y kc1zlx.PositionY;//y坐标test1.Text string.Format(卡尺1宽度{0}, kc1zlx.Width);//设置标签内容bqlb.Add(test1);//添加到集合里面//创建第2个卡尺坐标,获取宽度CogGraphicLabel test2 new CogGraphicLabel();//创建标签组件类test2.Font new Font(微软雅黑, 12); //字体test2.Color CogColorConstants.Yellow; //设置颜色test2.X kc2zlx.PositionX;//x坐标test2.Y kc2zlx.PositionY;//y坐标test2.Text string.Format(卡尺2宽度{0}, kc2zlx.Width);//设置标签内容bqlb.Add(test2);//添加到集合里面//创建第3个卡尺坐标,获取宽度CogGraphicLabel test3 new CogGraphicLabel();//创建标签组件类test3.Font new Font(微软雅黑, 12); //字体test3.Color CogColorConstants.Yellow; //设置颜色test3.X kc3zlx.PositionX;//x坐标test3.Y kc3zlx.PositionY;//y坐标test3.Text string.Format(卡尺3宽度{0}, kc3zlx.Width);//设置标签内容bqlb.Add(test3);//添加到集合里面图片输出里面写public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){//输出列表里的所有标签foreach(CogGraphicLabel dgbq in bqlb){//输出测试标签mToolBlock.AddGraphicToRunRecord(dgbq,lastRecord,CogCaliperTool1.InputImage,script);}}结果