领胜LDS 键盘AOI检测项目
xcd
2020-07-10 ca718bf67886ef08cfd22599d22d458b610c02aa
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Base;
using Bro.Common.UI;
using Bro.Common.PubSub;
using static Bro.Common.Helper.EnumHelper;
using Bro.Common.Model;
using HalconDotNet;
using System.Threading;
using System.Collections.Specialized;
 
namespace Bro.Common.Calibration
{
    public partial class CalibrationDynamic9PointFrm : Form
    {
        //PubSubCenter PubSubCenter = PubSubCenter.GetInstance();
        AutoResetEvent _confirmHandle = new AutoResetEvent(false);
 
        public CalibrationDynamic9PointFrm()
        {
            InitializeComponent();
        }
 
        public CalibrationDynamic9PointFrm(CameraBase camera, PLCBase plc, CalibrationDynamic9PointConfig config)
        {
            InitializeComponent();
 
            Camera = camera;
            PLC = plc;
            Config = config as CalibrationDynamic9PointConfig;
        }
 
        public CalibrationDynamic9PointConfig Config { get; set; }
        public CameraBase Camera { get; set; }
        public PLCBase PLC { get; set; }
 
        public CalibrationDynamic9PointConfig GetConfig()
        {
            return null;
        }
 
        public void LoadConfig(CalibrationDynamic9PointConfig config)
        {
            FillResults();
 
            if (lvResults.Items.Count > 0)
            {
                lvResults.Items[0].Selected = true;
            }
 
            pgCameraConfig.SelectedObject = config.CameraOpConfig;
        }
 
