领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
using Bro.Common.Base;
using Bro.Common.Base.UI;
using Bro.Common.Factory;
using Bro.Common.Interface;
using Bro.Common.Model;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Bro.Process
{
    public partial class FrmCalibration_9P : Form
    {
        public FrmCalibration_9P()
        {
            InitializeComponent();
        }
 
        public FrmCalibration_9P(CameraBase camera,
                IMotion motionDevice,
                CalibrationConfig config,
                Action<CameraBase, CalibrationConfig, string, int> singleResultAction,
                Func<CameraBase, IMotion, CalibrationConfig, AutoWaitConfirm, string> finalCalculation,
                Action<IOperationConfig> resetHalconToolAction,
                string methodName)
        {
            InitializeComponent();
 
            this.camera = camera;
            this.motionDevice = motionDevice;
            Config = config;
            this.singleResultAction = singleResultAction;
            this.finalCalculation = finalCalculation;
            this.resetHalconToolAction = resetHalconToolAction;
            this.methodName = methodName;
 
            this.Text += $" -- {methodName}";
        }
 
        public CalibrationConfig Config { get; set; }
 
        private Action<CameraBase, CalibrationConfig, string, int> singleResultAction;
        private Func<CameraBase, IMotion, CalibrationConfig, AutoWaitConfirm, string> finalCalculation;
        private Action<IOperationConfig> resetHalconToolAction;
 
        string methodName = "";
        CtrlCameraRunBase runCtrl = null;
        CameraBase camera = null;
        IMotion motionDevice = null;
 
        AutoResetEvent _confirmHandle = new AutoResetEvent(false);
 
        public void LoadConfig()
        {
            lvResults.Items.Clear();
 
            for (int i = 0; i < Config.Results.Count; i++)
            {
                ListViewItem item = new ListViewItem((i + 1).ToString());
                item.SubItems.Add(Config.Results[i].GetDisplayText());
 
                lvResults.Items.Add(item);
 
                Config.Results[i].PropertyChanged -= CtrlCalib9PDynamic_PropertyChanged;
                Config.Results[i].PropertyChanged += CtrlCalib9PDynamic_PropertyChanged;
            }
 
            if (lvResults.Items.Count > 0)
            {
                lvResults.Items[0].Selected = true;
            }
 
            propGridConfig.SelectedObject = Config.CameraOpConfig;
        }
 
        private void CtrlCalib9PDynamic_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnCalibPropertyChanged();
        }
 
        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].ImageMarkPoint.X != 0 || Config.Results[i].ImageMarkPoint.Y != 0)
                    {
                        lvResults.Items[i].BackColor = Color.GreenYellow;
                    }
                    else
                    {
                        lvResults.Items[i].BackColor = Color.Transparent;
                    }
                }
            }
        }
 
        private void FrmCalib9PDynamic_Load(object sender, EventArgs e)
        {
            runCtrl = UIFactory.GetRunCtrl(camera) as CtrlCameraRunBase;
            if (runCtrl is UserControl uc)
            {
                uc.Dock = DockStyle.Fill;
                plRunCtrl.Controls.Add(uc);
            }
 
            LoadConfig();
        }
 
        /// <summary>
        /// 标定结束时动作
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public void OnCalibFinalDone(string msg, AutoWaitConfirm saveConfigConfirm)
        {
            tsslInfo.Text = msg;
 
            saveConfigConfirm.WaitResult = MessageBox.Show("选择Yes将标定结果写入配置,选择No不修改当前配置\r\n该操作不保存当前配置,需要保存配置请退出标定界面后手动保存", "写入配置提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
            saveConfigConfirm.WaitHandle.Set();
        }
 
        public void OnCalibStepDone(int sequence)
        {
            tsslInfo.Text = $"步骤{sequence}完成,{Config.Results[sequence - 1].GetDisplayText()}";
            ;
            if (chkManualConfirm.Checked)
            {
                this.Invoke(new Action(() => btnContinueCalib.Visible = true)); ;
                _confirmHandle.WaitOne();
            }
        }
 
        int _selectedStepIndex = -1;
 
        private void lvResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvResults.SelectedItems.Count <= 0)
                return;
 
            _selectedStepIndex = lvResults.SelectedIndices[0];
        }
 
        private void lvResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvResults.SelectedItems.Count <= 0)
                return;
 
            _selectedStepIndex = lvResults.SelectedIndices[0];
            var result = Config.Results[_selectedStepIndex];
 
            if (result.Image != null)
            {
                runCtrl.CvImage.LoadImage(result.Image);
                runCtrl.CvImage.Elements.Clear();
                runCtrl.CvImage.Elements.Add(new PointIndicator() { Center = new Common.Model.CustomizedPoint(result.ImageMarkPoint) });
            }
        }
 
        private void btnStartCalib_Click(object sender, EventArgs e)
        {
            //if (chkOfflineCalib.Checked)
            //{
            //    ProcessControl.Calibration_Pick_9P_Dynamic_Offline(Config.Configs);
            //}
            //else
            //{
            //    ProcessControl.SendCalibStartSignal(Config.TriggerAddress);
            //}
        }
 
        private void btnLoadOfflineImages_Click(object sender, EventArgs e)
        {
            Config.Results.ForEach(c =>
            {
                if (!string.IsNullOrWhiteSpace(c.OfflineImagePath))
                {
                    c.Image = (Bitmap)Image.FromFile(c.OfflineImagePath);
                }
            });
 
            tsslInfo.Text = "离线图片载入完成";
        }
 
        private void btnStepRun_Click(object sender, EventArgs e)
        {
            //CalibrationConfig config = propGridConfig.SelectedObject as CalibrationConfig;
            //if (!chkOfflineRun.Checked)
            //{
            //    ProcessControl.CalibMarkPoint(Camera, config, _selectedStepIndex + 1);
            //}
            //else
            //{
            //    ProcessControl.CalibMarkPoint(_canvas.MAP, config, _selectedStepIndex + 1);
            //}
 
            //tsslInfo.Text = $"单步运算完成。标记点坐标:{config.ImageMarkPoint.X},{config.ImageMarkPoint.Y}";
 
            try
            {
                if (singleResultAction != null)
                {
                    singleResultAction(camera, Config, methodName, _selectedStepIndex + 1);
                    OnCalibStepDone(_selectedStepIndex + 1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("标定过程异常\r\n" + ex.Message);
            }
        }
 
        private void btnCalcuMatrix_Click(object sender, EventArgs e)
        {
            try
            {
                if (finalCalculation != null)
                {
                    var saveConfigConfirm = new AutoWaitConfirm() { WaitHandle = new AutoResetEvent(false), WaitResult = false };
                    OnCalibFinalDone(finalCalculation(camera, motionDevice, Config, saveConfigConfirm), saveConfigConfirm);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("标定结果计算异常\r\n" + ex.Message);
            }
        }
 
        private void btnSnap_Click(object sender, EventArgs e)
        {
            camera.UploadOperationConfig(Config.CameraOpConfig);
            camera.Snapshot();
        }
 
        //private void chkContinueMode_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkContinueMode.Checked)
        //    {
        //        Task.Run(() =>
        //        {
        //            HalconRelatedCameraOprerationConfigBase config = (propGridConfig.SelectedObject as CalibrationConfig).CameraOpConfig;
        //            bool temp = config.IsSaveImage;
        //            config.IsSaveImage = false;
 
        //            while (chkContinueMode.Checked)
        //            {
        //                try
        //                {
        //                    Camera.UploadOperationConfig(config);
        //                    Camera.Snapshot();
 
        //                    _imageShowedHandle.WaitOne(3000);
        //                }
        //                catch (Exception)
        //                {
        //                }
        //            }
 
        //            config.IsSaveImage = temp;
        //        });
        //    }
        //}
 
        private void btnContinueCalib_Click(object sender, EventArgs e)
        {
            _confirmHandle.Set();
            btnContinueCalib.Visible = false;
        }
 
        private void btnReloadAlgorithem_Click(object sender, EventArgs e)
        {
            resetHalconToolAction(Config.CameraOpConfig);
        }
    }
}