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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using Autofac;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection.Metadata.Ecma335;
 
namespace Bro.M141.Process
{
    public class CheckPoint : IComplexDisplay
    {
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("检测点位")]
        [Description("点位名称")]
        [DisplayName("点位名称")]
        public string Name { get; set; } = "";
 
        [Category("检测点位")]
        [Description("点位坐标,检测点位的运动坐标")]
        [DisplayName("点位坐标")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public CustomizedPoint PointPosition { get; set; } = new CustomizedPoint();
 
        [Category("检测点位")]
        [Description("点位坐标,检测点位的Z轴坐标")]
        [DisplayName("点位坐标Z")]
        public double PointZ { get; set; }
 
        [Category("标定设置")]
        [Description("该检测点位的标定矩阵,图像UV坐标转换为大板坐标系XY的标定矩阵")]
        [DisplayName("点位标定矩阵")]
        [TypeConverter(typeof(SimpleCollectionConvert<double>))]
        [Editor(typeof(SimpleCollectionEditor<double>), typeof(UITypeEditor))]
        public List<double> Matrix { get; set; } = new List<double>();
 
        [Category("启用配置")]
        [Description("true:启用该点位 false:禁用该点位")]
        [DisplayName("点位启用标志")]
        public bool IsEnabled { get; set; } = true;
 
        public string GetDisplayText()
        {
            return $"{(IsEnabled ? "" : "禁用 ")}{Name} {PointPosition.GetDisplayText()}";
        }
    }
 
    public enum ContourEdge
    {
        X,
        Y,
        Z
    }
    public class ContourPoint : IComplexDisplay, IImportFromFileInEditor
    {
        [Category("点位配置")]
        [Description("点位名称")]
        [DisplayName("点位名称")]
        public string Name { get; set; } = "";
 
        [Category("点位数据")]
        [Description("X数据")]
        [DisplayName("X数据")]
        public double X { get; set; } = 0;
 
        [Category("点位数据")]
        [Description("Y数据")]
        [DisplayName("Y数据")]
        public double Y { get; set; } = 0;
 
        [Browsable(false)]
        public double ActualZ { get; set; } = -999;
 
 
        [Category("点位配置")]
        [Description("算法输出点位索引,从0开始")]
        [DisplayName("索引")]
        public int index { get; set; } = 0;
 
 
 
        [Category("补偿值设置")]
        [Description("Z补偿值")]
        [DisplayName("Z补偿值")]
        public double compensateZ { get; set; } = 0;
 
 
 
        public ContourPoint Copy()
        {
            ContourPoint point = new ContourPoint();
 
            point.Name = Name;
            point.X = X;
            point.Y = Y;
            point.ActualZ = -999;
            point.compensateZ = compensateZ;
            return point;
        }
 
        [Browsable(false)]
        public double Error { get; set; } = 0;
 
        public string GetDisplayText()
        {
            return $"{Name} 序号{index} 数据{X},{Y}";
        }
 
 
        public IImportFromFileInEditor GetImportObject(string data, out string msg)
        {
            msg = "";
            var dataList = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (dataList.Length < 5)
            {
                msg = $"每行至少包含5段内容:点位名称,索引,X,Y,Z补偿值    {data}";
                throw new Exception(msg);
            }
            ContourPoint ret = new ContourPoint();
            try
            {
                ret.Name = dataList[0];
                ret.index = int.Parse(dataList[1]);
                ret.X = double.Parse(dataList[2]);
                ret.Y = double.Parse(dataList[3]);
                ret.compensateZ = double.Parse(dataList[4]);
            }
            catch
            {
                msg = $"数据内容:点位名称,索引,X,Y,Z补偿值    {data}";
                throw new Exception(msg + "数据类型异常");
            }
 
            return ret;
        }
 
