FrmAOICheck.cs
11.0 KB
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
using AccAOI.camera;
using AOI;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using System.Linq;
namespace TSA_V
{
public partial class FrmAOICheck : FrmBase
{
private List<ResultBean> ResultList = new List<ResultBean>();
private AOI.AoiProject CurrProject = null;
private DateTime showTime = DateTime.Now;
private DateTime endTime = DateTime.Now;
private bool IsCheck = false;
/// <summary>
/// 检测结果,0=未知,1=OK,2=NG
/// </summary>
private int CheckResult =0;
private int waitSeconds = 1;
public FrmAOICheck(AoiProject currAoi )
{
InitializeComponent();
CurrProject = currAoi;
}
private void FrmCamera_Load(object sender, EventArgs e)
{
FormStatus(false);
timer1.Start();
lblCurrWorkTime.Visible = true;
//默认显示程序配置的 图片
if (CurrProject == null)
{
this.Close();
return;
}
lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动检测", "{0}秒后自动检测", waitSeconds.ToString());
// this.picResult.Image = CurrProject.standardImage;
}
private void FormStatus(bool open)
{
}
private bool isAutoClose = false;
private void UpdateTextResult(Label label, int result)
{
if (result.Equals(0))
{
label.ForeColor = Color.Orange;
label.Text = "?";
}
else if (result.Equals(1))
{
label.Text = "OK";
label.ForeColor = Color.Green;
}
else if (result.Equals(2))
{
label.Text = "NG";
label.ForeColor = Color.Red;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
btnRun.Visible = !IsCheck;
TimeSpan span = DateTime.Now - showTime;
if (!IsCheck)
{
int times = waitSeconds - (int)(span.TotalSeconds);
if (times > 0)
{
if (times <= 1)
{
lblCurrWorkTime.Text = ResourceCulture.GetString("开始自动检测", "开始自动检测") + "......";
}else
{
lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动检测", "{0}秒后自动检测", times.ToString());
}
lblCurrWorkTime.Visible = true;
}
else
{
lblCurrWorkTime.Text = ResourceCulture.GetString("开始自动检测", "开始自动检测");
lblCurrWorkTime.Visible = true;
CheckResult = AoiCheck();
MesUtil.SetAOIResult(CheckResult);
UpdateTextResult(lblResult, CheckResult);
if (CheckResult.Equals(1))
{
isAutoClose = true;
}
else
{
isAutoClose = false;
}
}
}
else
{
lblCurrWorkTime.Text = ResourceCulture.GetString("请点击完成按钮结束检测", "请点击完成按钮结束检测", 0);
lblCurrWorkTime.Visible = true;
if (isAutoClose)
{
TimeSpan closeSpan = DateTime.Now - endTime;
int times = 3 - (int)(closeSpan.TotalSeconds);
if (times < 0)
{
lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动完成", "{0}秒后自动完成", 0);
lblCurrWorkTime.Visible = true;
this.Close();
}
else
{
lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动完成", "{0}秒后自动完成", times.ToString());
lblCurrWorkTime.Visible = true;
}
}
}
}
private int AoiCheck()
{
GC.Collect(0, GCCollectionMode.Forced);
//GC.Collect();
IsCheck = true;
btnRun.Visible = false;
lblError.Visible = false;
lblInfo.Visible = false;
int result = 2;
try
{
if (CurrProject == null)
{
return 0;
}
chbAllShow.Enabled = false;
string camera = ConfigAppSettings.GetValue(Setting_Init.CameraName);
camera = AccAOI.camera.CameraManager.hikNameList.ToArray().FirstOrDefault();
Image currImage = CameraManager.GetCamerImage(camera);
if (currImage != null)
{
//Bitmap bitmap = new Bitmap(currImage);
Image outImage = null;
List<ResultBean> resultBean = CurrProject.CheckAll(currImage, out outImage);
currImage.Dispose();
ResultList = resultBean;
if (outImage != null)
{
//this.picResult.SelectNone();
//picResult.SizeMode = Acc.ImageBox.ImageBoxSizeMode.Fit;
this.picResult.Image = outImage;
//picResult.SizeMode = Acc.ImageBox.ImageBoxSizeMode.Normal;
// pictureBox1.Image = outImage;
}
result = 1;
endTime = DateTime.Now;
string ngList = "NG:\r\n";
listErrorView.Items.Clear();
foreach (ResultBean bean in resultBean)
{
if (bean.result.Equals(false))
{
ngList += bean.MethodName + "\r\n";
result = 2;
// break;
listErrorView.Items.Add(GetShowName(bean));
}else if (chbAllShow.Checked)
{
listErrorView.Items.Add(GetShowName(bean));
}
}
if (listErrorView.Items.Count > 0)
{
listErrorView.Items[0].Selected = true ;
}
chbAllShow.Enabled = true;
if (result.Equals(1))
{
lblError.Visible = false ;
lblInfo.Text = AccAOI.ControlUtil.GetResultsStr(resultBean);
lblInfo.Visible = true;
LogUtil.info("AOI检测OK:" + lblInfo.Text);
}
else
{
lblInfo.Visible = false;
lblError.Text = ngList;
lblError.Visible = true;
LogUtil.info("AOI检测NG:" + ngList);
}
}
else
{
lblError.Text = ResourceCulture.GetString("获取相机图片失败", "获取相机图片失败");
lblError.Visible = true;
LogUtil.error("AOI检测:获取相机图片失败:" + CameraManager.ErrorMsg);
return 0;
}
chbAllShow.Enabled = true ;
}
catch (Exception ex)
{
lblError.Text = ResourceCulture.GetString("执行检测失败", "执行检测失败");
lblError.Visible = true;
LogUtil.error("检测出错:" + ex.ToString());
}
return result;
}
private string GetShowName(ResultBean bean)
{
return (bean.result ? "✔ " : "✘ ") + bean.MethodName;
}
private string GetNameByText(string str)
{
string name= str.Substring(2, str.Length - 2);
return name;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnRun_Click(object sender, EventArgs e)
{
CheckResult = AoiCheck();
MesUtil.SetAOIResult(CheckResult);
UpdateTextResult(lblResult, CheckResult);
if (CheckResult.Equals(1))
{
isAutoClose = true;
}
else
{
isAutoClose = false;
}
}
private void FrmCameraAOI_Shown(object sender, EventArgs e)
{
showTime = DateTime.Now;
}
private void btnExit_Click_1(object sender, EventArgs e)
{
timer1.Stop();
this.Close();
}
private void listErrorView_SelectedIndexChanged(object sender, EventArgs e)
{
if (listErrorView.SelectedItems != null && listErrorView.SelectedItems.Count > 0)
{
string selName = GetNameByText(listErrorView.SelectedItems[0].Text) ;
List<ResultBean> beans = (from m in ResultList where m.MethodName.Equals(selName) select m).ToList<ResultBean>();
if (beans.Count > 0)
{
ResultBean bean = beans[0];
picStand.Image = bean.standardRoiImage;
picCurr.Image = bean.currentRoiImage;
picResult.SelectionRegion = bean.roiPath;
//Graphics g = picResult.CreateGraphics();
//g.DrawPath(new Pen(Color.Orange), bean.roiPath);
//g.Dispose();
}
}
}
private void chbAllShow_CheckedChanged(object sender, EventArgs e)
{
if (ResultList.Count <= 0)
{
return;
}
listErrorView.Items.Clear();
foreach (ResultBean bean in ResultList)
{
if (bean.result.Equals(false))
{
listErrorView.Items.Add(GetShowName(bean));
}
else if (chbAllShow.Checked)
{
listErrorView.Items.Add(GetShowName(bean));
}
}
if (listErrorView.Items.Count > 0)
{
listErrorView.Items[0].Selected = true;
}
listErrorView_SelectedIndexChanged(null, null);
}
}
}