领胜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
using Autofac;
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.M071.Process
{
    public partial class M071Process
    {
        [ProcessMethod("OfflineTest", "OfflineTest", "离线测试", InvokeType.TestInvoke)]
        public ProcessResponse OfflineTest(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
        {
            if (opConfig is OfflineTestOpConfig config)
            {
                if (config.SnapshotConfigs.Count > 0)
                {
                    OnMeasureStart?.Invoke();
 
                    List<MeasurementUnit> measurements = new List<MeasurementUnit>();
                    Config.MeasurementUnitCollection.Where(u => u.IsEnabled).ToList().ForEach(u =>
                    {
                        var m = u.Copy();
                        m.InitialKeyUnitMeasureChanged();
                        measurements.Add(m);
                    });
 
                    var pMeasure = new ProductionMeasurement()
                    {
                        Barcode = $"OfflineTest_{DateTime.Now.ToString("HHmmss")}",
                        Measurements = measurements,
                        StartTime = DateTime.Now,
                    };
 
                    lock (productionLock)
                    {
                        var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
                        if (existedProduction != null)
                        {
                            productionList.Remove(existedProduction);
                            existedProduction.Dispose();
                            existedProduction = null;
                        }
 
                        productionList.Add(pMeasure);
                    }
 
                    pMeasure.InitialMeasurementsPropertyChanged();
                    pMeasure.PropertyChanged += MeasureProduction_PropertyChanged;
 
                    config.SnapshotConfigs.ForEach(s =>
                    {
                        ImageSet set = new ImageSet();
                        set.HImage = new HalconDotNet.HImage(s.OfflineImageFilePath);
                        set.ImageData = JsonConvert.SerializeObject(config.ScanParam);
 
                        var snapshotConfig = Config.SnapshotPointCollection.FirstOrDefault(u => u.Id == s.SnapshotPointId);
 
                        RunImageHandle(snapshotConfig.CameraOp.OpConfig, set, snapshotConfig.Id, snapshotConfig.Name, pMeasure.Measurements);
                    });
                }
 
            }
 
            return new ProcessResponse(true);
        }
    }
 
    [Device("OfflineTest", "离线测试操作配置", EnumHelper.DeviceAttributeType.OperationConfig)]
    public class OfflineTestOpConfig : OperationConfigBase
    {
        [Category("取像设置")]
        [Description("取像设置")]
        [TypeConverter(typeof(CollectionCountConvert))]
        [Editor(typeof(ComplexCollectionEditor<OfflineSnapshotPoint>), typeof(UITypeEditor))]
        public List<OfflineSnapshotPoint> SnapshotConfigs { get; set; } = new List<OfflineSnapshotPoint>();
 
        //[Category("离线图像目录")]
        //[Description("离线图片文件目录,目前只支持一级文件目录,该目录包含且仅包含一次完整测试需要的图片")]
        //[Editor(typeof(FoldDialogEditor),typeof(UITypeEditor))]
        //public string OfflineImageFolder { get; set; }
 
        [Category("扫描图像参数")]
        [Description("扫描图像参数")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public LaserScanParam ScanParam { get; set; } = new LaserScanParam();
    }
 
    public class OfflineSnapshotPoint : IComplexDisplay
    {
        [Category("取像点位及配置")]
        [TypeConverter(typeof(SnapshotPointConverter))]
        public string SnapshotPointId { get; set; }
 
        [Category("离线图像文件")]
        [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
        public string OfflineImageFilePath { get; set; }
 
        public string GetDisplayText()
        {
            string snapshotPointName = "";
 
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                var config = scope.Resolve<IProcessConfig>() as M071Config;
 
                if (config != null)
                {
                    snapshotPointName = config.SnapshotPointCollection.FirstOrDefault(u => u.Id == SnapshotPointId)?.Name;
                }
            }
 
            if (string.IsNullOrWhiteSpace(snapshotPointName))
            {
                snapshotPointName = "未指定";
            }
 
            return snapshotPointName;
        }
    }
}