        public bool ICSVOutput(object o)
        {
            try
            {
                if (o is List<ContourPoint> Pl)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; // 设置文件过滤器,只显示CSV文件
                    saveFileDialog.Title = "Save CSV File"; // 设置对话框标题
                    saveFileDialog.FileName = "ContourPoints"; // 默认文件名
                    saveFileDialog.DefaultExt = "csv"; // 默认文件扩展名
                    string filePath = "";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        filePath = saveFileDialog.FileName; // 获取用户选择的文件路径
                    }
                    else
                    {
                        return false;
                    }
 
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        // 写入标题行
                        writer.WriteLine("点位名称,索引,X,Y,Z补偿值");
                        // 写入数据行
                        foreach (var row in Pl)
                        {
                            writer.WriteLine($"{row.Name},{row.index},{row.X},{row.Y},{row.compensateZ}");
                        }
                    }
                }
            }
            catch
            {
 
            }
            return true;
        }
 
 
 
 
 
 
 
    }
 
    public class MeasureItemBind : IComplexDisplay, IHalconToolPath, IImportFromFileInEditor
    {
        [Category("检测标准")]
        [DisplayName("\t检测标准")]
        [Description("检测标准选择")]
        [TypeConverter(typeof(SpecCodeSelectorConverter))]
        public string SpecCode { get; set; }
 
        [Category("算法配置")]
        [DisplayName("算法路径")]
        [Description("算法路径")]
        [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
        public string AlgorithemPath { get; set; }
 
        [Category("检测点位")]
        [DisplayName("检测点位集合")]
        [Description("该检测项所涉及的所有检测点位集合")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(ComplexCollectionEditor<MeasurePointSelector>), typeof(UITypeEditor))]
 
 
 
        public List<MeasurePointSelector> MeasurePointNameCollection { get; set; } = new List<MeasurePointSelector>();
 
 
        public IImportFromFileInEditor GetImportObject(string data, out string msg)
        {
            msg = "";
            var dataList = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (dataList.Count < 3)
            {
                msg = $"每行至少包含3段内容:检测标准,检测点位,轮廓    {data}";
                throw new Exception(msg);
            }
            MeasureItemBind ret = new MeasureItemBind();
            try
            {
                using (var scope = GlobalVar.Container.BeginLifetimeScope())
                {
                    var config = scope.Resolve<IProcessConfig>();
                    if (config is M141Config iConfig)
                    {
                        if (iConfig.MeasureItemBinds.Any(u => u.SpecCode == dataList[0]))
                        {
                            ret = iConfig.MeasureItemBinds.FirstOrDefault(u => u.SpecCode == dataList[0]);
 
                            MeasurePointSelector mes = new MeasurePointSelector();
                            mes.MeasurePointName = dataList[1];
 
                            if (dataList[2].ToUpper() == "X")
                            {
                                mes.ContourEdge = ContourEdge.X;
                            }
                            else if (dataList[2].ToUpper() == "Y")
                            {
                                mes.ContourEdge = ContourEdge.Y;
                            }
                            else if (dataList[2].ToUpper() == "Z")
                            {
                                mes.ContourEdge = ContourEdge.Z;
                            }
                            else
                            {
                                throw new Exception("检测标准,检测点位,轮廓   " + data + " 轮廓选择异常");
                            }
                            ret.MeasurePointNameCollection.Add(mes);
 
                            iConfig.MeasureItemBinds.Remove(ret);
                        }
                        else
                        {
                            if (!iConfig.GetSpecList().Any(u => u.Code == dataList[0]))
                            {
                                throw new Exception("检测标准,检测点位,轮廓   " + data + "不存在于点位集合中,不可录入");
                            }
 
                            if (!iConfig.MeasurePointCollection.Any(u => u.Name == dataList[1]))
                            {
                                throw new Exception("检测标准,检测点位,轮廓   " + data + "不存在于点位集合中,不可录入");
                            }
 
                            MeasurePointSelector mes = new MeasurePointSelector();
                            ret.SpecCode = dataList[0];
                            mes.MeasurePointName = dataList[1];
 
                            if (dataList[2].ToUpper() == "X")
                            {
                                mes.ContourEdge = ContourEdge.X;
                            }
                            else if (dataList[2].ToUpper() == "Y")
                            {
                                mes.ContourEdge = ContourEdge.Y;
                            }
                            else if (dataList[2].ToUpper() == "Z")
                            {
                                mes.ContourEdge = ContourEdge.Z;
                            }
                            else
                            {
                                throw new Exception("检测标准,检测点位,轮廓   " + data + " 轮廓选择异常");
                            }
                            ret.MeasurePointNameCollection.Add(mes);
 
                        }
                    }
                }
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ret;
        }
 
        public bool ICSVOutput(object o)
        {
            try
            {
                if (o is List<MeasureItemBind> Pl)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; // 设置文件过滤器,只显示CSV文件
                    saveFileDialog.Title = "Save CSV File"; // 设置对话框标题
                    saveFileDialog.FileName = "MeasureItemBinds"; // 默认文件名
                    saveFileDialog.DefaultExt = "csv"; // 默认文件扩展名
                    string filePath = "";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        filePath = saveFileDialog.FileName; // 获取用户选择的文件路径
                    }
                    else
                    {
                        return false;
                    }
 
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        // 写入标题行
                        writer.WriteLine("检测标准,检测点位,轮廓");
                        // 写入数据行
                        foreach (var row in Pl)
                        {
                            foreach (var item in row.MeasurePointNameCollection)
                            {
                                string temC = "X";
                                if (item.ContourEdge == ContourEdge.X)
                                {
                                    temC = "X";
                                }
                                else if (item.ContourEdge == ContourEdge.Y)
                                {
                                    temC = "Y";
                                }
                                else if (item.ContourEdge == ContourEdge.Z)
                                {
                                    temC = "Z";
                                }
                                writer.WriteLine($"{row.SpecCode},{item.MeasurePointName},{temC}");
                            }
                        }
                    }
                }
            }
            catch
            {
 
            }
            return true;
        }
 
        public string GetDisplayText()
        {
            return $"{SpecCode} {string.Join(",", MeasurePointNameCollection.Select(u => u.GetDisplayText()))} {AlgorithemPath}";
        }
 
        public List<string> GetHalconToolPathList()
        {
            return new List<string>() { AlgorithemPath };
        }
    }
 
    public class MeasurePointSelector : IComplexDisplay
    {
        [Category("检测点位")]
        [Description("检测点位")]
        [DisplayName("检测点位")]
        [TypeConverter(typeof(MeasurePointNameSelectorConvertor))]
        public string MeasurePointName { get; set; } = "";
 
        [Category("检测点位")]
        [Description("轮廓选择。选择参与计算的数据")]
        [DisplayName("轮廓选择")]
        public ContourEdge ContourEdge { get; set; } = ContourEdge.Z;
 
 
        public string GetDisplayText()
        {
            return $"{MeasurePointName}";
        }
    }
 
    public class MeasurePointNameSelectorConvertor : ComboBoxItemTypeConvert
    {
        public override Dictionary<string, string> GetConvertDict(ITypeDescriptorContext context)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
 
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                var config = scope.Resolve<IProcessConfig>();
                if (config is M141Config iConfig)
                {
                    iConfig.MeasurePointCollection.ForEach(u =>
                    {
                        dict[u.Name] = u.Name;
                    });
                }
            }
            dict[""] = "未指定";
 
            return dict;
        }
    }
}