领胜LDS 键盘AOI检测项目
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
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.UI.Model.Winform;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Bro.Common.Helper.EnumHelper;
 
namespace Bro.M071.Process.UI
{
    [MenuNode("M071_MainForm", "运行界面", 1, "M071Node", true)]
    public partial class M071_MainForm : MenuFrmBase
    {
        Canvas cvImage = new Canvas();
        M071Config Config => Process?.IConfig as M071Config;
        M071Process Process_M071 => Process as M071Process;
 
        public M071_MainForm()
        {
            InitializeComponent();
 
            cvImage.IsShowElementList = false;
            tsmiShowStatusBar.Checked = cvImage.IsShowStatusBar = false;
            tsmiShowToolBar.Checked = cvImage.IsShowToolBar = false;
            cvImage.Dock = DockStyle.Fill;
            plImage.Controls.Add(cvImage);
 
            tscEditLocation.Visible = tsmiShowEditor.Checked = false;
 
            this.Load += async (s, e) =>
               {
                   await Task.Delay(300);
 
                   cvImage.SetScreenSize();
                   cvImage.OnElementChangedHandle -= CvImage_OnElementChangedHandle;
                   cvImage.OnElementChangedHandle += CvImage_OnElementChangedHandle;
               };
        }
 
        public override void OnProcessUpdated()
        {
            base.OnProcessUpdated();
 
            if (Config == null)
                return;
 
            if (string.IsNullOrWhiteSpace(Config.BackgroundImagePath))
                return;
 
            try
            {
                this.Invoke(new Action(() =>
                   {
                       try
                       {
                           Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
                           cvImage.LoadImage(image);
                           cvImage.SetScreenSize();
                       }
                       catch (Exception ex)
                       {
                           LogAsync(new LogMsg(DateTime.Now, "载入背景图异常", ex.Message));
                           return;
                       }
 
                       cvImage.Elements.Clear();
                       lvMeasures.Items.Clear();
 
                       txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
 
                       Config.MeasurementUnitCollection.ForEach(u =>
                       {
                           if (!u.IsEnabled)
                               return;
 
                           var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                           cvImage.Elements.Add(ele);
 
                           ListViewItem item = new ListViewItem(u.GetDisplayText());
                           item.Tag = u.Id;
                           lvMeasures.Items.Add(item);
                       });
                   }));
            }
            catch (Exception ex)
            {
            }
 
            Process_M071.OnClearBarcode -= M071_MainForm_OnClearBarcode;
            Process_M071.OnClearBarcode += M071_MainForm_OnClearBarcode;
            Process_M071.OnUpdateResult -= M071_MainForm_UpdateResult;
            Process_M071.OnUpdateResult += M071_MainForm_UpdateResult;
            Process_M071.OnUpdateCT -= M071_MainForm_UpdateCT;
            Process_M071.OnUpdateCT += M071_MainForm_UpdateCT;
 
            Process_M071.OnMeasureStart -= M071_MainForm_MeasureStart;
            Process_M071.OnMeasureStart += M071_MainForm_MeasureStart;
 
            Process_M071.OnMachineStateChanged -= M071_MainForm_MachineStateChanged;
            Process_M071.OnMachineStateChanged += M071_MainForm_MachineStateChanged;
 
            Process_M071.OnFullResetDone -= Process_M071_FullResetDone;
            Process_M071.OnFullResetDone += Process_M071_FullResetDone;
 
            Config.PropertyChanged -= Config_PropertyChanged;
            Config.PropertyChanged += Config_PropertyChanged;
 
            Process_M071.OnElementUpdated -= Process_M071_OnElementUpdated;
            Process_M071.OnElementUpdated += Process_M071_OnElementUpdated;
        }
 
        private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
        }
 
        #region 图片区右键菜单
        private void tsmiShowToolBar_CheckedChanged(object sender, EventArgs e)
        {
            cvImage.IsShowToolBar = tsmiShowToolBar.Checked;
        }
 
        private void tsmiShowStatusBar_CheckedChanged(object sender, EventArgs e)
        {
            cvImage.IsShowStatusBar = tsmiShowStatusBar.Checked;
        }
 
