ProjectorControl.cs
23.3 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
namespace UserFromControl
{
public partial class ProjectorControl : UserControl
{
public static Dictionary<int,int> LastStepIndexMap=null;
public int ConType = 0;//坐标类型,0=投影,1=mm/离线编程
public int MaxX = 1920;
public int MaxY = 1280;
public double X_ChangeValue = 1;
public double Y_ChangeValue = 1;
public delegate bool ShowPointDelegate(ProjectorPInfo p);
public event ShowPointDelegate ShowPointEvent;
public ProjectorControl()
{
if (ImageManager.IsInit == false)
{
ImageManager.Init();
}
InitializeComponent();
if (LastStepIndexMap == null)
{
LastStepIndexMap = new Dictionary<int, int>();
LastStepIndexMap.Add(0, 1);
LastStepIndexMap.Add(1, 0);
}
cmbStep.SelectedIndex = LastStepIndexMap[ConType];
if (cmbType.SelectedIndex < 0)
{
cmbType.SelectedIndex = 0;
}
cmbpolarities.SelectedIndex = 0;
PointSizeX = Setting_NInit.Device_DefaultPointSize;
PointSizeY = Setting_NInit.Device_DefaultPointSize;
PointType = Setting_NInit.Device_DefaultPointType;
PolaritiesType = 0;
numSizeX.Maximum = MaxX / 2;
numSizeY.Maximum=MaxY/ 2;
btnUp.BackgroundImage = ImageManager.imgUp;
btnDown.BackgroundImage = ImageManager.imgDown;
btnLeft.BackgroundImage = ImageManager.imgLeft;
btnRight.BackgroundImage = ImageManager.imgRight;
pictureBox1.Image = ImageManager.imgType1;
pictureBox2.Image = ImageManager.imgType2;
pictureBox3.Image = ImageManager.imgType3;
pictureBox4.Image = ImageManager.imgType4;
pictureBox5.Image = ImageManager.imgType5;
pictureBox6.Image = ImageManager.imgType6;
}
public void loadTypeList(string[] list, string[] dirlist,Dictionary<string,string> controlTextMap)
{
int oldT = cmbType.SelectedIndex;
int oldPt=cmbpolarities.SelectedIndex;
typeList = new List<string>();
this.cmbType.Items.Clear();
typeList.AddRange(list);
foreach (string str in typeList)
{
cmbType.Items.Add(str);
}
cmbpolarities.Items.Clear();
foreach(string str in dirlist)
{
cmbpolarities.Items.Add(str);
}
cmbType.SelectedIndex = oldT;
cmbpolarities.SelectedIndex = oldPt;
group.Text = controlTextMap["group_Text"];
lblLineWidth.Text = controlTextMap["lblLineWidth_Text"];
lblName.Text = controlTextMap["lblName_Text"];
lblSizeLength.Text = controlTextMap["lblSizeLength_Text"];
lblSizeWidth.Text = controlTextMap["lblSizeWidth_Text"];
lblStepValue.Text = controlTextMap["lblStepValue_Text"];
lbltype.Text = controlTextMap["lbltype_Text"];
lblx.Text = controlTextMap["lblx_Text"];
lbly.Text = controlTextMap["lbly_Text"];
btnDown.Text = controlTextMap["btnDown_Text"];
btnUp.Text = controlTextMap["btnUp_Text"];
btnShowPoint.Text = controlTextMap["btnShowPoint_Text"];
btnLeft.Text = controlTextMap["btnLeft_Text"];
btnRight.Text = controlTextMap["btnRight_Text"];
lblPolarities.Text = controlTextMap["lblPolarities_Text"];
btnDown.Text = "";
btnUp.Text = "";
btnLeft.Text = "";
btnRight.Text = "";
//
}
public string GroupName
{
get { return group.Text; }
set
{
group.Text = value;
}
}
public int ShowColor = -1;
private void btnShowPoint_Click(object sender, EventArgs e)
{
ShowCurrPoint();
}
public ProjectorPInfo GetPInfo()
{
double px = (double)(numX.Value);
double py = (double)(numY.Value);
int pointType = cmbType.SelectedIndex + 1;
int sizeX = FormUtil.GetIntValue(numSizeX);
int sizeY = FormUtil.GetIntValue(numSizeY);
int penWidth = (int)numPenWidth.Value;
string name = FormUtil.getValue(txtname);
int ptype = cmbpolarities.SelectedIndex;
int angle = (int)numAngle.Value;
angle = ProjectorPInfo.getAngleByType(ptype, angle);
////如果亮度和大小超过范围,直接用默认值
//if (sizeX < 1 || sizeX > 255)
//{
// sizeX = DefaultPSize;
// numSizeX.Text = sizeX.ToString();
//}
//if (sizeY < 1 || sizeY > 255)
//{
// sizeY = DefaultPSize;
// numSizeY.Text = sizeY.ToString();
//}
ProjectorPInfo p = new ProjectorPInfo(px, py, pointType,ptype, angle, sizeX, sizeY, penWidth,name, ShowColor);
return p;
}
public void ShowCurrPoint()
{
this.Cursor = Cursors.WaitCursor;
ShowPointEvent?.Invoke(GetPInfo());
this.Cursor = Cursors.Default;
}
private void btnUp_Click(object sender, EventArgs e)
{
double stepValue = (double)(GetStepValue() * Y_ChangeValue);
double targetValue = (double)(numY.Value) - stepValue;
if (targetValue >=(double) numY.Minimum )
{
numY.Value =(decimal) targetValue ;
ShowCurrPoint();
}
else
{
numY.Text = 1.ToString();
ShowCurrPoint();
}
}
private void btnLeft_Click(object sender, EventArgs e)
{
double stepValue = (double)(GetStepValue() * X_ChangeValue);
double targetValue = (double)(numX.Value) - stepValue;
if (targetValue >=(double) numX.Minimum )
{
numX.Value = (decimal)targetValue;
ShowCurrPoint();
}
else
{
numX.Text = 1.ToString();
ShowCurrPoint();
}
}
private void btnRight_Click(object sender, EventArgs e)
{
double stepValue = (double)(GetStepValue() * X_ChangeValue);
double targetValue =(double)numX.Value+ stepValue;
if ( targetValue <=(double)numX.Maximum)
{
numX.Value = (decimal)targetValue;
ShowCurrPoint();
}
else
{
numX.Text = MaxX.ToString();
ShowCurrPoint();
}
}
private void btnDown_Click(object sender, EventArgs e)
{
double stepValue = (double)(GetStepValue() * Y_ChangeValue);
double targetValue = (double)(numY.Value) + stepValue;
if ( targetValue <= (double)numY.Maximum)
{
numY.Value = (decimal)targetValue;
ShowCurrPoint();
}
else
{
numY.Text = MaxY.ToString();
ShowCurrPoint();
}
}
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbType.SelectedIndex >= 1 && cmbType.SelectedIndex <= 3)
{
numSizeY.Text = numSizeX.Text;
}
if (!(cmbType.SelectedIndex + 1).Equals(lastType))
{
lastType = cmbType.SelectedIndex + 1;
DrawPoint(lastType,lastPolaritiesType, lastAngle);
ShowCurrPoint();
TypeUpdate(cmbType.SelectedIndex);
}
}
private int lastType=1;
private int lastPolaritiesType = 0;
private int lastAngle = 0;
public double XValue
{
set
{
numX.Text = (value).ToString();
}
get
{
return (double)numX.Value;
}
}
public double YValue
{
set
{
numY.Text = (value).ToString();
}
get
{
return (double)numY.Value;
}
}
public int PointType
{
set
{
if ((int)value > cmbType.Items.Count)
{
value = cmbType.Items.Count - 1;
}
if ((int) value >= 1)
{
cmbType.SelectedIndex = ((int)value) - 1;
}
else
{
cmbType.SelectedIndex = 0;
}
}
get
{
return cmbType.SelectedIndex+1;
}
}
public int PointSizeX
{
set
{
numSizeX.Text = ((int)value).ToString();
}
get
{
return FormUtil.GetIntValue(numSizeX);
}
}
public int PointSizeY
{
set
{
numSizeY.Text = ((int)value).ToString();
}
get
{
return FormUtil.GetIntValue(numSizeY);
}
}
public int PenWidth
{
set
{
numPenWidth.Text = ((int)value).ToString();
}
get
{
return FormUtil.GetIntValue(numPenWidth);
}
}
public string ShowText
{
set
{
txtname.Text = (value).ToString();
}
get
{
return FormUtil.getValue(txtname);
}
}
public int PolaritiesType
{
set
{
if ((int)value >= cmbpolarities.Items.Count)
{
value = cmbpolarities.Items.Count - 1;
}
if ((int)value >= 0)
{
cmbpolarities.SelectedIndex = ((int)value) ;
}
else
{
cmbpolarities.SelectedIndex = 0;
}
}
get
{
return cmbpolarities.SelectedIndex;
}
}
public int PolarityAngle
{
set
{
if ((int)value >= 0)
{
numAngle.Value = value;
}
else
{
numAngle.Value = ProjectorPInfo.getAngleByType(PolaritiesType, value);
}
}
get
{
return (int) numAngle.Value;
}
}
public void UpdateStatus()
{
//numX.Text = GalvanometerManager.GetLastPoint().X.ToString();
//numY.Text = GalvanometerManager.GetLastPoint().Y.ToString();
}
private List<double> vallue = new List<double>() { 1, 2, 5, 10, 20, 50,100 };
private double GetStepValue()
{
int index = cmbStep.SelectedIndex;
LastStepIndexMap[ConType]= index;
if (vallue.Count > index)
{
return vallue[index];
}
return 10;
}
private List<string> typeList = new List<string>();
private List<PictureBox> pictureBoxes = new List<PictureBox>();
private List<Image> picDefImages = new List<Image>();
private List<Image> picSelImages = new List<Image>();
private void ProjectorControl_Load(object sender, EventArgs e)
{
try
{
if (cmbType.SelectedIndex < 0)
{
cmbType.SelectedIndex = 0;
}
numSizeX.Maximum = MaxX / 2;
numSizeY.Maximum = MaxY / 2;
//重新设置步进值
if (ConType == 1)
{
this.cmbStep.Items.Clear();
this.cmbStep.Items.AddRange(new object[] {
"0.1",
"0.2",
"0.5",
"1",
"2",
"5",
"10",
"20",
"50",
"100"});
numX.DecimalPlaces = 3;
numY.DecimalPlaces = 3;
vallue = new List<double>() { 0.1, 0.2, 0.51, 2, 5, 10, 20, 50, 100 };
}
else
{
numX.DecimalPlaces = 0;
numY.DecimalPlaces = 0;
}
int sindex = LastStepIndexMap[ConType];
if (cmbStep.Items.Count > sindex)
{
cmbStep.SelectedIndex = LastStepIndexMap[ConType];
}
else
{
cmbStep.SelectedIndex = cmbStep.Items.Count / 2;
}
pictureBoxes.Add(pictureBox1);
pictureBoxes.Add(pictureBox2);
pictureBoxes.Add(pictureBox3);
pictureBoxes.Add(pictureBox4);
pictureBoxes.Add(pictureBox5);
pictureBoxes.Add(pictureBox6);
picDefImages.Add(ImageManager.imgType1);
picDefImages.Add(ImageManager.imgType2);
picDefImages.Add(ImageManager.imgType3);
picDefImages.Add(ImageManager.imgType4);
picDefImages.Add(ImageManager.imgType5);
picDefImages.Add(ImageManager.imgType6);
picSelImages.Add(ImageManager.imgType1s);
picSelImages.Add(ImageManager.imgType2s);
picSelImages.Add(ImageManager.imgType3s);
picSelImages.Add(ImageManager.imgType4s);
picSelImages.Add(ImageManager.imgType5s);
picSelImages.Add(ImageManager.imgType6s);
TypeUpdate(cmbType.SelectedIndex);
}
catch (Exception ex)
{
LogUtil.error("ProjectorControl_Load 出错:" + ex.ToString());
}
}
private void DrawPoint(int type,int polaritiesType ,int angle, int x=0, int y=0, int sizeX = 2, int sizeY = 2 ,int lineLength=2 )
{
Graphics g = panImg.CreateGraphics();
if (sizeX < 2)
{
sizeX = 2;
}
if (sizeY < 2)
{
sizeY = 2;
}
x = panImg.Width / 2;
y = panImg.Height / 2;
sizeX = x > y ? (x / 4) : (y / 4);
sizeY = sizeX;
g.Clear(panImg.BackColor);
Pen pen = new Pen(panImg.ForeColor, lineLength);
int pointWidth = sizeX;//圆形的半径
if (sizeY > sizeX)
{
pointWidth = sizeY;
}
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(panImg.ForeColor);
//点类型,1 = 点,2 = +,3 =|,4 = -,5 = 方形,6 = 圆圈
if (type.Equals(2))
{
g.DrawLine(pen, new Point(x - sizeX, y), new Point(x + sizeX, y));
g.DrawLine(pen, new Point(x, y - sizeY), new Point(x, y + sizeY));
}
else if (type.Equals(3))
{
g.DrawLine(pen, new Point(x, y - sizeY), new Point(x, y + sizeY));
}
else if (type.Equals(4))
{
g.DrawLine(pen, new Point(x - sizeX, y), new Point(x + sizeX, y));
}
else if (type.Equals(5))
{
g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
}
else if (type.Equals(6))
{
g.DrawEllipse(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
}else if (type>6)
{
g.DrawEllipse(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
}
else
{
g.FillEllipse(myBrush, new Rectangle(x - sizeX / 2, y - sizeY / 2, sizeX, sizeY));//画实心椭圆
}
if (polaritiesType.Equals(0))
{
}
else if (polaritiesType.Equals(1))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x;
int pY = y - sizeY / 2 - lineLength - plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(2))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x;
int pY = y + sizeY / 2 + lineLength + plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(3))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeY / 2;
int pX = x - sizeX / 2 - lineLength - plusSize;
int pY = y;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(4))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeY / 2;
int pX = x + sizeX / 2 + lineLength + plusSize;
int pY = y;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(5))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x - 6;
int pY = y - sizeY / 2 - lineLength - plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(6))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x + 6;
int pY = y - sizeY / 2 - lineLength - plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(7))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x - 6;
int pY = y + sizeY / 2 + lineLength + plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (polaritiesType.Equals(8))
{
//sizeX = 12;
//sizeY = 36;
int plusSize = sizeX / 2;
int pX = x + 6;
int pY = y + sizeY / 2 + lineLength + plusSize;
g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
}
else if (angle > 0)
{
Point drawP = ProjectorPInfo.CalculatePoint(new Point(x, y), pointWidth, angle);
g.DrawLine(pen, new Point(drawP.X - pointWidth / 2, drawP.Y), new Point(drawP.X + pointWidth / 2, drawP.Y));
g.DrawLine(pen, new Point(drawP.X, drawP.Y - pointWidth / 2), new Point(drawP.X, drawP.Y + pointWidth / 2));
}
}
private void group_Paint(object sender, PaintEventArgs e)
{
return;
}
private void cmbpolarities_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(cmbpolarities.SelectedIndex + 1).Equals(lastPolaritiesType))
{
lastPolaritiesType = cmbpolarities.SelectedIndex;
lastAngle =(int) numAngle.Value;
lastAngle = ProjectorPInfo.getAngleByType(lastPolaritiesType, lastAngle);
numAngle.Enabled = lastPolaritiesType.Equals(9);
numAngle.Value = lastAngle;
DrawPoint(lastType, lastPolaritiesType, lastAngle);
ShowCurrPoint();
}
}
public void SetPointType(int pointType, int pointSizeX, int pointSizeY, int penWidth, int polaritiesType,int angle)
{
this.PointType = pointType;
this.PointSizeX = pointSizeX;
this.PointSizeY = pointSizeY;
this.PenWidth = penWidth;
this.PolaritiesType = polaritiesType;
this.PolarityAngle =ProjectorPInfo.getAngleByType(polaritiesType, angle);
}
private void TypeUpdate(int index)
{
for (int i = 0; i < pictureBoxes.Count; i++)
{
if (i.Equals(index))
{
pictureBoxes[i].Image = picSelImages[i];
}
else
{
pictureBoxes[i].Image = picDefImages[i];
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
PointType = 1;
}
private void pictureBox3_Click(object sender, EventArgs e)
{
PointType = 3;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
PointType = 2;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
PointType = 4;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
PointType = 5;
}
private void pictureBox6_Click(object sender, EventArgs e)
{
PointType = 6;
}
private void numAngle_ValueChanged(object sender, EventArgs e)
{
lastPolaritiesType = cmbpolarities.SelectedIndex;
lastAngle = (int)numAngle.Value;
lastAngle = ProjectorPInfo.getAngleByType(lastPolaritiesType, lastAngle);
DrawPoint(lastType, lastPolaritiesType, lastAngle);
ShowCurrPoint();
}
}
}