FrmAoiSetting.cs
14.7 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
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
using AOI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccAOI
{
public partial class FrmAoiSetting : Asa.Theme.FlatForm
{
private control.ABaseControl aoiControl = null;
public static Image BaseImg = null;
public static Image TestImage = null;
private AoiProject Project = null;
private bool CanSel = true;
private string DefaultPath = "";
public FrmAoiSetting()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}
public FrmAoiSetting(string programPath, Image image = null, string defaultPath = "")
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
if (!programPath.Equals("") && (image != null))
{
BaseImg = image;
// Project = new AoiProject(image);
string result = "";
Project= AoiProject.Load(programPath,out result);
if (!result.Equals(""))
{
MessageBox.Show("加载项目" + programPath + "失败:\r\n" + result);
}
}
DefaultPath = defaultPath;
ShowPorject();
}
private void FrmAoiSetting_Load(object sender, EventArgs e)
{
comType.ItemAdd(ControlType.Mark);
comType.ItemAdd(ControlType.AOIBlob);
comType.ItemAdd(ControlType.AOIRGB);
comType.ItemAdd(ControlType.Match);
comType.Text = ControlType.Mark;
string defaultImg = "F:\\电路板图片\\照片 2创建于2019年4月19日 11_34_53.jpg";
if (System.IO.File.Exists(defaultImg))
{
//读取图片内容
BaseImg = (Image)Image.FromFile(defaultImg).Clone();
imageBox1.Image = BaseImg;
}
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开本地图片";
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
//读取图片内容
BaseImg = (Image)Image.FromFile(fileName).Clone();
if (Project != null)
{
Project.standardImage = BaseImg;
}
imageBox1.Image = BaseImg;
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void btnGetCameraImg_Click(object sender, EventArgs e)
{
string camera = cmbCameraList.Text;
if (camera.Equals(""))
{
MessageBox.Show("清先选择相机");
return;
}
}
private void btnOpenPro_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开项目";
openDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
if (!String.IsNullOrEmpty(DefaultPath))
{
openDialog.InitialDirectory = DefaultPath;
}
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
// Project = new AoiProject(BaseImg);
string msg = "";
Project = AoiProject.Load(fileName, out msg);
if (!msg.Equals(""))
{
MessageBox.Show("加载项目" + fileName + "失败:\r\n" + msg);
}
ShowPorject();
}
private void btnSavePro_Click(object sender, EventArgs e)
{
if (this.Project == null)
{
MessageBox.Show("没有项目可保存");
return;
}
SaveCurrAoi();
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Title = "保存项目";
saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
if (!String.IsNullOrEmpty(DefaultPath))
{
saveFileDialog.InitialDirectory = DefaultPath;
}
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = saveFileDialog.FileName;
Project.Save(fileName);
MessageBox.Show("保存成功");
}
private void btnNewAoi_Click(object sender, EventArgs e)
{
if (BaseImg == null)
{
MessageBox.Show("清先选择基准图片");
return;
}
if (this.Project == null)
{
Project = new AoiProject(BaseImg);
}
if (comType.Text.Equals(""))
{
MessageBox.Show("请选择类型");
comType.Focus();
return;
}
string text = comType.Text;
CloseCurrAoi();
AoiMethod methodInfo = ControlUtil.GetMethod(text);
methodInfo.MethodName = Project.methodMap.Values.Count.ToString().PadLeft(2, '0') + "_" + text;
Project.methodMap.Add(methodInfo.MethodName, methodInfo);
ShowAoi(methodInfo);
aoiList.ItemAdd(aoiControl.TitleName);
}
private void ShowAoi(AoiMethod method)
{
CanSel = false;
imageBox1.SelectNone();
imageBox1.Image = BaseImg;
CanSel = true;
if (aoiControl == null)
{
aoiControl = ControlUtil.GetControl(method);
}
this.aoiControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
aoiControl.FormBorderStyle = FormBorderStyle.None;
aoiControl.StartPosition = FormStartPosition.CenterParent;
aoiControl.Location = new Point(0, 0);
aoiControl.BImageBox=this.imageBox1;
aoiControl.AoiInfo = method;
aoiControl.Size = panAoi.Size;
aoiControl.TitleName = method.MethodName;
aoiControl.TopLevel = false;
aoiControl.Parent = panAoi;
if (method.RoiPath != null)
{
currPath = method.RoiPath;
imageBox1.SelectionRegion = method.RoiPath;
//Image threshImage = CutImage(imageBox1.Image, currPath);
//cutImage = threshImage;
aoiControl.SetCurrPath(currPath,true);
}
aoiControl.Show();
aoiControl.ShowAoiInfo();
}
private void SaveCurrAoi()
{
if (aoiControl != null &&aoiControl.Visible)
{
AoiMethod method = aoiControl.GetAoiInfo();
//更新
if (Project.methodMap.ContainsKey(method.MethodName))
{
Project.methodMap[method.MethodName] = method;
}
}
}
private void CloseCurrAoi()
{
SaveCurrAoi();
if (aoiControl != null)
{
if (aoiControl.Visible)
{
aoiControl.Close();
}
aoiControl.Dispose();
aoiControl = null;
}
}
private void ShowPorject()
{
if (Project != null)
{
CloseCurrAoi();
aoiList.ItemClear();
imageBox1.Image = Project.standardImage;
imageBox1.SelectionRegion = new GraphicsPath();
if (Project.methodMap.Count > 0)
{
int index = 0;
foreach (string key in Project.methodMap.Keys)
{
aoiList.ItemAdd(key);
if (index.Equals(0))
{
aoiList.Index = index;
ShowAoi(Project.methodMap[key]);
}
}
}
}
}
private void aoiList_IndexChanged(object sender, EventArgs e)
{
int index = aoiList.Index;
if (index >= 0 && (Project != null))
{
List<AoiMethod> methods = new List<AoiMethod>(Project.methodMap.Values);
if (index < methods.Count)
{
AoiMethod method = methods[index];
if (aoiControl != null && aoiControl.TitleName.Equals(method.MethodName))
{
return;
}
else
{
CloseCurrAoi();
ShowAoi(method);
}
}
}
}
private Image cutImage = null;
private GraphicsPath currPath = null;
private void imageBox1_SelectionRegionChanged(object sender, EventArgs e)
{
try
{
if (!CanSel)
{
return;
}
if (imageBox1.Image == null || BaseImg == null || aoiControl == null)
{
return;
}
currPath = imageBox1.SelectionRegion;
aoiControl.SetCurrPath( currPath);
}catch(Exception ex)
{
}
}
private void btnExcute_Click(object sender, EventArgs e)
{
SaveCurrAoi();
CanSel = false;
if (BaseImg == null)
{
return;
}
if (this.Project == null)
{
return;
}
if (TestImage == null)
{
MessageBox.Show("请选择测试图片");
return;
}
Image outImage = null;
List<ResultBean> result = Project.CheckAll(TestImage, out outImage);
if (outImage != null)
{
if (!this.testImageBox1.Visible)
{
btnImageChange_Click(null, null);
}
this.testImageBox1.SelectNone();
this.testImageBox1.Image = outImage;
}
CanSel = true;
}
private void btnDel_Click(object sender, EventArgs e)
{
if (BaseImg == null)
{
return;
}
if (this.Project == null)
{
return;
}
if (this.aoiControl != null)
{
DialogResult result= MessageBox.Show("确定删除 " + aoiControl.AoiInfo.MethodName + " ?", "确认提示", MessageBoxButtons.OKCancel);
if (result.Equals(DialogResult.OK))
{
if (Project.methodMap.ContainsKey(aoiControl.AoiInfo.MethodName))
{
Project.methodMap.Remove(aoiControl.AoiInfo.MethodName);
ShowPorject();
}
}
}
}
private void btnImageChange_Click(object sender, EventArgs e)
{
if (testImageBox1.Visible)
{
testImageBox1.Visible = false;
imageBox1.Visible = true;
btnImageChange.Text = "显示测试图片";
lblCurrImage.Text = "基准图:";
lblCurrImage.ForeColor = Color.Green;
}
else
{
testImageBox1.Visible = true;
imageBox1.Visible = false;
btnImageChange.Text = "显示基准图片";
lblCurrImage.Text = "测试/效果图:";
lblCurrImage.ForeColor = Color.Orange;
}
}
private void flatButton2_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开本地图片";
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
//读取图片内容
TestImage = (Image)Image.FromFile(fileName).Clone();
testImageBox1.Image = TestImage;
if (testImageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void FrmAoiSetting_Shown(object sender, EventArgs e)
{
testImageBox1.Size = imageBox1.Size;
testImageBox1.Location = imageBox1.Location;
testImageBox1.Visible = false;
}
private void flatButton1_Click(object sender, EventArgs e)
{
}
}
}