        private void tsmiShowEditor_CheckedChanged(object sender, EventArgs e)
        {
            tscEditLocation.Visible = tsmiShowEditor.Checked;
            tscEditLocation.BringToFront();
        }
 
        private void tsmiRefreshLabels_Click(object sender, EventArgs e)
        {
            cvImage.Elements.Clear();
            lvMeasures.Items.Clear();
 
            Config.MeasurementUnitCollection.ForEach(u =>
            {
                if (!u.IsEnabled)
                    return;
 
                var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                cvImage.Elements.Add(ele);
 
                ListViewItem item = new ListViewItem(u.GetDisplayText());
                item.Tag = u.Id;
                lvMeasures.Items.Add(item);
            });
 
            this.Invalidate();
        }
        #endregion
 
        #region 标签编辑区
        private async void CvImage_OnElementChangedHandle(Common.Interface.IShapeElement ele)
        {
            this.Invoke(new Action(() =>
            {
                if (ele is KeyIndicator indicator)
                {
                    if (indicator.State == ElementState.Selected)
                    {
                        foreach (ListViewItem item in lvMeasures.Items)
                        {
                            if (item.Tag.ToString() == indicator.ID)
                            {
                                item.Selected = true;
 
                                lvMeasures.EnsureVisible(item.Index);
                                break;
                            }
                        }
                    }
                }
            }));
 
            await Task.Delay(100);
        }
 
