C# 获取VisionPro的CogToolGroup下的所有层次的ICogTool生成工具树

1、加载模板

1 tplpath = Application.StartupPath + "\\xxx.vpp";
2             public Cognex.VisionPro.ToolGroup.CogToolGroup toolGroup= (Cognex.VisionPro.ToolGroup.CogToolGroup)Cognex.VisionPro.CogSerializer.LoadObjectFromFile(tplpath);
View Code

2、创建根目录

 1 /// <summary>
 2 /// 加载相机模板
 3 /// </summary>
 4  private void LoadCogToolGroup(CogToolGroup tg)
 5  {
 6    toolGroup = tg;
 7    if (toolGroup.Tools.Count > 0)
 8     {
 9         TreeNode node=new TreeNode("相机模板");
10         trvTpl.Nodes.Add(node);
11         trvTpl.ExpandAll();
12 
13         foreach (ICogTool icCogTool in toolGroup.Tools)
14         {
15             GetToolBlockTools(icCogTool, node);
16         }
17      }
18  }
View Code

3、递归查找

 1  private void GetToolBlockTools(ICogTool icCogTool,TreeNode tnode)
 2         {
 3             if (icCogTool.GetType().Name == "CogToolBlock")
 4             {
 5                 CogToolBlock c = (CogToolBlock)icCogTool;
 6                 if (icCogTool.Name==tnode.Text)
 7                 {
 8                     TreeNode[] tds = trvTpl.Nodes.Find(icCogTool.Name, true);
 9                     if(tds.Length>0)
10                         GetToolBlockTools(icCogTool, tds[0]);
11                 }
12                 else
13                 {
14                     TreeNode node = new TreeNode(icCogTool.Name);
15                     node.Tag = icCogTool;
16                     tnode.Nodes.Add(node);
17                     foreach (ICogTool ict in c.Tools)
18                     {
19                         GetToolBlockTools(ict, node);
20                     }
21                 }
22             }
23             else
24             {
25                 TreeNode node = new TreeNode(icCogTool.Name);
26                 node.Tag = icCogTool;
27                 tnode.Nodes.Add(node);
28             }
29         }
View Code

 

posted on 2019-07-11 14:56  BennyHua  阅读(936)  评论(0编辑  收藏  举报

导航