FrmTSAV.cs
49.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
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
using System;
using System.Drawing;
using System.Windows.Forms;
using TSA_V.DeviceLibrary;
using TSA_V.Common;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using TSA_V.LoadCSVLibrary;
using AOI;
using System.Linq;
using System.Threading.Tasks;
namespace TSA_V
{
public partial class FrmWork : FrmBase
{
private bool isContinue;
public FrmWork(bool iscontinue=false)
{
InitializeComponent();
isContinue= iscontinue;
WorkInfo.isContinue = iscontinue;
}
private bool isInitOk = false;
private BoardInfo board = BoardManager.CurrBoard;
private PointDisplay display = new PointDisplay();
private List<SMTPointInfo> workSmtList = new List<SMTPointInfo>();
private string needShowMsg = "";
private void FrmWelding_Load(object sender, EventArgs e)
{
FormProcess();
TSAVBean.Work.WorkType = 1;
LanguageProcess();
BoardInfo board = BoardManager.CurrBoard;
if (board != null)
{
string modeStr = "";
lblGuoBan.Visible = false;
if (TSAVBean.OnlyGuoBan)
{
modeStr = "-" + ResourceCulture.GetString("FrmBoardSelect_chbGuoban_Text", "过板模式");
lblGuoBan.Text = ResourceCulture.GetString("FrmBoardSelect_chbGuoban_Text", "过板模式");
lblGuoBan.Visible = true;
}
else if (TSAVBean.OpenOfflineMode)
{
modeStr = "-" + ResourceCulture.GetString("FrmBoardSelect_chbOffLine_Text", "离线工作模式");
}
else if (TSAVBean.WorkSingleStart)
{
modeStr = "-" + ResourceCulture.GetString("FrmBoardSelect_chbWorkSingleStart_Text", "工作区检测开始工作");
}
this.Text = "Neo Station:" + board.boardName + modeStr;
workSmtList = board.GetSmtList();
preIndex = -1;
smtPoint = new SMTPointInfo();
timerShowForm.Stop();
//获取记录运行未完成的步骤序号
//int TagNumber = ConfigAppSettings.GetIntValue(Setting_Init.TagNumber);
int TagNumber = Setting_NInit.Work_TagNumber;
if (workSmtList.Count > 0&&TagNumber!=0&& isContinue)
{
var workSmt = workSmtList.Single(a => a.pointNum == TagNumber);
smtPoint = workSmt;
preIndex = TagNumber-1;
}
else
{
smtPoint = workSmtList[0];
}
ShowMsg();
picBoard.Image = board.GetImage();
loadPictureBoxSize(board);
LoadPoint();
txtAuToTime.Text = TSAVBean.AuToModeSeconds.ToString();
//btnCamera.Visible = TSAVBean.IsNeedAOI;
btnCamera.Visible = Setting_NInit.App_IsDebug && Setting_NInit.Work_IsNeedAOI;
if (TSAVBean.WorkMode.Equals(0))
{
radioButton1.Checked = true;
AutoVisiable(false);
}
else
{
radioButton2.Checked = true;
AutoVisiable(true);
}
}
else
{
LogUtil.info("配置程序之后才能工作!");
this.Close();
}
if (TSAVBean.IsNeedAOI && !string.IsNullOrEmpty(board.AOIProName))
{
LoadAoi();
if (CurrProject == null)
{
LogUtil.info("没有读取到AOI,结束,IsNeedAOI=true!");
needShowMsg+= ResourceCulture.GetString("加载AOI失败")+"\r\n";
//MessageBox.Show(ResourceCulture.GetString("加载AOI失败"));
//this.Close();
}
}
if (!IsSet)
{
TSAVBean.InputCodeEvent += TSAVBean_InputCodeEvent;
IsSet = true;
}
timerShowForm.Start();
if (TSAVBean.OnlyGuoBan)
{
bool Visible = false;
btnCamera.Visible = Visible;
btnWorkInfo.Visible = Visible;
btnStart.Visible = Visible;
btnStartWorking.Visible = Visible;
btnPrePoint.Visible = Visible;
btnNextPoint.Visible = Visible;
btnGoHome.Visible = Visible;
}
HandRecordManager.Reset();
isInitOk = true;
// string ip = ConfigAppSettings.GetValue(Setting_Init.StatusServerIp);
// bool result = StatusClient.instance.Connect();
// LogUtil.info("连接状态服务器【" + ip + "】【" + result + "】【" + StatusClient.instance.ErrInfo + "】");
// StatusClient.instance.SendNew(Color.Green, TSAVBean.Name + "进入待机状态");
}
private void FormProcess()
{
if (Setting_NInit.Device_HandsVideo)
{
groupHand.Visible = true;
groupBoard.Visible = true;
linkLabel1.Visible = true;
}
else
{
linkLabel1.Visible = false;
groupHand.Visible = false;
groupBoard.Visible = true;
groupBoard.Location = new Point(groupHand.Location.X, groupHand.Location.Y);
groupBoard.Size = new Size(groupHand.Width, groupHand.Height);
this.groupBoard.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
}
}
private AOI.AoiProject CurrProject = null;
private void LoadAoi()
{
try
{
Thread.Sleep(1);
string currProjectName = BoardManager.CurrBoard.GetAoiFileName();
if (File.Exists(currProjectName))
{
string msg = "";
CurrProject = AoiProject.Load(currProjectName, out msg);
if (!msg.Equals(""))
{
LogUtil.error("加载项目" + currProjectName + "失败:\r\n" + msg);
CurrProject = null;
}
else
{
LogUtil.error("加载项目" + currProjectName + "成功");
}
}
else
{
LogUtil.error("加载AOI项目" + currProjectName + ",文件不存在");
CurrProject = null;
}
}
catch (Exception ex)
{
LogUtil.error("加载aoi项目出错:" + ex.ToString());
}
}
private void AutoVisiable(bool isShow)
{
txtAuToTime.Visible = isShow;
btnUpdateAutoTime.Visible = isShow;
//label7.Visible = isShow;
label8.Visible = isShow;
}
private void LoadPoint()
{
if (smtPoint == null)
{
return;
}
if (pointXMap.ContainsKey(smtPoint.pointNum))
{
preShowPointX = pointXMap[smtPoint.pointNum];
preShowPointY = pointYMap[smtPoint.pointNum];
}
else
{ //画图
int width = Convert.ToInt32(board.boardWidth);
int height = Convert.ToInt32(board.boardLength);
//Graphics grfx = picBoard.CreateGraphics();
preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;
}
Crop(Brushes.Red);
if (timerShowForm.Enabled.Equals(false))
{
timerShowForm.Start();
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (btnPrePoint.Enabled.Equals(true) && btnNextPoint.Enabled.Equals(true))
{
// 按快捷键Ctrl+S执行按钮的点击事件方法
if (keyData.Equals(Keys.Up) || keyData.Equals(Keys.MButton | Keys.Space))
{
btnPrePoint.PerformClick();
return true;
}
if (keyData.Equals(Keys.Down) || keyData.Equals(Keys.Back | Keys.Space))
{
this.btnNextPoint.PerformClick();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData); // 其他键按默认处理
}
private void StartRun()
{
if (TSAVBean.Status <= TSAVStatus.Wait)
{
LogUtil.info("开始启动设备");
string str = TSAVBean.StartRun();
if (!str.Equals(""))
{
lblMsg.Text = ResourceCulture.GetString(ResourceCulture.StartError, "启动失败") + ":" + str;
lblMsg.ForeColor = Color.Red;
LogUtil.error("启动失败:" + str);
needShowMsg += ResourceCulture.GetString(ResourceCulture.StartError, "启动失败:") + str;
//MessageBox.Show(ResourceCulture.GetString(ResourceCulture.StartError, "启动失败:") + str);
}
}
timer.Start();
}
private Dictionary<int, double> pointXMap = new Dictionary<int, double>();
private Dictionary<int, double> pointYMap = new Dictionary<int, double>();
private Bitmap bitmap = null;
private bool isFirstLoad = true ;
private void loadPictureBoxSize(BoardInfo board)
{
display.LoadWorkFromPoint(board, smtPoint, true);
updatePicPointSize(picBoard.Width, picBoard.Height);
pointXMap = display.pointXMap;
pointYMap = display.pointYMap;
}
private void updatePicPointSize(int pWidth, int pHeight)
{
pWidth = pWidth * 100;
pHeight = pHeight * 100;
bool ischange = false;
if (pWidth > panPoint.Width)
{
picPoint.Height = pHeight * panPoint.Width / pWidth;
picPoint.Width = panPoint.Width;
ischange = true;
}
if (picPoint.Height > panPoint.Height)
{
picPoint.Width = pWidth * panPoint.Height / pHeight;
picPoint.Height = panPoint.Height;
ischange = true;
}
if (!ischange)
{
picPoint.Height = pHeight;
picPoint.Width = pWidth;
}
}
private void timer_Tick(object sender, EventArgs e)
{
try
{
TimeSpan ss = DateTime.Now - LastTime;
if (ss.TotalSeconds > TSAVBean.AuToModeSeconds)
{
LastTime = DateTime.Now;
int value = ConfigAppSettings.GetIntValue("CodeRun");
if (value.Equals(1))
{
btnNextPoint_Click(null, null);
}
}
FormStatus();
lblMsg.Text = TSAVBean.GetShowMsg();
lblMsg.ForeColor = Color.Red;
if (TSAVBean.Status.Equals(TSAVStatus.Runing))
{
//如果没有开始工作,自动开始工作
//if (!TSAVBean.Work.IsWorking && btnStartWorking.Enabled)
//{
// btnStartWorking_Click(null, null);
//}
if (!preIndex.Equals(TSAVBean.Work.currIndex) && (TSAVBean.Work.currPoint != null))
{
ShowTSAVPoint();
}
if (TSAVBean.Work.IsLastP())
{
ShowAOI();
}
lblPause.Visible = TSAVBean.WorkPause;
}
else
{
lblPause.Visible = false;
}
}
catch (Exception ex)
{
LogUtil.error(Name + "timer_Tick 出错:" + ex.ToString());
}
}
private DateTime LastTime = DateTime.Now;
private bool isShowAOI = false;
private void ShowAOI()
{
if (isShowAOI)
{
return;
}
if (Setting_NInit.Device_AutoGuoBan)
{
LogUtil.info(" 离线编程模式,不进入AOI");
return;
}
isShowAOI = true;
try
{
//判断是否需要AOI检测
if (TSAVBean.IsNeedAOI && TSAVBean.Work.IsShowAOI.Equals(false))
{
TSAVBean.Work.IsShowAOI = true;
LogUtil.info(" 工作完成, 清理投影内容,弹出 提示框提示进入AOI检测 ,设置 AOIopen = true;");
PTipSoundProcess.AOIopen = true;
IOManager.IOMove(IOManager.Device_Led, IO_VALUE.LOW);
FrmProjectorScreen.instance.ClearPoint();
//IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.HIGH);
FrmAOICheck frm = new FrmAOICheck(CurrProject);
frm.ShowDialog();
IOManager.IOMove(IOManager.Device_Led, IO_VALUE.HIGH );
PTipSoundProcess.AOIopen = false;
LogUtil.info("AOI检测已关闭 ,设置 AOIopen = false;");
// IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.LOW);
}
else
{
TSAVBean.Work.StopWork();
}
}
catch (Exception ex)
{
LogUtil.error("显示AOI出错:" + ex.ToString());
}
isShowAOI = false;
}
private void btnStartWorking_Click(object sender, EventArgs e)
{
//bool isCy = ConfigAppSettings.GetBoolValue(Setting_Init.IsCycleDebug);
LogUtil.info(Name + "点击:" + btnStartWorking.Text + ",自动启动循环测试模式=" + Setting_NInit.Device_IsCycleDebug);
btnStartWorking.Visible = false;
//开始工作
TSAVBean.StartWork(BoardManager.CurrBoard);
WorkInfo.IsCycleDebug = Setting_NInit.Device_IsCycleDebug;
btnStartWorking.Visible = true;
}
private void FrmWelding_FormClosing(object sender, FormClosingEventArgs e)
{
//StatusClient.instance.SendNew(Color.Green, TSAVBean.Name + "退出工作状态");
FrmProjectorScreen.instance.ClearPoint();
this.timer.Enabled = false;
this.timerShowForm.Enabled = false;
if (TSAVBean.Status > TSAVStatus.Reset)
{
TSAVBean.StopWork();
}
TSAVBean.ExitWork();
StopVideo();
}
private void FormStatus()
{
btnSave.Visible = txtCount.Visible ;
btnGoHome.Visible = true;
if (TSAVBean.Status <= (TSAVStatus.Wait))
{
btnStart.Visible = true;
btnGoHome.Visible = false;
}
else
{
btnStart.Visible = false;
btnGoHome.Visible = true;
}
if (TSAVBean.OnlyGuoBan) { }
else
{
if (TSAVBean.Status.Equals(TSAVStatus.Runing) && (!TSAVBean.IsInSuddenDown))
{
btnStartWorking.Visible = !(TSAVBean.Work.IsWorking);
btnNextPoint.Visible = true;
btnPrePoint.Visible = true;
}
else
{
btnStartWorking.Visible = false;
if (Setting_NInit.App_IsDebug.Equals(false))
{
btnNextPoint.Visible = false;
btnPrePoint.Visible = false;
}
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
StartRun();
FormStatus();
}
private void btnGoHome_Click(object sender, EventArgs e)
{
//btnGoHome.Enabled = false;
if (TSAVBean.LineStep.moveType.Equals(1))
{
DialogResult result = MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureReset, "是否确定重置,重置后当前工作将结束"),
ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"), MessageBoxButtons.OKCancel);
if (!result.Equals(DialogResult.OK))
{
return;
}
}
if (TSAVBean.Status > TSAVStatus.Wait)
{
TSAVBean.StopMove();
TSAVBean.StartReset();
}
//btnGoHome.Enabled = true;
FormStatus();
}
private void btnBack_Click(object sender, EventArgs e)
{
timerShowForm.Stop();
timer.Stop();
this.Close();
}
private void FrmWork_Shown(object sender, EventArgs e)
{
try
{
SetSkin(this);
//lblMsg.ForeColor = Color.Red;
lblNotices.ForeColor = Color.White;
label2.ForeColor = Color.White;
lblComNotices.ForeColor = Color.White;
label3.ForeColor = Color.White;
this.lblPointName.ForeColor = Color.White;
label4.ForeColor = Color.White;
lblPositionNum.ForeColor = Color.White;
BoardInfo board = BoardManager.CurrBoard;
display.SetPic(picBoard, panBoard);
if (board != null)
{
if (isFirstLoad&&Setting_NInit.Device_HandsVideo)
{
display.SetPicSize(board);
updateGroupSize();
isFirstLoad = false;
}
loadPictureBoxSize(board);
StartRun();
}
FrmProjectorScreen.instance.ClearPoint();
timer.Start();
LogUtil.info(Name + " Shown end ");
WriteStateStr();
if (needShowMsg != "")
{
MessageBox.Show(needShowMsg);
}
StartVideo();
}
catch (Exception ex)
{
}
}
private int iCropErr = 0;
double defaultXxishu = 0.5;
double defaultYxishu = 0.5;
private int lineWidth = 4;
int lineLength = 80;
private void Crop(Brush color)
{
loadPictureBoxSize(BoardManager.CurrBoard);
float width = picBoard.Width / 5;
float height = picBoard.Height / 5;
float defaultX = (float)(preShowPointX - width / 2);
float defaultY = (float)(preShowPointY - width / 2);
float X = (float)(preShowPointX - width / 2);
float Y = (float)(preShowPointY - height / 2);
//判断是否xy超过范围
if (X < 0)
{
X = 0;
//width = Math.Abs((float)preShowPointX * 2);
}
if (Y < 0)
{
Y = 0;
//height = Math.Abs((float)preShowPointY * 2);
}
if (X + width > picBoard.Width)
{
X = picBoard.Width - width;
//width = (float)(picBoard.Width - preShowPointX) * 2;
//X = picBoard.Width - width;
}
if (Y + height > picBoard.Height)
{
Y = picBoard.Height - height;
//height = (float)(picBoard.Height - preShowPointY) * 2;
//Y = picBoard.Height - height;
}
//updatePicPointSize(Convert.ToInt32(width*100), Convert.ToInt32(height*100));
updatePicPointSize(picBoard.Width, picBoard.Height);
//画矩形
Graphics picBoardGra = this.picBoard.CreateGraphics();
Pen pen = new Pen(Color.Red, 3);
picBoardGra.DrawRectangle(pen, X, Y, picBoard.Width / 5, picBoard.Height / 5);
picBoardGra.Flush();
picBoardGra.Dispose();
defaultXxishu = (defaultX - (X - width / 2)) / width;
defaultYxishu = (defaultY - (Y - width / 2)) / height;
if (bitmap == null)
{
bitmap = (Bitmap)picBoard.Image;
}
X = bitmap.Width * X / picBoard.Width;
Y = bitmap.Height * Y / picBoard.Height;
width = bitmap.Width * width / picBoard.Width;
height = bitmap.Height * height / picBoard.Height;
Rectangle rec = new Rectangle((int)Math.Round(X), (int)Math.Round(Y), (int)Math.Round(width), (int)Math.Round(height));
try
{
//GC.WaitForPendingFinalizers();
this.picPoint.Image = bitmap.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//bitmap.Dispose();
}
catch (Exception ex)
{
iCropErr++;
}
finally
{
}
//int pontX = picPoint.Width / 2;
//int pontY = picPoint.Height / 2;
int pontX = Convert.ToInt32(picPoint.Width * defaultXxishu);
int pontY = Convert.ToInt32(picPoint.Height * defaultYxishu);
Graphics g = picPoint.CreateGraphics();
//g.FillEllipse(color, pontX, pontY, pointWidh, pointWidh);
lineLength = picPoint.Width < picPoint.Height ? picPoint.Width / 8 : picPoint.Height / 8;
lineWidth = lineLength / 5;
g.DrawLine(new Pen(Color.Red, lineWidth), pontX - lineWidth / 2, pontY - lineLength, pontX - lineWidth / 2, pontY + lineLength - lineWidth / 2);
g.DrawLine(new Pen(Color.Red, lineWidth), pontX - lineLength, pontY - lineWidth / 2, pontX + lineLength - lineWidth / 2, pontY - lineWidth / 2);
g.Flush();
g.Dispose();
}
private void ShowMsg()
{
this.lblPointName.Text = " 暂无";
this.lblComDes.Text = "暂无";
this.lblComName.Text = "暂无";
this.lblComNotices.Text = "暂无";
if (smtPoint == null)
{
return;
}
if (!smtPoint.PN.Equals(""))
{
lblPositionNum.Text = smtPoint.PositionNum;
lblPartNum.Text = smtPoint.TagNo;
lblPointName.Text = smtPoint.PN;
#region 2023-10-31修改
ComponetInfo com =CSVBomManager.GetCom(BoardManager.CurrBoard.bomName, smtPoint,true);
if (com != null)
{
TSAVPosition position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
if (position != null)
{
lblPositionNum.Text = position.PositionNum;
smtPoint.PositionNum = position.PositionNum;
}
lblComDes.Text = com.ComponentDes;
this.lblComName.Text = com.PN;
lblCount.Text = com.ComCount.ToString();
txtCount.Text = com.ComCount.ToString();
lblComNotices.Text = com.Notes.ToString();
if (int.Parse(com.ComCount) <= 0)
{
lblComNotices.Text = ResourceCulture.GetString("元器件数量不足,请及时补充", "元器件数量不足,请及时补充");
}
//记录运行过程的位号
Setting_NInit.Work_ProcedureName=board.boardName;
Setting_NInit.Work_TagNumber = smtPoint.pointNum;
if (smtPoint.pointNum == 1)//第一个位号不需要记录
{
Setting_NInit.Work_ProcedureName = "";
Setting_NInit.Work_TagNumber = 0;
}
#endregion
}
else
{
string msg = ResourceCulture.GetString(ResourceCulture.DataLostMsg, "数据丢失");
lblComDes.Text = msg;
this.lblComName.Text = msg;
lblCount.Text = msg;
txtCount.Text = 0 + "";
lblComNotices.Text = msg;
}
}
else
{
this.lblPositionNum.Text = ResourceCulture.GetString(ResourceCulture.NoData, "暂无");
}
}
private void timerShowForm_Tick(object sender, EventArgs e)
{
try
{
if (smtPoint != null && smtPoint.PositionX > 0 && smtPoint.PositionY > 0)
{
//ShowMsg();
Color color = Color.Yellow;
if (preIsShow)
{
color = Color.Yellow;
preIsShow = false;
}
else
{
color = Color.Red;
preIsShow = true;
}
int pontX = Convert.ToInt32(picPoint.Width * defaultXxishu);
int pontY = Convert.ToInt32(picPoint.Height * defaultYxishu);
Graphics g = picPoint.CreateGraphics();
g.DrawLine(new Pen(Color.FromArgb(255, color), lineWidth), pontX - lineWidth / 2, pontY - lineLength, pontX - lineWidth / 2, pontY + lineLength - lineWidth / 2);
g.DrawLine(new Pen(Color.FromArgb(255, color), lineWidth), pontX - lineLength, pontY - lineWidth / 2, pontX + lineLength - lineWidth / 2, pontY - lineWidth / 2);
g.Flush();
g.Dispose();
//preIsShow = false;
}
}
catch (Exception ex)
{
LogUtil.error(Name + " timerShowForm_Tick 出错:" + ex.ToString());
}
}
private int preIndex = -1;
private SMTPointInfo smtPoint = new SMTPointInfo();
private double preShowPointX = 0;
private double preShowPointY = 0;
private bool preIsShow = false;
private void btnNextPoint_Click(object sender, EventArgs e)
{
//if (!TSAVBean.Work.IsWorking)
//{
// return;
//}
if (TSAVBean.Status.Equals(TSAVStatus.Runing))
{
Next(true);
}
else
{
PreOrNext(true);
}
}
private void Next(bool isNext)
{
TSAVBean.NextPoint(isNext, false);
ShowTSAVPoint();
}
private void ShowTSAVPoint()
{
if (!TSAVBean.Work.IsWorking)
{
return;
}
preIndex = TSAVBean.Work.currIndex;
smtPoint = TSAVBean.Work.currPoint;
ShowMsg();
if (pointXMap.ContainsKey(smtPoint.pointNum))
{
preShowPointX = pointXMap[smtPoint.pointNum];
preShowPointY = pointYMap[smtPoint.pointNum];
}
else
{ //画图
int width = Convert.ToInt32(board.boardWidth);
int height = Convert.ToInt32(board.boardLength);
Graphics grfx = picBoard.CreateGraphics();
preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;
}
int leftCount = workSmtList.Count - 1 - preIndex;
if (preIndex.Equals(0))
{
lblLast.Visible = false;
lblStart.Visible = true;
}
else if (leftCount <= 0)
{
lblStart.Visible = false;
lblLast.Visible = true;
}
else
{
lblStart.Visible = false;
lblLast.Visible = false;
}
this.lblLast.ForeColor = System.Drawing.Color.Olive;
this.lblStart.ForeColor = System.Drawing.Color.Olive;
if (leftCount > 0)
{
lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg, "当前:第{0}步,剩余{1}步", (preIndex + 1).ToString(), leftCount);
string btnText = ResourceCulture.GetString(this.ClassName + "_btnNextPoint_Text", "下一步");
if (!btnNextPoint.Text.Equals(btnText))
{
btnNextPoint.Text = btnText;
}
}
else
{
lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg2, "当前:第{0}步", (preIndex + 1).ToString());
btnNextPoint.Text = ResourceCulture.GetString(ResourceCulture.NexProText, "下一个程序");
}
Crop(Brushes.Red);
if (timerShowForm.Enabled.Equals(false))
{
timerShowForm.Start();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnPrePoint_Click(object sender, EventArgs e)
{
if (TSAVBean.Status.Equals(TSAVStatus.Runing))
{
Next(false);
}
else
{
PreOrNext(false);
}
}
private void PreOrNext(bool isNext)
{
if (board != null && workSmtList.Count > 0)
{
if (isNext)
{
preIndex++;
if (preIndex < 0)
{
preIndex = 0;
}
}
else
{
preIndex--;
if (preIndex < 0)
{
preIndex = workSmtList.Count - 1;
}
}
if (workSmtList.Count <= preIndex)
{
PTipSoundProcess.PlayFile(Setting_NInit.Set_BoardEndSound);
ShowAOI();
preIndex = 0;
}
else if (preIndex < 0)
{
preIndex = workSmtList.Count - 1;
}
smtPoint = workSmtList[preIndex];
ShowMsg();
if (pointXMap.ContainsKey(smtPoint.pointNum))
{
preShowPointX = pointXMap[smtPoint.pointNum];
preShowPointY = pointYMap[smtPoint.pointNum];
}
else
{ //画图
int width = Convert.ToInt32(board.boardWidth);
int height = Convert.ToInt32(board.boardLength);
//Graphics grfx = picBoard.CreateGraphics();
preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;
}
LogUtil.debug($"投影点位:{smtPoint.PN}-{smtPoint.TagNo}-{smtPoint.PositionNum}");
FrmProjectorScreen.instance.ShowPoint(true,BoardManager.CurrBoard.PointColor, smtPoint);
#region 这段代码是不是没必要
//ComponetInfo com = CSVBomManager.GetCom(this.board.bomName, smtPoint,true);
//TSAVPosition position = null;
//if (com != null)
//{
// position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
//}
//else
//{
// position = CSVPositionReader<TSAVPosition>.GetPositonByNum(smtPoint.PositionNum);
//}
//if (position == null)
//{
// //ComponetInfo com = ComponentManager.getById(smtPoint.pointType);
// lblPositionNum.Text = ResourceCulture.GetString(ResourceCulture.ComInsufficient, "元器件库存不足");
//}
//else
//{
// #region 2023-09-22 添加 控制便签功能
// //string count = null;
// //string pnname = null;
// //string ComponentDes = null;
// //if (com != null)
// //{
// // count = com.ComCount.ToString();
// // pnname = com.PN.ToString();
// // ComponentDes = com.ComponentDes;
// //}
// ////调用指示灯显示
// //ScanRequestLabel.RequestPost(position, count, pnname, ComponentDes);
// #endregion
// lblPositionNum.Text = position.PositionNum;
//}
#endregion
int leftCount = workSmtList.Count - 1 - preIndex;
if (preIndex.Equals(0))
{
lblLast.Visible = false;
lblStart.Visible = true;
}
else if (leftCount <= 0)
{
lblStart.Visible = false;
lblLast.Visible = true;
}
else
{
lblStart.Visible = false;
lblLast.Visible = false;
}
this.lblLast.ForeColor = System.Drawing.Color.Olive;
this.lblStart.ForeColor = System.Drawing.Color.Olive;
if (leftCount > 0)
{
lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg, "当前:第{0}步,剩余{1}步", (preIndex + 1).ToString(), leftCount);
}
else
{
lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg2, "当前:第{0}步", (preIndex + 1).ToString());
}
Crop(Brushes.Red);
if (timerShowForm.Enabled.Equals(false))
{
timerShowForm.Start();
}
if (preIndex != 0)
{
PTipSoundProcess.PlayFile(Setting_NInit.Set_PointChangeSound);
}
else
{
PTipSoundProcess.PlayFile(Setting_NInit.Set_NewBoardSound);
}
}
}
private void btnStopMove_Click(object sender, EventArgs e)
{
if (TSAVBean.IsNeedAOI)
{
LogUtil.info("点击【" + btnCamera.Text + "】,清理投影内容,进入AOI检测 , 设置AOIopen = true");
IOManager.IOMove(IOManager.Device_Led, IO_VALUE.LOW);
PTipSoundProcess.AOIopen = true;
FrmProjectorScreen.instance.ClearPoint();
// IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.HIGH);
FrmAOICheck frm = new FrmAOICheck(CurrProject);
frm.defFile = "G:\\组装工作站 AOI\\img\\img\\8.bmp";
frm.ShowDialog();
PTipSoundProcess.AOIopen = false;
IOManager.IOMove(IOManager.Device_Led, IO_VALUE.HIGH);
LogUtil.info("点击【" + btnCamera.Text + "】,AOI检测已关闭 , 设置AOIopen = false");
// IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.LOW);
}
}
private void btnUpateCount_Click(object sender, EventArgs e)
{
ShowUpdateCount(true);
}
private void ShowUpdateCount(bool isShow)
{
lblCount.Visible = !isShow;
txtCount.Visible = isShow;
btnSave.Visible = isShow;
btnCancel.Visible = isShow;
//btnUpateCount.Visible = !isShow;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (TSAVBean.Work == null || TSAVBean.Work.currBoard == null)
{
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DeviceNotOkMsg, "设备未连接!"));
return;
}
if (smtPoint == null)
{
return;
}
//string comName = lblComName.Text;
int count = FormUtil.GetIntValue(txtCount);
string bomName = TSAVBean.Work.currBoard.bomName;
if (CSVBomManager.UpdateCount(bomName, smtPoint, count))
{
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.UpdateCNumOk, "元器件【{0}】的数量已更新为【{1}】", smtPoint.TagNo, count));
}
try
{
ComponetInfo com = CSVBomManager.GetCom(BoardManager.CurrBoard.bomName, smtPoint, true);
TSAVPosition position = null;
if (com != null)
{
position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
}
else
{
position = CSVPositionReader<TSAVPosition>.GetPositonByNum(smtPoint.PositionNum);
}
LabelInfo label = LedLabelController.GetLabel(position, com, true);
LedLabelController.UpdateScreen(label);
}
catch (Exception ex)
{
LogUtil.error("更改屏幕后更新电子屏出错:" + ex.ToString());
}
txtCount.Visible = false;
lblCount.Text = count.ToString();
ShowUpdateCount(false);
}
private void btnCancel_Click(object sender, EventArgs e)
{
ShowUpdateCount(false);
}
private void btnTest_Click(object sender, EventArgs e)
{
TSAVBean.StartTest();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (!isInitOk)
{
return;
}
if (TSAVBean.WorkMode.Equals(0))
{
if (radioButton2.Checked)
{
AutoVisiable(true);
TSAVBean.WorkMode = 1;
//ConfigAppSettings.SaveValue(Setting_Init.WorkMode, 1);
Setting_NInit.Work_WorkMode = 1;
LogUtil.info("切换工作模式为:自动工作");
}
else
{
AutoVisiable(false);
}
}
else if (TSAVBean.WorkMode.Equals(1))
{
if (radioButton1.Checked)
{
AutoVisiable(false);
TSAVBean.WorkMode = 0;
//ConfigAppSettings.SaveValue(Setting_Init.WorkMode, 0);
Setting_NInit.Work_WorkMode = 0;
LogUtil.info("切换工作模式为:脚踏工作");
}
else
{
AutoVisiable(true);
}
}
}
private void btnUpdateAutoTime_Click(object sender, EventArgs e)
{
int value = FormUtil.GetIntValue(txtAuToTime);
if (value <= 0)
{
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.TimeNotVailid, "输入的间隔时间无效 "),
ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
txtAuToTime.Text = TSAVBean.AuToModeSeconds.ToString();
return;
}
if (TSAVBean.AuToModeSeconds.Equals(value))
{
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.NotNeedUpdate, "时间间隔未改变,不需要更新 "), ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
return;
}
TSAVBean.AuToModeSeconds = value;
//ConfigAppSettings.SaveValue(Setting_Init.AuToModeSeconds, value);
Setting_NInit.Work_AuToModeSeconds = value;
MessageBox.Show(ResourceCulture.GetString(ResourceCulture.UpdateTOK, "已更新自动工作时间间隔为{0}秒 ", value)
, ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
}
private void btnWorkInfo_Click(object sender, EventArgs e)
{
FrmWorkCount frm = new FrmWorkCount();
frm.ShowDialog();
}
private static bool IsSet = false;
private void TSAVBean_InputCodeEvent(int count)
{
MesUtil.ClearCode();
FrmCodeInPut frmCode = new FrmCodeInPut();
frmCode.SetCount(count);
frmCode.ShowDialog();
}
private void btnCodeTest_Click(object sender, EventArgs e)
{
TSAVBean_InputCodeEvent(0);
}
private void WriteStateStr()
{
return;
//全局图(粉色表示已完成;橘色表示未开始;红色表示实时位置) 12, 327
//Graphics g = e.Graphics;
Graphics g= groupBoard.CreateGraphics();
Font f = new Font("微软雅黑", 10.5F);
string end = ResourceCulture.GetString(ResourceCulture.State_End, "已完成");
string notStart = ResourceCulture.GetString(ResourceCulture.State_NotStart, "未开始");
string postion = ResourceCulture.GetString(ResourceCulture.State_Postiion, "实时位置");
int width = groupBoard.Size.Width;
g.DrawString(end, f, Brushes.HotPink, width-260, 0);
g.DrawString(notStart, f, Brushes.Orange, width-160, 0);
g.DrawString(postion, f, Brushes.Red, width-80, 0);
//g.Dispose();
g.Flush();
g.Dispose();
}
private void btnIo_Click(object sender, EventArgs e)
{
FrmIoManager frmWorkIo = new FrmIoManager(true);
frmWorkIo.ShowDialog();
}
private void lblCount_DoubleClick(object sender, EventArgs e)
{
if (lblCount.Visible)
{
ShowUpdateCount(true);
}
}
private void picBoard_SizeChanged(object sender, EventArgs e)
{
}
private void updateGroupSize()
{
if (groupBoard.Size.Width > picBoard.Width && groupBoard.Size.Height > picBoard.Height)
{
groupBoard.Size = new Size(picBoard.Width + 6, picBoard.Height + 6);
groupBoard.Location = new Point(groupBox2.Location.X + groupBox2.Width - groupBoard.Width, groupBox2.Location.Y + groupBox2.Height - groupBoard.Height);
}
}
private void StartVideo()
{
try
{
Task.Factory.StartNew(() =>
{
VideoThreadRun();
});
}
catch(Exception ex)
{
LogUtil.error("StartVideo 出错:" + ex.ToString());
}
}
private bool mstart = false;
private string lastMsg = "";
private bool isDrawing = false;
private float xBili = 1F;
private float yBili = 1F;
private void VideoThreadRun()
{
mstart = true;
while (mstart&& picHandsVideo.Visible)
{
try
{
if (HVideoManager.lastInfo!=null&&!HVideoManager.lastInfo.ImgStr.Equals(lastMsg))
{
Bitmap bitmap = HVideoManager.GetLastImg();
if (bitmap != null)
{
if (picHandsVideo.Image != null)
{
picHandsVideo.Image.Dispose();
picHandsVideo.Image = null;
}
xBili = bitmap.Width*1F / picHandsVideo.Width;
yBili= bitmap.Height*1F/ picHandsVideo.Height;
picHandsVideo.Image = CropImage(bitmap, selectedRectangle);
}
string curr = (HandRecordManager.currHasPoint ? ResourceCulture.GetString("handWork","组装中" ):"");
//lblHandInfo.ForeColor = Color.Cyan;
lblHandInfo.Text = $"{ResourceCulture.GetString("handCount","次数")}: {HandRecordManager.recordCount},\t" +
$" {ResourceCulture.GetString("handTime", "总时长")}:{FormUtil.GetSpanStr(HandRecordManager.recordSpan)} \t {curr}";
}
Thread.Sleep(200);
}
catch (Exception ex)
{
LogUtil.error("VideoThreadRun 出错:" + ex.ToString());
}
finally
{
}
}
LogUtil.info("主线程已退出.");
}
private void StopVideo()
{
try
{
mstart = false;
}
catch (Exception ex)
{
LogUtil.error("StopVideo 出错:" + ex.ToString());
}
}
private bool cutImg = false;
private Rectangle selectedRectangle;
private void picHandsVideo_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 记录选中的框
selectedRectangle = new Rectangle(e.X, e.Y, 0, 0);
picHandsVideo.Invalidate();
isDrawing = true;
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
cutImg = false;
selectedRectangle = new Rectangle(0,0,0,0);
}
private void picHandsVideo_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
// 更新选中的框大小
selectedRectangle.Width = e.X - selectedRectangle.X;
selectedRectangle.Height = e.Y - selectedRectangle.Y;
picHandsVideo.Invalidate();
}
}
private Bitmap CropImage(Bitmap image, Rectangle cropRectangle)
{
if (cropRectangle == null || cropRectangle.IsEmpty || cropRectangle.Width <= 0 || cropRectangle.Height <= 0 || isDrawing || (!cutImg))
{
return (Bitmap)image;
}
int nWidth = (int)(cropRectangle.Width * xBili);
int yHeight = (int)(cropRectangle.Height * yBili);
int sX=(int)(cropRectangle.X*xBili) ;
int sY=(int)(cropRectangle.Y*yBili );
Rectangle newRect = new Rectangle(sX, sY, nWidth, yHeight);
Bitmap croppedImage = new Bitmap(nWidth, yHeight);
using (Graphics g = Graphics.FromImage(croppedImage))
{
g.DrawImage(image, new Rectangle(0, 0, nWidth, yHeight), newRect, GraphicsUnit.Pixel);
}
return croppedImage;
}
private void picHandsVideo_MouseUp(object sender, MouseEventArgs e)
{
base.OnMouseUp(e);
try
{
isDrawing = false;
if (e.Button == MouseButtons.Left)
{
if (selectedRectangle == null || selectedRectangle.IsEmpty || selectedRectangle.Width <= 0 || selectedRectangle.Height <= 0)
{
cutImg = false;
}
else
{
cutImg = true;
}
if (picHandsVideo.Image != null)
{
Image img = picHandsVideo.Image;
if (img != null)
{
picHandsVideo.Image = CropImage((Bitmap)img, selectedRectangle);
}
}
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
private void picHandsVideo_Paint(object sender, PaintEventArgs e)
{
if (isDrawing)
{
if (selectedRectangle == null || selectedRectangle.IsEmpty || selectedRectangle.Width <= 0 || selectedRectangle.Height <= 0)
{
return;
}
// 绘制白色边框
using (Pen pen = new Pen(Color.White, 2))
{
e.Graphics.DrawRectangle(pen, selectedRectangle);
}
}
}
}
}