Form8.cs
45.5 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
using AGVMapDemo.DemoModel;
using CtuDeviceLib;
using DeviceLibrary;
using log4net;
using Mushiny;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using zhouchen.chart;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace TheMachine.Model
{
public partial class Form8 : Form
{
private CTUPointCode _agvPosition;
private List<CTUPointCode> _pathPoints = new List<CTUPointCode>();
float ratio = 0.019f;//比例
float addreX = 50;//整体偏移X
float addreY = 50;//整体偏移Y
//新的缩放方法参数
float maxPointX = float.MinValue;
float maxPointY = float.MinValue;
float minPointX = float.MaxValue;
float minPointY = float.MaxValue;
float RangX = float.MaxValue;
float RangY = float.MaxValue;
int screenWidth = 1920;
int screenHeight = 1080;
int screenRatio = 2;
List<CTURobot> cTUs = new List<CTURobot>();
List<TweenAnimationModel> cTUs_Animation = new List<TweenAnimationModel>();
object Lock_Update_CTU = new object();
object Lock_Update_CTUUI = new object();
float CTU_Width = 50f;
System.Timers.Timer CTU_timer = new System.Timers.Timer();
System.Timers.Timer CTU_test_timer = new System.Timers.Timer();
System.Timers.Timer CTU_Update_timer = new System.Timers.Timer();
List<PointCode> PointCodes = new List<PointCode>();
Bitmap MapBmp;
BitmapPool DrawMapBmp;
float distanceRatio = 0.04f;
float markRatio = 5.0f;
int markFontRatio = 6;
float LineRatio = 2.0f;
float CTULineRatio = 2.0f;
int CTUFontRatio = 12;
event EventHandler<Bitmap> setimage;
Graphics mapDraw = null;
Graphics CTUDraw = null;
//箭头
// 角度A(以度为单位)
float angleA = 135;
// 长度B
float lengthB = 5;
Dictionary<uint, CTUPointCode> CTUMap = new Dictionary<uint, CTUPointCode>();
#region 测试
List<uint> planway = new List<uint>() { 427, 525, 3001, 3002, 3003, 3004,
3005, 3006, 3007, 3008, 3009, 3010, 3011, 3012,
3013, 3014, 3015, 3016, 3017, 3018, 3019, 3020, 539, 487 };
List<uint> currentway = new List<uint>() { 427, 525, 3001, 3002, 3003, 3004 };
List<uint> occupyway = new List<uint>() { 427, 525, 3001 };
Color ctuclor = Color.Red;
Color planclor = Color.Orange;
Color currentclor = Color.Yellow;
Color occupyclor = Color.Green;
int inedx = 0;
private void CTU_test_timer_Tick(object sender, EventArgs e)
{
//if (inedx > planway.Count - 2)
//{
// inedx = 0;
//}
//else
//{
// inedx++;
// occupyway.RemoveAt(0);
// occupyway.Add(planway[inedx]);
// Update_CTU(3037, planway, currentway, occupyway, ctuclor, planclor, currentclor, occupyclor, "正常");
//}
}
#endregion
/// <summary>
/// 更新单个小车位置信息
/// </summary>
/// <param name="Id">小车ID</param>
/// <param name="PlanWay">小车完整路径</param>
/// <param name="CurrentWay">小车当前阶段运行路径</param>
/// <param name="OccupyPoint">小车占用地标</param>
/// <param name="CTUColor">小车颜色</param>
/// <param name="PlanWayColor">小车计划路径颜色</param>
/// <param name="CurrentWayColor">小车当前路径颜色</param>
/// <param name="OccupyPointColor">小车占用地标颜色</param>
/// <param name="state">小车状态</param>
public void Update_CTU(uint Id, List<uint> PlanWay, List<uint> CurrentWay, List<uint> OccupyPoint,
Color CTUColor, Color PlanWayColor, Color CurrentWayColor, Color OccupyPointColor, string state)
{
lock (Lock_Update_CTU)
{
if (cTUs.Count > 0)
{
var ctu = cTUs.Find(m => m.Id == Id);
if (ctu != null)
{
ctu.PlanWay.Clear();
ctu.CurrentWay.Clear();
ctu.OccupyPoint.Clear();
ctu.PlanWay = ConvertToCTUPointCode(PlanWay);
ctu.CurrentWay = ConvertToCTUPointCode(CurrentWay);
ctu.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
ctu.CTUColor = CTUColor;
ctu.CTUPlanWayColor = PlanWayColor;
ctu.CTUCurrentWayColor = CurrentWayColor;
ctu.CTUOccupyPointColor = OccupyPointColor;
}
var ctu_Animation = cTUs_Animation.Find(m => m.CTUID == Id);
if (ctu_Animation != null)
{
ctu_Animation.PrePoint = ctu_Animation.CurrPoint;
ctu_Animation.PreTime = ctu_Animation.CurrTime;
ctu_Animation.CurrPoint = ctu.OccupyPoint[OccupyPoint.Count / 2 - 1];
ctu_Animation.CurrTime = DateTime.Now;
}
}
else
{
CTURobot cTU = new CTURobot();
cTU.Id = Id;
cTU.PlanWay = ConvertToCTUPointCode(PlanWay);
cTU.CurrentWay = ConvertToCTUPointCode(CurrentWay);
cTU.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
cTU.CTUColor = CTUColor;
cTU.CTUPlanWayColor = PlanWayColor;
cTU.CTUCurrentWayColor = CurrentWayColor;
cTU.CTUOccupyPointColor = OccupyPointColor;
cTUs.Add(cTU);
TweenAnimationModel tween = new TweenAnimationModel();
tween.CTUID = Id;
tween.CurrTime = DateTime.Now;
tween.CurrPoint = cTU.OccupyPoint[OccupyPoint.Count / 2 - 1];
cTUs_Animation.Add(tween);
}
}
}
/// <summary>
/// 更新单个小车位置信息
/// </summary>
/// <param name="name">小车名称</param>
/// <param name="PlanWay">小车完整路径</param>
/// <param name="CurrentWay">小车当前阶段运行路径</param>
/// <param name="OccupyPoint">小车占用地标</param>
/// <param name="CTUColor">小车颜色</param>
/// <param name="PlanWayColor">小车计划路径颜色</param>
/// <param name="CurrentWayColor">小车当前路径颜色</param>
/// <param name="OccupyPointColor">小车占用地标颜色</param>
/// <param name="state">小车状态</param>
public void Update_CTU(string name, uint curPos, List<uint> PlanWay, List<uint> CurrentWay, List<uint> OccupyPoint,
Color CTUColor, Color PlanWayColor, Color CurrentWayColor, Color OccupyPointColor, string state)
{
lock (Lock_Update_CTU)
{
if (cTUs.Count > 0)
{
var ctu = cTUs.Find(m => name.Equals(m.Name));
if (ctu != null)
{
ctu.PlanWay.Clear();
ctu.CurrentWay.Clear();
ctu.OccupyPoint.Clear();
ctu.CurrentPoint = null;
ctu.CurrentPoint = ConvertToCTUPointCode(curPos);
ctu.PlanWay = ConvertToCTUPointCode(PlanWay);
ctu.CurrentWay = ConvertToCTUPointCode(CurrentWay);
ctu.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
ctu.CTUColor = CTUColor;
ctu.CTUPlanWayColor = PlanWayColor;
ctu.CTUCurrentWayColor = CurrentWayColor;
ctu.CTUOccupyPointColor = OccupyPointColor;
}
else
{
CTURobot cTU = new CTURobot();
cTU.Name = name;
cTU.CurrentPoint = ConvertToCTUPointCode(curPos);
cTU.PlanWay = ConvertToCTUPointCode(PlanWay);
cTU.CurrentWay = ConvertToCTUPointCode(CurrentWay);
cTU.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
cTU.CTUColor = CTUColor;
cTU.CTUPlanWayColor = PlanWayColor;
cTU.CTUCurrentWayColor = CurrentWayColor;
cTU.CTUOccupyPointColor = OccupyPointColor;
cTUs.Add(cTU);
}
}
else
{
CTURobot cTU = new CTURobot();
cTU.Name = name;
cTU.CurrentPoint = ConvertToCTUPointCode(curPos);
cTU.PlanWay = ConvertToCTUPointCode(PlanWay);
cTU.CurrentWay = ConvertToCTUPointCode(CurrentWay);
cTU.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
cTU.CTUColor = CTUColor;
cTU.CTUPlanWayColor = PlanWayColor;
cTU.CTUCurrentWayColor = CurrentWayColor;
cTU.CTUOccupyPointColor = OccupyPointColor;
cTUs.Add(cTU);
}
}
}
CTUPointCode ConvertToCTUPointCode(uint pointCode)
{
CTUPointCode cTU = null;
if (CTUMap.TryGetValue(pointCode, out cTU))
{
return cTU;
}
else
{
// LogUtil.MapLog.Error($"传入的地码ID:【{pointCode.ToString()}】未包含在生成的地图内");
return null;
}
}
List<CTUPointCode> ConvertToCTUPointCode(List<uint> pointCodes)
{
CTUPointCode cTU;
List<CTUPointCode> cTUs = new List<CTUPointCode>();
foreach (var pathPoint in pointCodes)
{
if (CTUMap.TryGetValue(pathPoint, out cTU))
{
cTUs.Add(cTU);
}
}
return cTUs;
}
private PointF RotateVector(PointF vector, float angleDegrees)
{
float angleRadians = (float)(angleDegrees * Math.PI / 180.0);
float cosTheta = (float)Math.Cos(angleRadians);
float sinTheta = (float)Math.Sin(angleRadians);
float newX = vector.X * cosTheta - vector.Y * sinTheta;
float newY = vector.X * sinTheta + vector.Y * cosTheta;
return new PointF(newX, newY);
}
private void DrwaMap(Graphics g)
{
#region 绘制地图
//this.Invoke(new Action(UpdateUI));
// 重新绘制路径和AGV位置
//Graphics g = panel1.CreateGraphics();
Pen pen = new Pen(Color.Blue, LineRatio);
Brush brushpoint = new SolidBrush(Color.Blue);
Brush brushTurningpoint = new SolidBrush(Color.Green);
Brush brush = new SolidBrush(Color.Red);
// 绘制地标点
for (int i = 1; i < _pathPoints.Count; i++)
{
if (_pathPoints[i].Type == LandmarkType.Turning)
{
g.FillRectangle(brushTurningpoint, _pathPoints[i].X, _pathPoints[i].Y, markRatio, markRatio);
//文字
// 设置字体和笔刷
Font font = new Font("Arial", markFontRatio);
Brush CTUNameBrush = Brushes.Black;
// 要显示的文字
string CTUNameText = _pathPoints[i].Id.ToString();
PointF location = new PointF(_pathPoints[i].X + 10, _pathPoints[i].Y);
// 绘制文字
g.DrawString(CTUNameText, font, CTUNameBrush, location);
// 释放字体资源(可选,如果使用IDisposable的对象,最好进行释放)
font.Dispose();
}
else
{
g.FillEllipse(brushpoint, _pathPoints[i].X, _pathPoints[i].Y, markRatio, markRatio);
//文字
// 设置字体和笔刷
Font font = new Font("Arial", markFontRatio);
Brush CTUNameBrush = Brushes.Black;
// 要显示的文字
string CTUNameText = _pathPoints[i].Id.ToString();
PointF location = new PointF(_pathPoints[i].X + 10, _pathPoints[i].Y);
// 绘制文字
g.DrawString(CTUNameText, font, CTUNameBrush, location);
// 释放字体资源(可选,如果使用IDisposable的对象,最好进行释放)
font.Dispose();
}
}
//绘制路径
//将原有坐标点转换为图论的坐标点
List<Point> points = new List<Point>();
foreach (CTUPointCode ctuPoint in _pathPoints)
{
Point point = new Point(ctuPoint.X, ctuPoint.Y, ctuPoint.Id);
//左
{
if (ctuPoint.Left != null)
{
uint id = ctuPoint.Left.Id;
point.LeftID = id;
}
}
//右
{
if (ctuPoint.Right != null)
{
uint id = ctuPoint.Right.Id;
point.RightID = id;
}
}
//上
{
if (ctuPoint.Above != null)
{
uint id = ctuPoint.Above.Id;
point.UpID = id;
}
}
//下
{
if (ctuPoint.Below != null)
{
uint id = ctuPoint.Below.Id;
point.DownID = id;
}
}
points.Add(point);
}
//为图的坐标点确定上下左右的点
foreach (var point in points)
{
point.Up = points.Find(m => m.Id == point.UpID);
point.Down = points.Find(m => m.Id == point.DownID);
point.Left = points.Find(m => m.Id == point.LeftID);
point.Right = points.Find(m => m.Id == point.RightID);
}
// 创建GraphTraversal实例并执行遍历
GraphTraversal traversal = new GraphTraversal();
traversal.Traverse(points[0]); // 从点(0, 0)开始遍历
// 输出所有的边
// 用于存储单向联通的边
List<(Point, Point)> oneWayEdges = new List<(Point, Point)>();
// 用于存储双向联通的边
List<(Point, Point)> twoWayEdges = new List<(Point, Point)>();
oneWayEdges = traversal.oneWayEdges;
twoWayEdges = traversal.twoWayEdges;
//绘制单向边
Pen penOneWay = new Pen(Color.Red, LineRatio);
foreach (var point in oneWayEdges)
{
g.DrawLine(penOneWay, point.Item1.X, point.Item1.Y, point.Item2.X, point.Item2.Y);
// 计算P1P2的方向向量
PointF direction = new PointF(point.Item2.X - point.Item1.X, point.Item2.Y - point.Item1.Y);
// 计算旋转后的向量(角度A需要转换为弧度)
PointF rotatedDirectionA = RotateVector(direction, angleA);
PointF rotatedDirectionB = RotateVector(direction, -angleA);
// 规范化旋转后的向量,并乘以长度B得到终点坐标
float magnitudeA = (float)Math.Sqrt(rotatedDirectionA.X * rotatedDirectionA.X + rotatedDirectionA.Y * rotatedDirectionA.Y);
float magnitudeB = (float)Math.Sqrt(rotatedDirectionB.X * rotatedDirectionB.X + rotatedDirectionB.Y * rotatedDirectionB.Y);
PointF normalizedRotatedDirectionA = new PointF(rotatedDirectionA.X / magnitudeA, rotatedDirectionA.Y / magnitudeA);
PointF normalizedRotatedDirectionB = new PointF(rotatedDirectionB.X / magnitudeB, rotatedDirectionB.Y / magnitudeB);
PointF endPointA = new PointF(point.Item2.X + normalizedRotatedDirectionA.X * lengthB, point.Item2.Y + normalizedRotatedDirectionA.Y * lengthB);
PointF endPointB = new PointF(point.Item2.X + normalizedRotatedDirectionB.X * lengthB, point.Item2.Y + normalizedRotatedDirectionB.Y * lengthB);
// 绘制线段
g.DrawLine(penOneWay, point.Item2.X, point.Item2.Y, endPointA.X, endPointA.Y);
g.DrawLine(penOneWay, point.Item2.X, point.Item2.Y, endPointB.X, endPointB.Y);
//g.DrawLine(penOneWay, point.Item2.X, point.Item2.Y, (point.Item2.X + point.Item1.X) / 2, point.Item2.Y + Math.Abs(point.Item2.X - point.Item1.X) / 2);
//g.DrawLine(penOneWay, point.Item2.X, point.Item2.Y, (point.Item2.X + point.Item1.X) / 2, point.Item2.Y - Math.Abs(point.Item2.X - point.Item1.X) / 2);
}
//绘制双向边
Pen penTwoWay = new Pen(Color.Green, LineRatio);
foreach (var point in twoWayEdges)
{
g.DrawLine(penTwoWay, point.Item1.X, point.Item1.Y, point.Item2.X, point.Item2.Y);
}
#endregion
g.Save();
}
private void UpdateCTU(Graphics g)
{
try
{
lock (Lock_Update_CTU)
{
#region 绘制AGV小车,路线
foreach (var ctu in cTUs)
{
if (ctu.CurrentPoint == null)
{
continue;
}
//if (ctu.PlanWay.Count == 0 || ctu.CurrentWay.Count == 0 || ctu.OccupyPoint.Count == 0 || ctu.CTUPlanWayColor == null || ctu.CTUCurrentWayColor == null || ctu.CTUOccupyPointColor == null)
//{
// continue;
//}
//小车(以固定半透明框代表)
Color semiTransparentColor = Color.FromArgb(128, ctu.CTUColor); // 128是透明度(0-255),后面是RGB颜色值
Brush semiTransparentBrush = new SolidBrush(semiTransparentColor);
var CTUPoint = ctu.CurrentPoint;
g.FillRectangle(semiTransparentBrush, CTUPoint.X - CTU_Width / 2, CTUPoint.Y - CTU_Width / 2, CTU_Width, CTU_Width);
//文字
// 设置字体和笔刷
Font font = new Font("Arial", CTUFontRatio);
Brush CTUNameBrush = Brushes.Black;
// 要显示的文字
string CTUNameText = ctu.Name.ToString();
// 测量文字的大小以获取其位置
//SizeF textSize = g.MeasureString(CTUNameText, font);
//PointF location = new PointF((panel1.Width - textSize.Width) / 2, (panel1.Height - textSize.Height) / 2);
PointF location = new PointF(CTUPoint.X, CTUPoint.Y);
// 绘制文字
g.DrawString(CTUNameText, font, CTUNameBrush, location);
// 释放字体资源(可选,如果使用IDisposable的对象,最好进行释放)
font.Dispose();
#region 目前不显示路径
////计划路径
//if (ctu.PlanWay != null)
//{
// Pen PlanWay = new Pen(ctu.CTUPlanWayColor, CTULineRatio);
// for (int i = 1; i < ctu.PlanWay.Count; i++)
// {
// g.DrawLine(PlanWay, ctu.PlanWay[i - 1].X, ctu.PlanWay[i - 1].Y, ctu.PlanWay[i].X, ctu.PlanWay[i].Y);
// }
//}
////当前路径
//if (ctu.CurrentWay != null)
//{
// Pen CurrentWay = new Pen(ctu.CTUCurrentWayColor, CTULineRatio);
// for (int i = 1; i < ctu.CurrentWay.Count; i++)
// {
// g.DrawLine(CurrentWay, ctu.CurrentWay[i - 1].X, ctu.CurrentWay[i - 1].Y, ctu.CurrentWay[i].X, ctu.CurrentWay[i].Y);
// }
//}
////占用路径
//#region 新的路径显示方法
//{
// //绘制路径
// //将原有坐标点转换为图论的坐标点
// List<Point> points = new List<Point>();
// foreach (CTUPointCode ctuPoint in ctu.OccupyPoint)
// {
// Point point = new Point(ctuPoint.X, ctuPoint.Y, ctuPoint.Id);
// //左
// {
// if (ctuPoint.Left != null)
// {
// uint id = ctuPoint.Left.Id;
// point.LeftID = id;
// }
// }
// //右
// {
// if (ctuPoint.Right != null)
// {
// uint id = ctuPoint.Right.Id;
// point.RightID = id;
// }
// }
// //上
// {
// if (ctuPoint.Above != null)
// {
// uint id = ctuPoint.Above.Id;
// point.UpID = id;
// }
// }
// //下
// {
// if (ctuPoint.Below != null)
// {
// uint id = ctuPoint.Below.Id;
// point.DownID = id;
// }
// }
// points.Add(point);
// }
// //为图的坐标点确定上下左右的点
// foreach (var point in points)
// {
// point.Up = points.Find(m => m.Id == point.UpID);
// point.Down = points.Find(m => m.Id == point.DownID);
// point.Left = points.Find(m => m.Id == point.LeftID);
// point.Right = points.Find(m => m.Id == point.RightID);
// }
// //输出所有的边
// // 用于存储单向联通的边
// List<(Point, Point)> oneWayEdges = new List<(Point, Point)>();
// foreach (var point in points)
// {
// if (point.Up != null && !oneWayEdges.Contains((point.Up, point)))
// {
// oneWayEdges.Add((point, point.Up));
// }
// if (point.Down != null && !oneWayEdges.Contains((point.Down, point)))
// {
// oneWayEdges.Add((point, point.Down));
// }
// if (point.Left != null && !oneWayEdges.Contains((point.Left, point)))
// {
// oneWayEdges.Add((point, point.Left));
// }
// if (point.Right != null && !oneWayEdges.Contains((point.Right, point)))
// {
// oneWayEdges.Add((point, point.Right));
// }
// }
// //绘制边
// Pen OccupyWay = new Pen(ctu.CTUOccupyPointColor, CTULineRatio);
// foreach (var point in oneWayEdges)
// {
// g.DrawLine(OccupyWay, point.Item1.X, point.Item1.Y, point.Item2.X, point.Item2.Y);
// }
//}
//#endregion
////if (ctu.OccupyPoint != null)
////{
//// Pen OccupyWay = new Pen(ctu.CTUOccupyPointColor, CTULineRatio);
//// for (int i = 1; i < ctu.OccupyPoint.Count; i++)
//// {
//// g.DrawLine(OccupyWay, ctu.OccupyPoint[i - 1].X, ctu.OccupyPoint[i - 1].Y, ctu.OccupyPoint[i].X, ctu.OccupyPoint[i].Y);
//// }
////}
#endregion
}
#endregion
}
}
catch (Exception ex)
{
LogUtil.MapLog.Error($"AGV运动画面出错:【{ex}】");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lock (Lock_Update_CTUUI)
{
//if (cTUs != null && mapDraw != null && MapBmp != null)
//{
// using (Bitmap DrawMapBmp = MapBmp.Clone() as Bitmap)
// {
// //Bitmap DrawMapBmp = MapBmp.Clone() as Bitmap;
// CTUDraw = Graphics.FromImage(DrawMapBmp);
// // 设置绘图品质
// CTUDraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// CTUDraw.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// // 绘制背景,填充为白色
// UpdateCTU(CTUDraw);
// setimage?.Invoke(this, DrawMapBmp);
// }
//}
if (cTUs != null && mapDraw != null && MapBmp != null)
{
var bitmap1 = DrawMapBmp.GetBitmap();
bitmap1 = MapBmp.Clone() as Bitmap;
//Bitmap DrawMapBmp = MapBmp.Clone() as Bitmap;
using (Graphics CTUDraw = Graphics.FromImage(bitmap1))
{
//CTUDraw = Graphics.FromImage(bitmap1);
// 设置绘图品质
CTUDraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//CTUDraw.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
CTUDraw.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
// 绘制背景,填充为白色
UpdateCTU(CTUDraw);
setimage?.Invoke(this, bitmap1);
DrawMapBmp.ReturnBitmap(bitmap1);
CTUDraw.Dispose();
}
//CTUDraw.Dispose();
//CTUDraw = null;
}
//优化思路:用变量存储下DrawMapBmp,
//if (cTUs != null && mapDraw != null && MapBmp != null)
//{
// //Bitmap DrawMapBmp = MapBmp.Clone() as Bitmap;
// //CTUDraw = Graphics.FromImage(DrawMapBmp);
// // 设置绘图品质
// //CTUDraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// //CTUDraw.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// // 绘制背景,填充为白色
// testDrawMapBmp = MapBmp.Clone() as Bitmap;
// testCTUDraw = Graphics.FromImage(testDrawMapBmp);
// UpdateCTU(testCTUDraw);
// setimage?.Invoke(this, testDrawMapBmp);
//}
}
}
Bitmap testDrawMapBmp = null;
Graphics testCTUDraw = null;
public static void Init()
{
MushinyManager.Init();
}
/// <summary>
/// 加工坐标点
/// </summary>
/// <param name="point">输入点</param>
/// <param name="ratio">缩放比例</param>
/// <param name="overturn">是否翻转</param>
/// <param name="addreX">X轴偏移量</param>
/// <param name="addreY">Y轴偏移量</param>
/// <returns></returns>
CTUPointCode PointProcess(CTUPointCode point, float ratio = 1, bool overturn = true, float addreX = 0, float addreY = 0)
{
float x = point.X;
float y = point.Y;
if (overturn)
{
point.X = y * ratio + addreY;
point.Y = x * ratio + addreX;
}
else
{
point.X = x * ratio + addreX;
point.Y = y * ratio + addreY;
}
return point;
}
private void AlgoMatch_setimage(object sender, Bitmap e)
{
Bitmap temp = e.Clone() as Bitmap;
this.pictureBox1.Invoke(new Action(() =>
{
pictureBox1.Image = temp;
}));
}
public Form8()
{
InitializeComponent();
Init();
CTU_test_timer.Enabled = true;
CTU_test_timer.Elapsed += CTU_test_timer_Tick;
CTU_test_timer.Interval = 50;
CTU_test_timer.Start();
}
private void Form3_Load(object sender, EventArgs e)
{
#region 对于加载的地码重新计算坐标
PointCode tempPoint;
foreach (var pathPoint in LandMarks.GetPointCodes())
{
tempPoint = new PointCode()
{
Above = pathPoint.Above,
Below = pathPoint.Below,
Id = pathPoint.Id,
Name = pathPoint.Name,
Type = pathPoint.Type,
CanTurning = pathPoint.CanTurning,
Left = pathPoint.Left,
Right = pathPoint.Right,
X = pathPoint.X,
Y = pathPoint.Y,
Reserved = pathPoint.Reserved
};
// 更新路径点
PointCodes.Add(tempPoint);
}
List<Point_CTU> points = new List<Point_CTU>();
foreach (PointCode ctuPoint in PointCodes)
{
Point_CTU point = new Point_CTU(ctuPoint.X, ctuPoint.Y, ctuPoint.Id);
#region
//左
{
if (ctuPoint.Left != null)
{
uint id = ctuPoint.Left.Id;
point.LeftID = id;
point.LeftDistance = ctuPoint.Left.Distance;
}
}
//右
{
if (ctuPoint.Right != null)
{
uint id = ctuPoint.Right.Id;
point.RightID = id;
point.RightDistance = ctuPoint.Right.Distance;
}
}
//上
{
if (ctuPoint.Above != null)
{
uint id = ctuPoint.Above.Id;
point.UpID = id;
point.UpDistance = ctuPoint.Above.Distance;
}
}
//下
{
if (ctuPoint.Below != null)
{
uint id = ctuPoint.Below.Id;
point.DownID = id;
point.DownDistance = ctuPoint.Below.Distance;
}
}
#endregion
points.Add(point);
}
//为图的坐标点确定上下左右的点
foreach (var point in points)
{
point.Up = points.Find(m => m.Id == point.UpID);
point.Down = points.Find(m => m.Id == point.DownID);
point.Left = points.Find(m => m.Id == point.LeftID);
point.Right = points.Find(m => m.Id == point.RightID);
}
// 创建GraphTraversal实例并执行遍历
GraphTraversal_CTU traversal = new GraphTraversal_CTU();
traversal.Traverse(points.First(m => m.Id == 617)); // 从点(0, 0)开始遍历
//更新原始地码的坐标
foreach (var point in traversal.PointCodes)
{
PointCodes.Find(m => m.Id == point.Id).X = point.X;
PointCodes.Find(m => m.Id == point.Id).Y = point.Y;
}
#endregion
foreach (var pathPoint in PointCodes)
{
_agvPosition = new CTUPointCode()
{
Above = pathPoint.Above,
Below = pathPoint.Below,
Id = pathPoint.Id,
Name = pathPoint.Name,
Type = pathPoint.Type,
CanTurning = pathPoint.CanTurning,
Left = pathPoint.Left,
Right = pathPoint.Right,
X = pathPoint.X,
Y = pathPoint.Y,
Reserved = pathPoint.Reserved
};
// 更新路径点
_pathPoints.Add(PointProcess(_agvPosition, distanceRatio, false, addreX, addreY));
}
foreach (var point in _pathPoints)
{
if (maxPointX < point.X)
{
maxPointX = point.X;
}
if (maxPointY < point.Y)
{
maxPointY = point.Y;
}
if (minPointX > point.X)
{
minPointX = point.X;
}
if (minPointY > point.Y)
{
minPointY = point.Y;
}
}
RangX = maxPointX - minPointX;
RangY = maxPointY - minPointY;
//修正坐标为正数
#region 修正坐标为正数
List<CTUPointCode> tempPathPoints = new List<CTUPointCode>();
foreach (var point in _pathPoints)
{
tempPathPoints.Add(PointProcess(point, 1, false, -minPointX + addreX, -minPointY + addreY));
}
_pathPoints.Clear();
_pathPoints = tempPathPoints;
#endregion
maxPointX = float.MinValue;
maxPointY = float.MinValue;
minPointX = float.MaxValue;
minPointY = float.MaxValue;
RangX = float.MaxValue;
RangY = float.MaxValue;
foreach (var point in _pathPoints)
{
if (maxPointX < point.X)
{
maxPointX = point.X;
}
if (maxPointY < point.Y)
{
maxPointY = point.Y;
}
if (minPointX > point.X)
{
minPointX = point.X;
}
if (minPointY > point.Y)
{
minPointY = point.Y;
}
}
#region 按Y轴镜像
List<CTUPointCode> tempPathPointsY = new List<CTUPointCode>();
foreach (var point in _pathPoints)
{
point.Y = (maxPointY) - point.Y + addreY;
tempPathPointsY.Add(point);
}
_pathPoints.Clear();
_pathPoints = tempPathPointsY;
#endregion
#region 处理好的点加载进地图字典
foreach (var point in _pathPoints)
{
CTUMap.Add(point.Id, point);
}
#endregion
MapBmp = new Bitmap(Convert.ToInt32(maxPointX + addreX), Convert.ToInt32(maxPointY + addreY), PixelFormat.Format32bppRgb);
DrawMapBmp = new BitmapPool(25, MapBmp.Width, MapBmp.Height);
mapDraw = Graphics.FromImage(MapBmp);
// 设置绘图品质
mapDraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
mapDraw.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// 绘制背景,填充为白色
mapDraw.Clear(Color.White);
DrwaMap(mapDraw);
setimage += AlgoMatch_setimage;
// 保存图像到文件
MapBmp.Save("output.png", System.Drawing.Imaging.ImageFormat.Png);
pictureBox1.Size = new Size(MapBmp.Width, MapBmp.Height);
testDrawMapBmp= MapBmp.Clone() as Bitmap;
testCTUDraw = Graphics.FromImage(testDrawMapBmp);
setimage?.Invoke(this, MapBmp);
CTU_timer.Enabled = true;
CTU_timer.Elapsed += timer1_Tick;
CTU_timer.Interval = 200;
CBCTU.Enabled = false;
CBSpeed.Enabled = false;
BTStart.Enabled = false;
BTStop.Enabled = false;
//CTUtable.Columns.Add("CTUID", typeof(uint));
//CTUtable.Columns.Add("PointCode", typeof(uint));
//CTUtable.Columns.Add("Time", typeof(DateTime));
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
System.Environment.Exit(0);
DrawMapBmp.ClearPool();
}
//DataTable CTUtable = new DataTable("CTUTable");
List<CTUWayPoint> cTUWayPoints = new List<CTUWayPoint>();
static string dateformat = "yyyy-MM-dd HH:mm:ss,fff";
List<string> CTULists = new List<string>();
private int currentIndex = 0;
private float speedMultiplier = 1.0f;
private DateTime lastTime;
List<CTUWayPoint> displayPoint = new List<CTUWayPoint>();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择一个文本文件";
openFileDialog.Filter = "日志文件 (*.log)|*.log|所有文件 (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
var fs = File.OpenRead(filePath);
var sr = new StreamReader(fs);
int ctupre, ctulate, pointlate;
while (true)
{
var t = sr.ReadLine();
if (t == null)
{
if (CTULists.Count > 1)
{
CTULists.Add("全部");
}
CBCTU.DataSource = CTULists;
break;
}
ctupre = t.IndexOf("INFO 【");
ctulate = t.IndexOf("】当前位置【");
pointlate = t.IndexOf("】状态【");
if (ctupre > -1 && ctulate > -1 && pointlate > -1)
{
string time = t.Substring(1, 23);
string CTU = t.Substring(ctupre + 7, ctulate - ctupre - 7);
string currPoint = t.Substring(ctulate + 6, pointlate - ctulate - 6);
//DataRow row = CTUtable.NewRow();
//row["CTUID"] = uint.Parse(CTU);
//row["PointCode"] = uint.Parse(currPoint);
//row["Time"] = DateTime.ParseExact(time, dateformat, CultureInfo.InvariantCulture, DateTimeStyles.None);
//CTUtable.Rows.Add(row);
CTUWayPoint temp = new CTUWayPoint();
temp.CTUID = uint.Parse(CTU);
temp.PointCode = uint.Parse(currPoint);
temp.Time = DateTime.ParseExact(time, dateformat, CultureInfo.InvariantCulture, DateTimeStyles.None);
cTUWayPoints.Add(temp);
if (!CTULists.Contains(CTU))
{
CTULists.Add(CTU);
}
}
}
CBCTU.Enabled = true;
CBSpeed.Enabled = true;
BTStart.Enabled = true;
}
}
bool is_pause=true;
private void BTStart_Click(object sender, EventArgs e)
{
if (is_pause)
{
is_pause = false;
BTStop.Enabled = true;
button1.Enabled = false;
CBCTU.Enabled = false;
BTStart.Text = "暂停";
setimage?.Invoke(this, MapBmp);
//currentIndex = 0;
lastTime = DateTime.Now;
//DataTable dt = new DataTable();
displayPoint.Clear();
if (CBCTU.Text != "全部")
{
//CTUWayPoint temp = CTUtable.AsEnumerable()
// .Where(row => row.Field<uint>("CTUID").Equals(uint.Parse(CBCTU.Text)))
// .OrderByDescending(row => row.Field<DateTime>("Time"))
// .ToList().ForEach(s =>s as CTUWayPoint);
displayPoint = (List<CTUWayPoint>)cTUWayPoints
.Where(s => s.CTUID.Equals(uint.Parse(CBCTU.Text)))
.OrderBy(s => s.Time).ToList();
}
else
{
displayPoint = (List<CTUWayPoint>)cTUWayPoints
.OrderBy(s => s.Time).ToList();
}
if (displayPoint.Count > 0)
{
CTU_Update_timer.Interval = 1; // ~60 FPS
CTU_Update_timer.Elapsed += Timer_Tick;
CTU_Update_timer.Start();
}
CTU_timer.Start();
}
else
{
is_pause = true;
BTStart.Text = "启动";
CTU_Update_timer.Stop();
}
}
//当所要展示的倍数带来的更新数量超过每秒的处理逻辑数量时只能采用跳帧的方法
private void Timer_Tick(object sender, EventArgs e)
{
if (currentIndex < displayPoint.Count - 1)
{
var currentTime = DateTime.Now;
int elapsed = (int)(currentTime - lastTime).TotalMilliseconds;
int requiredTime = (int)((displayPoint[currentIndex + 1].Time - displayPoint[currentIndex].Time).TotalMilliseconds / speedMultiplier);
if (elapsed >= requiredTime)
{
currentIndex++;
lastTime = currentTime;
Update_CTU(displayPoint[currentIndex].CTUID.ToString(), displayPoint[currentIndex].PointCode, new List<uint>(), new List<uint>(),
new List<uint>(), Color.DarkBlue, Color.DarkGreen, Color.DarkGray, Color.Yellow, "回放");
}
}
else
{
CTU_Update_timer.Stop();
}
}
private void CBSpeed_SelectedIndexChanged(object sender, EventArgs e)
{
string select = CBSpeed.SelectedItem.ToString();
int index= select.IndexOf("倍");
speedMultiplier = float.Parse(CBSpeed.SelectedItem.ToString().Substring(0, index));
}
private void BTStop_Click(object sender, EventArgs e)
{
is_pause = true;
BTStart.Text = "启动";
BTStop.Enabled = false;
CBCTU.Enabled = true;
button1.Enabled = true;
currentIndex = 0;
CTU_Update_timer.Stop();
//清空画面保留地图
CTU_timer.Stop();
setimage?.Invoke(this, MapBmp);
cTUs.Clear();
}
}
}