Gavin
2021-02-04 4e5aaefc7162b700b95c750caeff35e6323631d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using HalconDotNet;
 
 
namespace BroCVisionHalcon
{
    public class HDevEngineTool
    {
 
        #region 常量
 
        // path of external procedures
        string ProcedurePath = Environment.CurrentDirectory + "\\Vision\\";
 
        #endregion
 
        #region 成员变量
 
        /// <summary>
        /// 处理过程名
        /// </summary>
        public string ProcedureName;
 
        /// <summary>
        /// hdev程序启动引擎
        /// </summary>
        private HalconDotNet.HDevEngine MyEngine;
 
        /// <summary>
        /// 程序载入工具 .hdev
        /// </summary>
        private HDevProgramCall ProgramCall;
 
        /// <summary>
        /// 过程载入工具 .hdvp
        /// </summary>
        private HDevProcedureCall ProcedureCall;
 
        /// <summary>
        /// 控制参数字典
        /// </summary>
        public Dictionary<string, HTuple> TupleDictionary { get; set; }
 
        /// <summary>
        /// 图形参数字典
        /// </summary>
        public Dictionary<string, HObject> ImageDictionary { get; set; }
 
        #endregion
 
        #region 初始化
        /// <summary>
        /// 实例化 默认搜索路径为: 启动路径//Vision//
        /// </summary>
        public HDevEngineTool()
        {
            ProcedureName = "";
            MyEngine = new HalconDotNet.HDevEngine();
            MyEngine.SetProcedurePath(ProcedurePath);
 
            ImageDictionary = new Dictionary<string, HObject>();
            TupleDictionary = new Dictionary<string, HTuple>();
        }
 
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="path">外部函数搜索路径</param>
        public HDevEngineTool(string path)
        {
            MyEngine = new HalconDotNet.HDevEngine();
            MyEngine.SetProcedurePath(path);
 
            ImageDictionary = new Dictionary<string, HObject>();
            TupleDictionary = new Dictionary<string, HTuple>();
        }
        #endregion
 
        /// <summary>
        /// 设置函数运行所需参数
        /// </summary>
        /// <param name="_tupleDictionary">控制参数</param>
        /// <param name="_imageDictionary">图形参数</param>
        public void SetDictionary(Dictionary<string, HTuple> _tupleDictionary, Dictionary<string, HObject> _imageDictionary)
        {
            this.TupleDictionary = _tupleDictionary;
            this.ImageDictionary = _imageDictionary;
        }
 
        /// <summary>
        /// 载入过程 .hdvp
        /// </summary>
        /// <param name="procedureName">过程名</param>
        /// <param name="hwin"></param>
        //public void LoadProcedure(string procedureName,HDisplayControl.HDisplayControl hwin)
        //{
        //    ProcedureName = procedureName;
 
        //    MyEngine.SetHDevOperators(new HDevOpMultiWindowImpl(hwin.HalconWindow));
 
        //    try
        //    {
        //        HDevProcedure procedure = new HDevProcedure(procedureName);
        //        ProcedureCall = new HDevProcedureCall(procedure);
        //    }
        //    catch (HDevEngineException Ex)
        //    {
        //        Trace.TraceInformation("HDevProgram Load fail, ex:" + Ex.Message);
        //        return;
        //    }
        //}
 
        /// <summary>
        /// 载入过程 .hdvp
        /// </summary>
        /// <param name="procedureName">过程名</param>
        public void LoadProcedure(string procedureName)
        {
            ProcedureName = procedureName;
            try
            {
                HDevProcedure procedure = new HDevProcedure(procedureName);
                ProcedureCall = new HDevProcedureCall(procedure);
            }
            catch (HDevEngineException Ex)
            {
                Trace.TraceInformation("HDevProgram {0} Run fail ,Error Line : {1}, Line number: {2}, Halcon error number : {3}", Ex.ProcedureName, Ex.LineText, Ex.LineNumber, Ex.HalconError);
                return;
            }
        }
        
        /// <summary>
        /// 执行过程
        /// </summary>
        public void RunProcedure()
        {
            try
            {
 
                foreach (KeyValuePair<string, HTuple> pair in TupleDictionary)
                {
                    if(pair.Key.Contains("INPUT"))
                    {
                        ProcedureCall.SetInputCtrlParamTuple(pair.Key, pair.Value);
                    }
                }
 
                foreach(KeyValuePair<string,HObject>pair in ImageDictionary)
                {
                    if(pair.Key.Contains("INPUT"))
                    {
                        ProcedureCall.SetInputIconicParamObject(pair.Key, pair.Value);
                    }
                }
 
 
                ProcedureCall.Execute();
 
                string[] tupleKeys= TupleDictionary.Keys.ToArray<string>();
                string[] imageKeys = ImageDictionary.Keys.ToArray<string>();
 
                for (int i = 0; i < TupleDictionary.Count;i++ )
                {
                    if (tupleKeys[i].Contains("OUTPUT"))
                    {
                        TupleDictionary[tupleKeys[i]] = ProcedureCall.GetOutputCtrlParamTuple(tupleKeys[i]);
                    } 
                }
 
                for (int i = 0; i < ImageDictionary.Count;i++ )
                {
                    if (imageKeys[i].Contains("OUTPUTOBJ"))
                    {
                        ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamObject(imageKeys[i]);
                    }
                    if (imageKeys[i].Contains("OUTPUTIMG"))
                    {
                        ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamImage(imageKeys[i]);
                    }
                    if (imageKeys[i].Contains("OUTPUTREG"))
                    {
                        ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamRegion(imageKeys[i]);
                    }
                }
                
            }
            catch (HDevEngineException Ex)
            {
                Trace.TraceInformation("HDevProgram {0} Run fail ,Error Line : {1}, Line number: {2}, Halcon error number : {3}",Ex.ProcedureName,Ex.LineText,Ex.LineNumber,Ex.HalconError);
                return;
            }
        }
    }
}