        private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvMeasures.SelectedItems.Count <= 0)
                return;
 
            foreach (ListViewItem item in lvMeasures.Items)
            {
                item.BackColor = SystemColors.Control;
            }
 
            foreach (ListViewItem item in lvMeasures.SelectedItems)
            {
                item.BackColor = Color.Orange;
            }
 
            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
            propGridKeyIndicator.SelectedObject = ele;
        }
 
        private void btnCancelEdit_Click(object sender, EventArgs e)
        {
            cvImage.Elements.Clear();
            lvMeasures.Items.Clear();
            Config.MeasurementUnitCollection.ForEach(u =>
            {
                if (!u.IsEnabled)
                    return;
 
                var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                cvImage.Elements.Add(ele);
 
                ListViewItem item = new ListViewItem(u.GetDisplayText());
                item.Tag = u.Id;
                lvMeasures.Items.Add(item);
            });
 
            MessageBox.Show("取消标签修改");
        }
 
        private void btnConfirmEdit_Click(object sender, EventArgs e)
        {
            cvImage.Elements.ToList().ForEach(ele =>
            {
                var measure = Config.MeasurementUnitCollection.FirstOrDefault(u => u.Id == ele.ID);
                if (measure != null)
                {
                    measure.DisplayLocation = (ele as KeyIndicator).DisplayRect;
                }
            });
 
            MessageBox.Show("标签修改完成");
        }
 
        private void lvMeasures_DoubleClick(object sender, EventArgs e)
        {
            if (lvMeasures.SelectedItems.Count <= 0)
                return;
 
            cvImage.Elements.ToList().ForEach(u => u.State = ElementState.Normal);
            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
            if (ele != null)
            {
                ele.State = ElementState.Selected;
                //cvImage.Invalidate();
            }
        }
        #endregion
 
        #region 标签结果显示
        private async void Process_M071_OnElementUpdated(Common.Interface.IShapeElement obj)
        {
            if (obj is KeyIndicator keyIndicator)
            {
                this.Invoke(new Action(() =>
                {
                    var ele = cvImage.Elements.FirstOrDefault(u => u.ID == keyIndicator.ID);
 
                    (ele as KeyIndicator).Text = keyIndicator.Text;
                    (ele as KeyIndicator).ResultState = keyIndicator.ResultState;
 
                    this.Invalidate();
                }));
            }
 
            await Task.Delay(100);
        }
        #endregion
 
        #region 上方状态区
 
        #region 条码
        string _barcode = "";
        private void M071_MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            string keyStr = e.KeyCode.ToString();
            if (keyStr.Length == 1)
            {
                _barcode += keyStr.ToUpper();
            }
 
            if (e.KeyValue == 13)
            {
                txtBarcode.Text = _barcode;
                _barcode = "";
            }
 
            if (e.KeyValue == 27)
            {
                cvImage.Elements.ToList().ForEach(ele => ele.State = ElementState.Normal);
            }
        }
 
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            Process_M071.BarCode = txtBarcode.Text.Trim();
        }
 
        private void M071_MainForm_OnClearBarcode()
        {
            txtBarcode.BeginInvoke(new Action(() => txtBarcode.Clear()));
        }
        #endregion
 
        #region 机台状态
        private async void M071_MainForm_MachineStateChanged(MachineState state)
        {
            if (lblMachineState.IsHandleCreated)
            {
                lblMachineState.BeginInvoke(new Action(() =>
                {
                    switch (state)
                    {
                        case MachineState.Alarm:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Red;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White;
                            lblMachineState.Text = "警报";
 
                            btnStartMeasure.Text = "开始测量";
                            btnStartMeasure.Enabled = false;
                            break;
                        case MachineState.Pause:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Yellow;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black;
                            lblMachineState.Text = "暂停";
 
                            btnStartMeasure.Text = "继续测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Ready:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black;
                            lblMachineState.Text = "就绪";
                            
                            btnStartMeasure.Text = "开始测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Running:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Transparent;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.DarkGreen;
                            lblMachineState.Text = "运行";
 
                            btnStartMeasure.Text = "暂停测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Unknown:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = SystemColors.Control;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = SystemColors.ControlText;
                            lblMachineState.Text = "未知";
 
                            btnStartMeasure.Enabled = false;
                            break;
                    }
                }));
            }
 
            await Task.Delay(100);
        }
        #endregion
 
        #region 产品结果
        private async void M071_MainForm_UpdateCT(float ct)
        {
            lblCT.BeginInvoke(new Action(() =>
            {
                lblCT.Text = ct.ToString("f2") + " 秒";
            }));
            await Task.Delay(100);
        }
 
        private async void M071_MainForm_UpdateResult(DateTime arg1, int result)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (result == 1)
                {
                    lblProductionState.BackColor = Color.Green;
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "OK";
                }
                else
                {
                    lblProductionState.BackColor = Color.Red;
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "NG";
                }
 
                //btnStartMeasure.Text = "开始测量";
            }));
            await Task.Delay(100);
        }
 
        private async void M071_MainForm_MeasureStart()
        {
            this.BeginInvoke(new Action(() =>
            {
                lblProductionState.BackColor = SystemColors.Control;
                lblProductionState.ForeColor = SystemColors.ControlText;
                lblProductionState.Text = "测试中";
 
                lblCT.Text = "";
 
                cvImage.Elements.ToList().ForEach(e =>
                {
                    if (e is KeyIndicator indicator)
                    {
                        indicator.Text = "";
                        indicator.ResultState = null;
                    }
                });
            }));
            await Task.Delay(100);
        }
        #endregion
 
        #endregion
 
        private async void btnStartMeasure_Click(object sender, EventArgs e)
        {
            if (btnStartMeasure.Text == "开始测量")
            {
                await Task.Run(() =>
                {
                    try
                    {
                        Process_M071.StartJob(null, null, null);
                    }
                    catch (Exception ex)
                    {
                        LogAsync(DateTime.Now, "流程异常", ex.Message);
                        Process_M071.ExceptionRaisedInMonitor(ex);
                    }
                });
            }
            else if (btnStartMeasure.Text == "继续测量")
            {
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 10 } }, null, null);
            }
            else
            {
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 11 } }, null, null);
            }
        }
 
        #region 复位操作
        private async void Process_M071_FullResetDone()
        {
            btnStartMeasure.BeginInvoke(new Action(() =>
            {
                btnStartMeasure.Enabled = true;
                btnStartMeasure.Text = "开始测量";
            }));
            await Task.Delay(100);
        }
 
        private void btnReset_MouseDown(object sender, MouseEventArgs e)
        {
            if (Process_M071.MachineState != MachineState.Running)
            {
                Process_M071.Reset(null, null, null);
            }
 
            Process_M071.ResetTimer.Change(1000 * 5, -1);
        }
 
        private void btnReset_MouseUp(object sender, MouseEventArgs e)
        {
            Process_M071.ResetTimer.Change(-1, -1);
        }
        #endregion
 
    }
}