FrmCalibrate.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
namespace TSA_V.frmBoard
{
public partial class FrmCalibrate : FrmBase
{
private BoardInfo currBoard = null;
/// <summary>
/// 1=已确认左上角位置
/// 2=已确认右下角位置
/// 3=正在校准中
/// </summary>
private int CurrStep = 0;
public FrmCalibrate(BoardInfo board)
{
InitializeComponent();
this.currBoard = board;
}
private void FrmCalibrate_Load(object sender, EventArgs e)
{
picBoard.Image = currBoard.GetImage();
picBoard.SizeMode = PictureBoxSizeMode.StretchImage;
SetPicSize();
LoadPoint();
timer1.Start();
xyMoveControl1.loadTypeList(ResourceCulture.GetPTypes(), ResourceCulture.GetDirTypes(), ResourceCulture.GetXYControlMap());
xyMoveControl1.ShowPointEvent += XyMoveControl1_ShowPointEvent;
xyMoveControl1.PointType = 2;
xyMoveControl1.PolaritiesType = 0;
xyMoveControl1.PointSizeX = 20;
xyMoveControl1.PointSizeY = 20;
xyMoveControl1.PenWidth = 4;
xyMoveControl1.XValue = 300;
xyMoveControl1.YValue = 300;
btnLeftUp.Visible = true;
btnRightBottom.Visible = false;
btnCal.Visible = false;
xyMoveControl1.ShowCurrPoint();
CurrStep = 0;
LoadCalInfo();
}
private bool XyMoveControl1_ShowPointEvent(ProjectorPInfo p)
{
FrmProjectorScreen.instance.ClearPoint();
FrmProjectorScreen.instance.ShowPoint(p);
return true;
}
private void SetPicSize()
{
int yuliuP = 30;
int xishu = 100;
picBoard.Width = currBoard.boardWidth * xishu;
picBoard.Height = currBoard.boardLength * xishu;
int maxWidth = panBoard.Width - yuliuP * 2;
int maxHeight = panBoard.Height - yuliuP * 2;
if (picBoard.Width > maxWidth)
{
picBoard.Height = currBoard.boardLength * xishu * maxWidth / (currBoard.boardWidth * xishu);
picBoard.Width = maxWidth;
}
if (picBoard.Height > maxHeight)
{
picBoard.Width = currBoard.boardWidth * xishu * maxHeight / (currBoard.boardLength * xishu);
picBoard.Height = maxHeight;
}
picBoard.Location=new Point( (panBoard.Width - picBoard.Width )/ 2,(panBoard.Height-picBoard.Height)/2);
}
private void LoadCalInfo()
{
if (currBoard.calInfo == null)
{
CurrStep = 0;
}
else
{
if (CurrStep == 0)
{
Point p = new Point();
if (currBoard.calInfo.leftUpPoint != null)
{
p = currBoard.calInfo.leftUpPoint;
xyMoveControl1.XValue = p.X;
xyMoveControl1.YValue = p.Y;
}
else
{
xyMoveControl1.XValue = 300;
xyMoveControl1.YValue = 300;
}
xyMoveControl1.ShowCurrPoint();
btnLeftUp.Visible = true;
btnRightBottom.Visible = false;
btnCal.Visible = false;
lblMsg.Text = ResourceCulture.GetString("CalMsg1", "第一步:请将光标移动至如图示电路板左上角位置,确认后点击设置按钮");
}
else if (CurrStep == 1)
{
Point p = new Point();
if (currBoard.calInfo.rightBottomPoint != null)
{
p = currBoard.calInfo.rightBottomPoint;
xyMoveControl1.XValue = p.X;
xyMoveControl1.YValue = p.Y;
}
else
{
p = currBoard.calInfo.leftUpPoint;
xyMoveControl1.XValue = p.X;
xyMoveControl1.YValue = p.Y;
}
xyMoveControl1.ShowCurrPoint();
btnLeftUp.Visible = false;
btnRightBottom.Visible = true;
btnCal.Visible = false;
lblMsg.Text = ResourceCulture.GetString("CalMsg2", "第二步:请将光标移动至如图示电路板右下角位置,确认后点击设置按钮");
}
else if (CurrStep == 2)
{
btnLeftUp.Visible = false;
btnRightBottom.Visible = false;
btnCal.Visible = true;
lblMsg.Text = ResourceCulture.GetString("CalMsg3", "第三步:请点击开始校准按钮,自动计算投影坐标");
}
}
}
private void LoadPoint()
{
Crop();
}
private void Crop()
{
try
{
Color color = lastColor;
Graphics g = panBoard.CreateGraphics();
g.Clear(panBoard.BackColor);
int lineWidth = 5;
int lineLength = 20;
if (CurrStep == 0 || CurrStep == 2)
{
Point point1 = new Point(picBoard.Location.X, picBoard.Location.Y);
g.DrawLine(new Pen(color, lineWidth), new Point(point1.X - lineLength, point1.Y), new Point(point1.X + lineLength, point1.Y));
g.DrawLine(new Pen(color, lineWidth), new Point(point1.X, point1.Y - lineLength), new Point(point1.X, point1.Y + lineLength));
}
if (CurrStep == 1 || CurrStep == 2)
{
Point point2 = new Point(picBoard.Location.X + picBoard.Width, picBoard.Location.Y + picBoard.Height);
g.DrawLine(new Pen(color, lineWidth), new Point(point2.X - lineLength, point2.Y), new Point(point2.X + lineLength, point2.Y));
g.DrawLine(new Pen(color, lineWidth), new Point(point2.X, point2.Y - lineLength), new Point(point2.X, point2.Y + lineLength));
}
g.Flush();
g.Dispose();
if (lastColor.Equals(Color.Red))
{
lastColor = Color.Orange;
}
else
{
lastColor = Color.Red;
}
}
catch(Exception ex)
{
LogUtil.error("画标定位置出错:" + ex.ToString());
}
}
private Color lastColor = Color.Red;
private void timer1_Tick(object sender, EventArgs e)
{
if (!this.Visible)
{
return;
}
Crop();
}
private void btnLeftUp_Click(object sender, EventArgs e)
{
Point p = new Point((int)xyMoveControl1.XValue, (int)xyMoveControl1.YValue);
if (currBoard.calInfo == null)
{
currBoard.calInfo = new CalibrateBean();
currBoard.calInfo.leftUpPoint = p;
currBoard.calInfo.rightBottomPoint = new Point(p.X + 300, p.Y + 300 ) ;
}
else
{
currBoard.calInfo.leftUpPoint = p;
}
currBoard.calInfo.CurrStep = 1;
//BoardManager.Update(currBoard);
LogUtil.info($"程序{currBoard.boardName}设置左上角坐标为:{p.X},{p.Y}");
CurrStep = 1;
LoadCalInfo();
}
private void btnRightBottom_Click(object sender, EventArgs e)
{
Point p = new Point((int)xyMoveControl1.XValue, (int)xyMoveControl1.YValue);
if (currBoard.calInfo == null)
{
currBoard.calInfo = new CalibrateBean();
}
currBoard.calInfo.rightBottomPoint = p;
currBoard.calInfo.CurrStep = 2;
//BoardManager.Update(currBoard);
LogUtil.info($"程序{currBoard.boardName}设置右下角坐标为:{p.X},{p.Y}");
CurrStep = 2;
LoadCalInfo();
}
private void btnCal_Click(object sender, EventArgs e)
{
this.Enabled = false;
try
{
if (currBoard.calInfo == null || currBoard.calInfo.leftUpPoint == null)
{
CurrStep = 0;
LoadCalInfo();
return;
}
else if (currBoard.calInfo.rightBottomPoint == null)
{
CurrStep = 1;
LoadCalInfo();
return;
}
else
{
CalibrationProcess();
}
}
catch (Exception ex)
{
LogUtil.error("校准出错:" + ex.ToString());
}
finally
{
this.Enabled = true;
}
}
private void CalibrationProcess()
{
try
{
List<SMTPointInfo> pointList = new List<SMTPointInfo>(currBoard.smtList);
List<SMTPointInfo> checkOKList = currBoard.getCalPoint();
LogUtil.info($" 【{currBoard.boardName}】左上角(0,0)坐标【{checkOKList[0].NodePositionX},{checkOKList[0].NodePositionY}】右下角" +
$"({checkOKList[1].PositionX},{checkOKList[1].PositionY})【{checkOKList[1].NodePositionX},{checkOKList[1].NodePositionY}】");
for (int i = 0; i < pointList.Count; i++)
{
pointList[i] =XYConvertManager.CalPointNodePosition(pointList[i], checkOKList);
}
currBoard.smtList = pointList;
BoardManager.Update(currBoard);
FrmProjectorScreen.instance.ClearPoint();
FrmProjectorScreen.instance.ShowPoint(false, currBoard.PointColor, pointList.ToArray());
this.Cursor = Cursors.Default;
//重新加载
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.TrainOK, "坐标校准完成!"));
//记录校准信息
XYConvertManager.SaveData(currBoard);
DialogResult = DialogResult.OK;
this.Close();
}
catch (Exception ex)
{
LogUtil.error("校准坐标出错:" + ex.ToString());
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void FrmCalibrate_Shown(object sender, EventArgs e)
{
xyMoveControl1.ShowCurrPoint();
}
}
}