        private void FillResults()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => FillResults()));
            }
            else
            {
                lvResults.Items.Clear();
                for (int i = 0; i < Config.Results.Count; i++)
                {
                    ListViewItem item = new ListViewItem(Config.Results[i].StepNum.ToString());
                    item.SubItems.Add(Config.Results[i].GetDisplayText());
                    item.Tag = Config.Results[i];
                    item.BackColor = Config.Results[i].IsDone ? Color.GreenYellow : Color.Transparent;
 
                    //if (Config.Results[i].Image != null)
                    //{
                    //    item.BackColor = Color.GreenYellow;
                    //}
                    //else
                    //{
                    //    item.BackColor = Color.Transparent;
                    //}
 
                    Config.Results[i].PropertyChanged -= CtrlCalib9PDynamic_PropertyChanged;
                    Config.Results[i].PropertyChanged += CtrlCalib9PDynamic_PropertyChanged;
 
                    lvResults.Items.Add(item);
                }
            }
        }
 
        private void CtrlCalib9PDynamic_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            FillResults();
        }
 
        //private void OnCalibPropertyChanged()
        //{
        //    if (this.InvokeRequired)
        //    {
        //        this.Invoke(new Action(() => OnCalibPropertyChanged()));
        //    }
        //    else
        //    {
        //        for (int i = 0; i < Config.Results.Count; i++)
        //        {
        //            if (Config.Results[i].Image != null)
        //            {
        //                lvResults.Items[i].BackColor = Color.GreenYellow;
        //            }
        //            else
        //            {
        //                lvResults.Items[i].BackColor = Color.Transparent;
        //            }
        //        }
        //    }
        //}
 
        Canvas _canvas = new Canvas();
        private void CtrlCalib9PDynamic_Load(object sender, EventArgs e)
        {
            _canvas.IsShowElementList = true;
            _canvas.Dock = DockStyle.Fill;
            plImage.Controls.Add(_canvas);
 
            RemoveHandles();
 
            if (Camera != null)
            {
                Camera.OnImageUpdated -= Camera_UpdateShowImage;
                Camera.OnImageUpdated += Camera_UpdateShowImage;
            }
 
            Config.Results.OnItemChanged = OnResultsListChanged;
            LoadConfig(Config);
        }
 
        private void OnResultsListChanged(NotifyCollectionChangedAction obj)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => FillResults()));
            }
            else
            {
                FillResults();
            }
        }
 
        public void RemoveHandles()
        {
            if (Camera != null)
            {
                Camera.OnImageUpdated -= Camera_UpdateShowImage;
            }
            //PubSubCenter.RemoveSubscriber(PubTag.CalibStepDone.ToString(), CalibStepDone);
            //PubSubCenter.RemoveSubscriber(PubTag.CalibAllDone.ToString(), CalibAllDone);
 
            chkContinueMode.Checked = false;
        }
 
        AutoResetEvent _imageShowedHandle = new AutoResetEvent(false);
        private void Camera_UpdateShowImage(CameraBase camera, Bitmap image)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<CameraBase, Bitmap>(Camera_UpdateShowImage), camera, image);
            }
            else
            {
                _canvas.LoadImage(image);
                _imageShowedHandle.Set();
            }
        }
 
        //private object CalibAllDone(ISubscriber arg1, object arg2, object arg3)
        //{
        //    string msg = arg3.ToString();
        //    tsslInfo.Text = msg;
 
        //    return null;
        //}
 
        //private object CalibStepDone(ISubscriber arg1, object arg2, object arg3)
        //{
        //    int index = Convert.ToInt32(arg2);
 
        //    _canvas.LoadImage(Config.Results[index].Image);
        //    _canvas.Elements.Clear();
        //    CrossHair ch = new CrossHair(Config.Results[index].PointResult);
        //    _canvas.Elements.Add(ch);
 
        //    tsslInfo.Text = $"步骤{index + 1}完成";
 
        //    if (chkManualConfirm.Checked)
        //    {
        //        this.Invoke(new Action(() => btnContinueCalib.Visible = true)); ;
        //        _confirmHandle.WaitOne();
        //    }
 
        //    return null;
        //}
 
        private void lvConfigs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvResults.SelectedItems.Count <= 0)
                return;
 
            var result = lvResults.SelectedItems[0].Tag as CalibrationResult;
 
            tsslStepHint.Text = $"{(result.StepNum)}/{Config.TotalSteps}";
 
            if (result.Image != null)
            {
                _canvas.LoadImage(result.Image);
                _canvas.Elements.Clear();
                _canvas.Elements.Add(new CrossHair(result.PointResult));
            }
        }
 
        private void btnStartCalib_Click(object sender, EventArgs e)
        {
            if (OnStartCalibration == null)
            {
                throw new ProcessException("StartCalibration不能为空");
            }
 
            OnStartCalibration(PLC, Config.TriggerAddress);
        }
 
        private void btnStepRun_Click(object sender, EventArgs e)
        {
            int stepNum = 1;
            if (lvResults.SelectedItems.Count > 0)
            {
                var step = lvResults.SelectedItems[0].Tag as CalibrationResult;
                stepNum = step.StepNum;
            }
 
            var config = pgCameraConfig.SelectedObject as CameraOprerationConfigBase;
 
            if (OnCalculateStep == null)
            {
                MessageBox.Show("单步运算函数为空");
                return;
            }
            try
            {
                CalibrationResult result = OnCalculateStep(Camera, PLC, Config, stepNum);
                ShowCalibStepDone(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetExceptionMessage());
            }
        }
 
        private void btnCalcuMatrix_Click(object sender, EventArgs e)
        {
            if (OnCalculateAll == null)
            {
                MessageBox.Show("标定结果运算函数为空");
                return;
            }
 
            try
            {
                string result = OnCalculateAll(Camera, Config);
                ShowCalibAllDone(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetExceptionMessage());
            }
        }
 
        private void btnSnap_Click(object sender, EventArgs e)
        {
            CameraOprerationConfigBase config = pgCameraConfig.SelectedObject as CameraOprerationConfigBase;
            Camera.UploadOperationConfig(config);
            Camera.Snapshot(config, out HObject hImage);
            hImage.Dispose();
        }
 
        private bool _isContinueMode = false;
        CameraOprerationConfigBase _selectedCameraOpConfig = null;
        private void chkContinueMode_CheckedChanged(object sender, EventArgs e)
        {
            _selectedCameraOpConfig = pgCameraConfig.SelectedObject as CameraOprerationConfigBase;
            _isContinueMode = chkContinueMode.Checked;
 
            if (_isContinueMode)
            {
                bool temp = _selectedCameraOpConfig.IsSaveOriginImage;
                _selectedCameraOpConfig.IsSaveOriginImage = false;
 
                Task.Run(() =>
                {
                    while (_isContinueMode)
                    {
                        try
                        {
                            Camera.UploadOperationConfig(_selectedCameraOpConfig);
                            Camera.Snapshot();
 
                            _imageShowedHandle.WaitOne(3000);
                        }
                        catch (Exception)
                        {
                        }
                    }
 
                    _selectedCameraOpConfig.IsSaveOriginImage = temp;
                });
            }
        }
 
        private void btnContinueCalib_Click(object sender, EventArgs e)
        {
            _confirmHandle.Set();
            btnContinueCalib.Visible = false;
        }
 
        private void CalibrationDynamic9PointFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            RemoveHandles();
        }
 
        #region 匿名委托
        //public virtual void StartCalibration()
        //{            
        //}
 
        //public virtual void CalibMarkPoint(CameraBase camera, CalibrationDynamic9PointConfig config, int stepIndex)
        //{
        //}
 
        //public virtual void CalculationMatrix(List<CalibrationDynamic9PointConfig> configs)
        //{
        //}
 
        public Action<PLCBase, int> OnStartCalibration { get; set; }
        public Func<CameraBase, PLCBase, CalibrationDynamic9PointConfig, int, CalibrationResult> OnCalculateStep { get; set; }
        public Func<CameraBase, CalibrationDynamic9PointConfig, string> OnCalculateAll { get; set; }
 
        #endregion
 
        #region UI显示
        private void ShowCalibStepDone(CalibrationResult result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<CalibrationResult>(ShowCalibStepDone), result);
            }
            else
            {
                //_canvas.LoadImage(result.Image);
                _canvas.Elements.Clear();
                CrossHair ch = new CrossHair(result.PointResult);
                _canvas.Elements.Add(ch);
 
                tsslInfo.Text = $"步骤{result.StepNum}完成,{result.PointResult.GetDisplayText()}";
 
                if (chkManualConfirm.Checked)
                {
                    btnContinueCalib.Visible = true;
                }
 
                if (chkManualConfirm.Checked)
                {
                    _confirmHandle.WaitOne();
                }
            }
        }
 
        private void ShowCalibAllDone(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ShowCalibAllDone), msg);
            }
            else
            {
                tsslInfo.Text = msg;
            }
        }
        #endregion
 
        private void btnClearResult_Click(object sender, EventArgs e)
        {
            Config.Results.ForEach(r =>
            {
                r.Image = null;
                r.PointResult = new CalibrationPoint();
                r.IsDone = false;
            });
        }
    }
}