领胜LDS 键盘AOI检测项目
patrick.xu
2021-04-28 96b6869bb20677f9b945f67c014a9b992ee05ac4
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
using Autofac;
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace Bro.M071.Process
{
    public class KeyLocation : IComplexDisplay
    {
        [Category("键名配置")]
        [Description("单键键名")]
        [TypeConverter(typeof(KeyNameDictConverter))]
        public string Key { get; set; }
 
        [Category("默认配置")]
        [Description("是否默认尺寸配置")]
        public bool IsDefault { get; set; } = false;
 
        //[Category("位置配置")]
        //[Description("单键在键盘平面图上的位置显示")]
        //public RectangleF KeyRect { get; set; } = new RectangleF(0, 0, 0, 0);
 
        [Category("位置配置")]
        [Description("单键左上角坐标")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
        public CustomizedPoint KeyPosition { get; set; } = new CustomizedPoint();
 
        [Category("位置配置")]
        [Description("单键尺寸")]
        public SizeF KeySize { get; set; } = new SizeF();
 
        [Category("位置配置")]
        [Description("单键之后间隙位置")]
        public SizeF IntervalSize { get; set; } = new SizeF();
 
        public string GetDisplayText()
        {
            return $"{(IsDefault ? "Default" : Key)}:{KeyPosition.X},{KeyPosition.Y},{KeySize.Width},{KeySize.Height}";
        }
    }
 
    public class KeyAlgorithem : IComplexDisplay
    {
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("单键算法配置")]
        [Description("算法名称")]
        public string Name { get; set; }
 
        [Category("单键算法配置")]
        [Description("算法路径")]
        [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
        public string AlgorithemPath { get; set; }
 
        public string GetDisplayText()
        {
            return $"{Name} -- {AlgorithemPath}";
        }
    }
 
    public class MeasureType : Spec
    {
        [Category("检测类型")]
        [Description("检测类型")]
        [DisplayName("检测类型")]
        public override string Code { get; set; }
 
        [Category("检测算法")]
        [Description("检测算法路径")]
        [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
        public string AlgorithemPath { get; set; }
 
        [Category("上传NG代码")]
        [Description("上传NG代码")]
        public string NGCode { get; set; }
 
        [Category("显示配置")]
        [Description("显示区域大小")]
        [DisplayName("标签大小")]
        public Size DisplayRect { get; set; }
    }
 
    public class KeyResult : IComplexDisplay
    {
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("单键结果配置")]
        [Description("结果配置名称")]
        public string Name { get; set; }
 
        [Category("单键结果配置")]
        [Description("结果配置集合")]
        [TypeConverter(typeof(SimpleCollectionConvert<string>))]
        [Editor(typeof(SimpleCollectionEditor<string>), typeof(UITypeEditor))]
        public List<string> Results { get; set; } = new List<string>();
 
        public string GetDisplayText()
        {
            return $"{Name} -- {(string.Join(" ", Results))}";
        }
    }
 
    public class SnapshotPoint : IComplexDisplay, IHalconToolPath
    {
        public string GetDisplayText()
        {
            return (IsEnabled ? "" : "禁用 ") + Name + " " + MotionOp.GetDisplayText() + " " + CameraOp.GetDisplayText();
        }
 
        public List<string> GetHalconToolPathList()
        {
            if (CameraOp.OpConfig is IHalconToolPath path)
            {
                return path.GetHalconToolPathList();
            }
            else
            {
                return new List<string>();
            }
        }
 
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("拍照点名称")]
        [Description("拍照点名称")]
        public string Name { get; set; }
 
        [Category("启用设置")]
        [Description("true:启用 false:禁用")]
        public bool IsEnabled { get; set; } = true;
 
        //[Category("设备配置")]
        //[Description("运动设备")]
        //[TypeConverter(typeof(DeviceSelectorConverter<IMotion>))]
        //public string MotionDevice { get; set; }
 
        //[Category("运动点位")]
        //[Description("运动点位")]
        //[TypeConverter(typeof(ComplexObjectConvert))]
        //[Editor(typeof(ComplexCollectionEditor<AxisInfo>), typeof(UITypeEditor))]
        //public List<AxisInfo> Destination { get; set; } = new List<AxisInfo>();
 
        [Category("运动机构配置")]
        [Description("运动机构配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(IOperationConfigByDeviceEditor), typeof(UITypeEditor))]
        public DeviceOpBind MotionOp { get; set; } = new DeviceOpBind();
 
        [Category("相机和操作配置")]
        [Description("相机和操作配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(IOperationConfigByDeviceEditor), typeof(UITypeEditor))]
        public DeviceOpBind CameraOp { get; set; } = new DeviceOpBind();
    }
 
    public class KeyUnit : IComplexDisplay, IHalconToolPath
    {
        #region 配置
        private string key = "";
        [Category("键名配置")]
        [Description("单键检测键名")]
        [TypeConverter(typeof(KeyNameDictConverter))]
        public string Key
        {
            get => key;
            set
            {
                AliasName = key = value;
            }
        }
 
        [Category("键名配置")]
        [Description("别名")]
        public string AliasName { get; set; }
 
        [Category("启用设置")]
        [Description("true:启用 false:禁用")]
        public bool IsEnabled { get; set; } = true;
 
        [Category("图像来源")]
        [Description("拍照点位")]
        [TypeConverter(typeof(SnapshotPointConverter))]
        public string SnapshotPointId { get; set; }
 
        [Category("图像来源")]
        [Description("拍照点位获取的图片序号,从1开始")]
        public int ImageSeq { get; set; } = 1;
 
        [Category("算法配置")]
        [Description("单键检测配置算法类型")]
        [TypeConverter(typeof(KeyAlgorithemConverter))]
        public string KeyAlgorithemId { get; set; }
 
        [Category("算法配置")]
        [Description("单键检测配置结果类型")]
        [TypeConverter(typeof(KeyResultConverter))]
        public string KeyResultId { get; set; }
        #endregion
 
        #region 检测相关字段
        public Dictionary<string, double> KeyValues = null;
        //public SnapshotPoint SnapshotPoint = null;
        public string KeyAlgorithemPath = "";
        public List<string> KeyResultList = null;
        #endregion
 
        public string GetDisplayText()
        {
            string snapShot = "未指定";
 
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                var config = scope.Resolve<IProcessConfig>();
                if (config != null)
                {
                    var snapshotPoint = (config as M071Config).SnapshotPointCollection.FirstOrDefault(u => u.Id == SnapshotPointId);
                    if (snapshotPoint != null)
                    {
                        snapShot = snapshotPoint.Name;
                    }
                }
            }
 
            return $"{AliasName}-{snapShot}-{ImageSeq}";
        }
 
        public List<string> GetHalconToolPathList()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                IProcessConfig iConfig = scope.Resolve<IProcessConfig>();
 
                if (iConfig is M071Config config)
                {
                    var algo = config.KeyAlgorithemCollection.FirstOrDefault(u => u.Id == KeyAlgorithemId);
                    if (algo != null)
                    {
                        return new List<string>() { algo.AlgorithemPath };
                    }
                }
            }
 
            return new List<string>();
        }
    }
 
    public class ProductionMeasurement : INotifyPropertyChanged, IDisposable
    {
        public string Barcode;
        public string PResult;
 
        public List<MeasurementUnit> Measurements = new List<MeasurementUnit>();
 
        public List<IShapeElement> ElementList = new List<IShapeElement>();
 
        public DateTime? StartTime = null;
 
        public DateTime? EndTime = null;
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void Dispose()
        {
            Measurements?.ForEach(m => m?.Dispose());
            Measurements = null;
            ElementList = null;
            //Barcode = null;
            GC.Collect();
        }
 
        public void InitialMeasurementsPropertyChanged()
        {
            Measurements.ForEach(m => m.PropertyChanged += M_PropertyChanged);
        }
 
        private object pChangedLock = new object();
        private void M_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            lock (pChangedLock)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Measurements"));
            }
        }
    }
 
    public class MeasurementUnit : IComplexDisplay, INotifyPropertyChanged, IDisposable
    {
        public string Id = Guid.NewGuid().ToString();
 
        #region 配置
        [Category("名称")]
        [Description("名称")]
        public string Name { get; set; } = "";
 
        [Category("启用设置")]
        [Description("true:启用 false:禁用")]
        public bool IsEnabled { get; set; } = true;
 
        [Category("算法配置")]
        [Description("检测和标准类型")]
        [TypeConverter(typeof(MeasureTypeConverter))]
        public string MeasureType { get; set; }
 
        [Category("算法配置")]
        [Description("涉及单键集合")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(ComplexCollectionEditor<KeyUnitBind>), typeof(UITypeEditor))]
        public List<KeyUnitBind> KeyUnitCollection { get; set; } = new List<KeyUnitBind>();
        #endregion
 
        #region 显示
        [Browsable(false)]
        public Rectangle DisplayLocation { get; set; } = new Rectangle(10, 10, 100, 100);
        #endregion
 
        #region 检测结果
        [Browsable(false)]
        public MeasureType Spec { get; set; } = null;
        #endregion
 
        public bool IsUpdated = false;
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void Dispose()
        {
            KeyUnitCollection?.ForEach(k => k?.Dispose());
            KeyUnitCollection = null;
        }
 
        public string GetDisplayText()
        {
            if (string.IsNullOrWhiteSpace(Name))
            {
                return (IsEnabled ? "" : "禁用 ") + string.Join("-", KeyUnitCollection.Select(u => u.GetDisplayText())) + "-" + MeasureType?.ToString();
            }
            else
            {
                return (IsEnabled ? "" : "禁用 ") + Name;
            }
        }
 
        public void InitialKeyUnitMeasureChanged()
        {
            KeyUnitCollection.ForEach(k => k.PropertyChanged += K_PropertyChanged);
        }
 
        private void K_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("KeyUnitCollection"));
        }
 
        public MeasurementUnit Copy()
        {
            MeasurementUnit mUnit = new MeasurementUnit();
 
            mUnit.Id = this.Id;
            mUnit.Name = this.Name;
            if (string.IsNullOrWhiteSpace(mUnit.Name))
            {
                mUnit.Name = string.Join("-", KeyUnitCollection.Select(u => u.GetDisplayText()));
            }
 
            mUnit.IsEnabled = this.IsEnabled;
            mUnit.MeasureType = this.MeasureType;
 
            mUnit.DisplayLocation = new Rectangle(this.DisplayLocation.Location, this.DisplayLocation.Size);
 
            if (this.Spec == null)
            {
                mUnit.Spec = null;
            }
            else
            {
                mUnit.Spec = new MeasureType();
                mUnit.Spec.Code = this.Spec.Code;
                mUnit.Spec.AlgorithemPath = this.Spec.AlgorithemPath;
                mUnit.Spec.StandardValue = this.Spec.StandardValue;
                mUnit.Spec.Tolrenance_Positive = this.Spec.Tolrenance_Positive;
                mUnit.Spec.Tolrenance_Negative = this.Spec.Tolrenance_Negative;
            }
 
            this.KeyUnitCollection.ForEach(k =>
            {
                mUnit.KeyUnitCollection.Add(k.Copy());
            });
 
            return mUnit;
        }
    }
 
    public class KeyUnitBind : IComplexDisplay, INotifyPropertyChanged, IDisposable
    {
        [Category("单键配置")]
        [Description("单键配置")]
        [TypeConverter(typeof(KeyNameDictConverter))]
        public string Key { get; set; }
 
        [Category("单键配置")]
        [Description("单键结果配置")]
        [TypeConverter(typeof(KeyUnitResultConverter))]
        public string KeyResultId { get; set; } = "";
 
 
        public List<HImage> KeyImages = new List<HImage>();
 
        public volatile int ImageSaveStatus = 0;
        //[Browsable(false)]
        //public NoticedDictionary<string, double?> MeasureValueDict { get; set; } = new NoticedDictionary<string, double?>();
 
        [Browsable(false)]
        public Dictionary<string, double?> MeasureValueDict { get; set; } = new Dictionary<string, double?>();
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        //public void InitialMeasureValueDictPropertyChanged()
        //{
        //    MeasureValueDict.PropertyChanged += MeasureValueDict_PropertyChanged;
        //}
 
        //private void MeasureValueDict_PropertyChanged(object sender, PropertyChangedEventArgs e)
        //{
        //    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MeasureValueDict"));
        //}
 
        public bool? IsDone = null;
 
        public string GetDisplayText()
        {
            string msg = Key;
 
            msg += string.IsNullOrWhiteSpace(KeyResultId) ? "" : $"-{KeyResultId}";
 
            return msg;
        }
 
        public void Dispose()
        {
            while (ImageSaveStatus != 0)
            {
                Thread.Sleep(10);
            }
 
            KeyImages?.ForEach(i =>
            {
                i?.Dispose();
                i = null;
            });
            KeyImages = null;
            MeasureValueDict = null;
        }
 
        object valueDictLock = new object();
        public void FillKeyValues(Dictionary<string, double> valuePairs)
        {
            lock (valueDictLock)
            {
                if (valuePairs == null || valuePairs.Count == 0)
                {
                    IsDone = false;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsDone"));
                    return;
                }
 
                foreach (KeyValuePair<string, double> pair in valuePairs)
                {
                    if (MeasureValueDict.ContainsKey(pair.Key))
                    {
                        MeasureValueDict[pair.Key] = pair.Value;
                    }
                }
 
                if (MeasureValueDict.Values.All(u => u != null))
                {
                    IsDone = true;
                }
 
                if (IsDone != null)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsDone"));
                }
            }
        }
 
        public KeyUnitBind Copy()
        {
            KeyUnitBind clone = new KeyUnitBind();
            clone.Key = this.Key;
            clone.KeyResultId = this.KeyResultId;
 
            this.MeasureValueDict.Keys.ToList().ForEach(k =>
            {
                clone.MeasureValueDict[k] = null;
            });
 
            return clone;
        }
    }
 
    public class KeyCodeMap : IComplexDisplay
    {
        [Category("键名映射配置")]
        [Description("键名")]
        [TypeConverter(typeof(KeyNameDictConverter))]
        public string Key { get; set; }
 
        [Category("键名映射配置")]
        [Description("上传对应键代码Code")]
        public string KeyCode { get; set; }
 
        public string GetDisplayText()
        {
            return $"{Key}--{KeyCode}";
        }
    }
 
    //public class NoticedDictionary<T1, T2> : Dictionary<T1, T2>, INotifyPropertyChanged
    //{
    //    public event PropertyChangedEventHandler PropertyChanged;
 
    //    public new T2 this[T1 index]
    //    {
    //        get => base[index];
    //        set
    //        {
    //            base[index] = value;
    //            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Index"));
    //        }
    //    }
    //}
}