(C#-VisionPro)用代码方式新建VisionPro视觉文件(.vpp)

痞子三分冷 提交于 2020-09-27 17:04:37

1.方法一:

引用:

using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro.Exceptions;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Display;
using System.Drawing;
using Cognex.VisionPro.CalibFix;
//新增工具
        public void VisionPro_Add()
        {
            //实例化工具
            CogToolBlock CogToolBlock1 = new CogToolBlock();
            CogPMAlignTool CogPMAlignTool1 = new CogPMAlignTool();
            CogFixtureTool CogFixtureTool1 = new CogFixtureTool();
            CogPMAlignTool CogPMAlignTool2 = new CogPMAlignTool();

            //将工具单独保存在加载
            CogPMAlignTool CogPMAlignTool3 = new CogPMAlignTool();
            CogPMAlignTool3 = CogSerializer.LoadObjectFromFile(@"E:\CogPMAlignTool1.vtt") as CogPMAlignTool;

            //给工具命名
            CogToolBlock1.Name = "CogToolBlock1";
            CogPMAlignTool1.Name = "CogPMAlignTool1";
            CogFixtureTool1.Name = "CogFixtureTool1";
            CogPMAlignTool2.Name = "CogPMAlignTool2";

            //将工具添加到CogToolBlock工具中再保存CogToolBlock工具
            CogToolBlock1.Tools.Add(CogPMAlignTool1);
            CogToolBlock1.Tools.Add(CogFixtureTool1);
            CogToolBlock1.Tools.Add(CogPMAlignTool2);
            CogSerializer.SaveObjectToFile(CogToolBlock1, @"E:\ToolBlock2.vpp");
            MessageBox.Show("保存成功");
        }

结果:

 

 方法二:添加工具输出项

//新增工具
        public void VisionPro_Add()
        {
            //实例化工具
            CogToolBlock CogToolBlock1 = new CogToolBlock();
            CogPMAlignTool CogPMAlignTool1 = new CogPMAlignTool();
            CogFixtureTool CogFixtureTool1 = new CogFixtureTool();
            CogPMAlignTool CogPMAlignTool2 = new CogPMAlignTool();

            //将工具单独保存在加载
            CogPMAlignTool CogPMAlignTool3 = new CogPMAlignTool();
            CogPMAlignTool3 = CogSerializer.LoadObjectFromFile(@"E:\CogPMAlignTool1.vtt") as CogPMAlignTool;
            
               
            //给工具命名
            CogToolBlock1.Name = "CogToolBlock1";
            CogPMAlignTool1.Name = "CogPMAlignTool1";
            CogFixtureTool1.Name = "CogFixtureTool1";
            CogPMAlignTool2.Name = "CogPMAlignTool2";

            string[] s1 = new string[1];
            string[] s2 = new string[5];
            s1[0] = "|InputImage|InputImage";
            s2[0] = "| Results.Item[0].GetPose() | Results.Item[0].GetPose()";
            s2[1] = "|Results.Item[0].GetPose().TranslationX|Results.Item[0].GetPose().TranslationX";
            s2[2] = "|Results.Item[0].GetPose().TranslationX|Results.Item[0].GetPose().TranslationY";
            s2[3] = "|Results.Item[0].GetPose().Rotation|Results.Item[0].GetPose().Rotation";
            s2[4] = "|Results.Item[0].Score|Results.Item[0].Score";

            CogPMAlignTool1.UserData.Add("_ToolInputTerminals", s1);//添加终端-InputImage
            CogPMAlignTool1.UserData.Add("_ToolOutputTerminals", s2);

            //将工具添加到CogToolBlock工具中再保存CogToolBlock工具
            CogToolBlock1.Tools.Add(CogPMAlignTool1);
            CogToolBlock1.Tools.Add(CogFixtureTool1);
            CogToolBlock1.Tools.Add(CogPMAlignTool2);
            CogSerializer.SaveObjectToFile(CogToolBlock1, @"E:\ToolBlock2.vpp");
            MessageBox.Show("保存成功");
        }

 

 方法三:单独保存工具再去加载

系统工具默认地址:

 

 

 

 保存工具模板

点击右键保存工具模板(.vtt)文件

 

 

//加载系统初始工具
            CogPMAlignTool CogPMAlignTool1 = CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\bin\Templates\Tools\C#\CogPMAlignTool.vtt") as CogPMAlignTool;
            CogPMAlignTool CogPMAlignTool2 = CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\bin\Templates\Tools\C#\CogPMAlignTool.vtt") as CogPMAlignTool;
            CogFixtureTool CogFixtureTool1 = CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\bin\Templates\Tools\C#\CogFixtureTool.vtt") as CogFixtureTool;
            CogToolBlock CogToolBlock1 = CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\bin\Templates\Tools\C#\CogToolBlock.vtt") as CogToolBlock;

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!