Form2.cs
29.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
using CtuDeviceLib;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TheMachine
{
class CTUPointCode : PointCode
{
public new float X { get; set; } = -1;
public new float Y { get; set; } = -1;
}
class CTURobot
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 编号
/// </summary>
public uint Id { get; set; }
/// <summary>
/// 计划路径
/// </summary>
public List<CTUPointCode> PlanWay { get; set; }
/// <summary>
/// 当前路径
/// </summary>
public List<CTUPointCode> CurrentWay { get; set; }
/// <summary>
/// 占用地标
/// </summary>
public List<CTUPointCode> OccupyPoint { get; set; }
/// <summary>
/// CTU小车颜色
/// </summary>
public Color CTUColor { get; set; }
/// <summary>
/// CTU计划路径颜色
/// </summary>
public Color CTUPlanWayColor { get; set; }
/// <summary>
/// CTU当前路径颜色
/// </summary>
public Color CTUCurrentWayColor { get; set; }
/// <summary>
/// CTU占用地标颜色
/// </summary>
public Color CTUOccupyPointColor { get; set; }
}
public partial class Form2 : Form
{
private TcpClient _tcpClient;
private Thread _dataReceiveThread;
private bool _isRunning;
private CTUPointCode _agvPosition;
private List<CTUPointCode> _pathPoints = new List<CTUPointCode>();
static CtuDeviceLib.CTUBean ctuBean;
static string appPath = System.Windows.Forms.Application.StartupPath;
static ILog log = Mushiny.Common.LOGGER;
public static string FinishedCTUName = "原材料运输";
float ratio = 0.03f;//比例
float addreX = 150f;//整体偏移X
float addreY = -250f;//整体偏移Y
List<CTURobot> cTUs = new List<CTURobot>();
object Lock_Update_CTU = new object();
List<Color> colors = new List<Color>();
float CTU_Width = 50f;
private static Color GenerateRandomColor()
{
Random random = new Random();
int red = random.Next(0, 256);
int green = random.Next(0, 256);
int blue = random.Next(0, 256);
return Color.FromArgb(red, green, blue);
}
private void CreationCTUColor(int CTUNumber)
{
// 生成m种颜色
while (colors.Count < CTUNumber)
{
Color newColor = GenerateRandomColor();
// 确保颜色唯一
if (!colors.Contains(newColor))
{
colors.Add(newColor);
}
}
}
/// <summary>
/// 更新单个小车位置信息
/// </summary>
/// <param name="Id">小车ID</param>
/// <param name="PlanWay">小车完整路径</param>
/// <param name="CurrentWay">小车当前阶段运行路径</param>
/// <param name="OccupyPoint">小车占用地标</param>
public void Update_CTU(uint Id, List<PointCode> PlanWay, List<PointCode> CurrentWay, List<PointCode> OccupyPoint)
{
lock (Lock_Update_CTU)
{
if (cTUs != null)
{
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);
}
}
}
}
List<CTUPointCode> ConvertToCTUPointCode(List<PointCode> pointCodes)
{
CTUPointCode cTU;
List<CTUPointCode> cTUs = new List<CTUPointCode>();
foreach (var pathPoint in pointCodes)
{
cTU = 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
};
cTUs.Add(PointProcess(_agvPosition, ratio, true, addreX, addreY));
}
return cTUs;
}
/// <summary>
/// 注册小车
/// </summary>
/// <param name="Id">小车ID</param>
/// <param name="Name">小车名称</param>
/// <param name="PlanWay">小车计划路径可为null</param>
/// <param name="CurrentWay">小车当前路径可为null</param>
/// <param name="OccupyPoint">小车占用地标可为null</param>
public void Add_CTU(uint Id, string Name, List<PointCode> PlanWay, List<PointCode> CurrentWay, List<PointCode> OccupyPoint)
{
CTURobot cTU = new CTURobot();
cTU.Id = Id;
cTU.Name = Name;
if (PlanWay != null)
{
cTU.PlanWay = ConvertToCTUPointCode(PlanWay);
}
if (CurrentWay != null)
{
cTU.CurrentWay = ConvertToCTUPointCode(CurrentWay);
}
if (OccupyPoint != null)
{
cTU.OccupyPoint = ConvertToCTUPointCode(OccupyPoint);
}
if (colors.Count > 0)
{
cTU.CTUColor = colors.Last();
colors.Remove(cTU.CTUColor);
cTU.CTUPlanWayColor = colors.Last();
colors.Remove(cTU.CTUPlanWayColor);
cTU.CTUCurrentWayColor = colors.Last();
colors.Remove(cTU.CTUCurrentWayColor);
cTU.CTUOccupyPointColor = colors.Last();
colors.Remove(cTU.CTUOccupyPointColor);
}
else
{
cTU.CTUColor = Color.Red;
cTU.CTUPlanWayColor = Color.Red;
cTU.CTUCurrentWayColor = Color.Red;
cTU.CTUOccupyPointColor = Color.Red;
}
cTUs.Add(cTU);
}
public Form2()
{
InitializeComponent();
Init();
CreationCTUColor(48);//预设48种颜色
//InitializeAGVData();
}
private void InitializeAGVData()
{
// 初始化TCP连接(根据具体情况修改IP和端口)
_tcpClient = new TcpClient("192.168.1.100", 12345);
_isRunning = true;
_dataReceiveThread = new Thread(ReceiveData);
_dataReceiveThread.Start();
// 启动定时器
timer1.Interval = 1000;
timer1.Start();
}
private void ReceiveData()
{
NetworkStream stream = _tcpClient.GetStream();
byte[] buffer = new byte[1024];
while (_isRunning)
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
string data = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// 解析数据并更新AGV位置(根据具体数据格式)
ParseAGVData(data);
}
}
}
private void ParseAGVData(string data)
{
// 解析数据字符串,假设格式为 "X,Y"
string[] parts = data.Split(',');
if (parts.Length == 2)
{
float x = float.Parse(parts[0]) * ratio;
float y = float.Parse(parts[1]) * ratio;
//_agvPosition = new PointF(x, y);
// 更新路径点
_pathPoints.Add(_agvPosition);
// 通过Invoke调用更新UI的方法
this.Invoke(new Action(UpdateUI));
}
}
private void UpdateUI()
{
//// 清除之前的绘图
////panel1.Invalidate();
//// 重新绘制路径和AGV位置
//Graphics g = panel1.CreateGraphics();
//Pen pen = new Pen(Color.Blue, 5);
//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, 10, 10);
// }
// else
// {
// g.FillEllipse(brushpoint, _pathPoints[i].X, _pathPoints[i].Y, 10, 10);
// }
//}
//#region 绘制AGV小车,路线
//foreach(var ctu in cTUs)
//{
// //小车
// //Brush CTU = new SolidBrush(ctu.CTUColor);
// //g.FillEllipse(CTU, _agvPosition.X - 5, _agvPosition.Y - 5, 20, 20);
// //计划路径
// Pen PlanWay = new Pen(ctu.CTUPlanWayColor, 5);
// 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);
// }
// //当前路径
// Pen CurrentWay = new Pen(ctu.CTUCurrentWayColor, 5);
// 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);
// }
// //占用面积
// // 创建一个半透明的Brush
// Color semiTransparentColor = Color.FromArgb(128,ctu.CTUOccupyPointColor); // 128是透明度(0-255),后面是RGB颜色值
// Brush semiTransparentBrush = new SolidBrush(semiTransparentColor);
// float RectangleX = (ctu.OccupyPoint.First().X + ctu.OccupyPoint.Last().X)/2;
// float RectangleY = (ctu.OccupyPoint.First().Y + ctu.OccupyPoint.Last().Y)/2;
// float RectangleWidth = 50;
// float RectangleHight = (float)Math.Sqrt(Math.Pow(ctu.OccupyPoint.Last().X - ctu.OccupyPoint.First().X, 2) + Math.Pow(ctu.OccupyPoint.Last().Y - ctu.OccupyPoint.First().Y, 2));
// // 使用Math.Atan2计算倾斜角度(以弧度为单位)
// double angleInRadians = Math.Atan2(ctu.OccupyPoint.Last().Y - ctu.OccupyPoint.First().Y, ctu.OccupyPoint.Last().X - ctu.OccupyPoint.First().X);
// // 将弧度转换为度(1弧度 = 180/π度)
// double RectangleAngle = angleInRadians * (180.0 / Math.PI);
// GraphicsState gs = panel1.Graphics.Save();
// g.FillRectangle(semiTransparentBrush, RectangleX, RectangleY, RectangleWidth, RectangleHight);
// g.FillRectangles;
//}
//#endregion
// 绘制AGV当前位置
//panel1.Update();
}
private void timer1_Tick(object sender, EventArgs e)
{
// 可以在这里添加其他定时刷新逻辑
panel1.Update();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_isRunning = false;
if (_dataReceiveThread != null && _dataReceiveThread.IsAlive)
{
_dataReceiveThread.Join();
}
_tcpClient.Close();
}
static short disBtwLandmark = ConfigHelper.Config.Get<short>(Mushiny.Common.Mushiny_ + "DefaultDisBtwLandmark", 1000);
static bool setNeighboringLand(LandmarkNeighboor landmark, string line, short angle = 0)
{
if (string.IsNullOrEmpty(line))
{
return true;
}
string[] tmp1 = line.Split('#');
landmark.Andle = angle;
landmark.Distance = disBtwLandmark;
if (tmp1.Length == 3)
{
var id = uint.Parse(tmp1[0]);
var dis = short.Parse(tmp1[1]);
var angl = short.Parse(tmp1[2]);
landmark.Id = id;
landmark.Distance = dis;
landmark.Andle = angl;
return true;
}
else if (tmp1.Length == 2)
{
var id = uint.Parse(tmp1[0]);
var dis = short.Parse(tmp1[1]);
landmark.Distance = dis;
landmark.Id = id;
return true;
}
else if (tmp1.Length == 1)
{
var id = uint.Parse(tmp1[0]);
landmark.Id = id;
return true;
}
return false;
}
public static void Init()
{
try
{
ctuBean = new CTUBean(FinishedCTUName, new FinishedTrans());
string linefilePath = appPath + $"/Config/CTU/tbl_landmarks.csv";
if (!File.Exists(linefilePath))
{
log.Error($"未找到【{ctuBean.Name}】路标配置路径:{linefilePath}");
return;
}
log.Info($"加载【{ctuBean.Name}】路标配置:{linefilePath}");
string[] lines = File.ReadAllLines(linefilePath);
ctuBean.PointCodes.Clear();
for (int i = 1; i < lines.Length; i++)
{
string[] fields = lines[i].Split(',');
if (fields.Length < 10)
continue;
var landmark = new PointCode();
uint.TryParse(fields[0], out uint id);
landmark.Id = id;
landmark.Name = fields[1];
landmark.Type = (LandmarkType)Enum.Parse(typeof(LandmarkType), fields[2]);
landmark.CanTurning = bool.Parse(fields[3]);
string above = fields[4];
if (!string.IsNullOrEmpty(above))
{
landmark.Above = new LandmarkNeighboor();
}
if (!setNeighboringLand(landmark.Above, above, 0))
{
log.Error($"【{ctuBean.Name}】加载异常:{above}");
return;
}
string right = fields[5];
if (!string.IsNullOrEmpty(right))
{
landmark.Right = new LandmarkNeighboor();
}
if (!setNeighboringLand(landmark.Right, right, 90))
{
log.Error($"【{ctuBean.Name}】加载异常:{right}");
return;
}
string below = fields[6];
if (!string.IsNullOrEmpty(below))
{
landmark.Below = new LandmarkNeighboor();
}
if (!setNeighboringLand(landmark.Below, below, 180))
{
log.Error($"【{ctuBean.Name}】加载异常:{below}");
return;
}
string left = fields[7];
if (!string.IsNullOrEmpty(left))
{
landmark.Left = new LandmarkNeighboor();
}
if (!setNeighboringLand(landmark.Left, left, 270))
{
log.Error($"【{ctuBean.Name}】加载异常:{left}");
return;
}
landmark.X = int.Parse(fields[8]);
landmark.Y = int.Parse(fields[9]);
//landmark.Reserved = fields[8];
ctuBean.PointCodes.Add(landmark);
}
linefilePath = appPath + $"/Config/CTU/tbl_shelves.csv";
if (!File.Exists(linefilePath))
{
log.Error($"未找到【{ctuBean.Name}】料架配置路径:{linefilePath}");
return;
}
log.Info($"加载【{ctuBean.Name}】料架配置:{linefilePath}");
lines = File.ReadAllLines(linefilePath);
ctuBean.PosInfos.Clear();
for (int i = 1; i < lines.Length; i++)
{
string[] fields = lines[i].Split(',');
if (fields.Length < 8)
continue;
string posId = fields[0];
string pointCode = fields[1];
int.TryParse(fields[2], out int updown);
int.TryParse(fields[3], out int inout);
int.TryParse(fields[4], out int scancode);
int.TryParse(fields[5], out int containerWidth);
var shelve = new Shelves(posId, pointCode, updown, inout, scancode, containerWidth);
string side = fields[6].Trim();
if (!string.IsNullOrEmpty(side))
{
shelve.Side = side[0];
}
shelve.Desc = fields[7];
if (ctuBean.PosInfos.Find(s => s.ShelveCode == posId) != null)
{
log.Warn($"【{ctuBean.Name}】{posId} 已存在,请确保货架码唯一");
//MessageBox.Show($"【{ctuBean.Name}】{posId} 已存在,请确保货架码唯一");
continue;
}
var pos = new PosInfo()
{
Name = posId.Trim(),
Dir = (byte)(shelve.Side == 'L' ? 1 : 2),
InoutDepth = shelve.InoutDepth,
PointCode = shelve.PointCode,
ScanCodeShift = shelve.ScanCodeShift,
ShelveCode = posId,
UpDownHeight = shelve.UpDownHeight,
Lanway = shelve.Lanway,
Row = shelve.Row,
};
if (!string.IsNullOrEmpty(shelve.Desc))
{
pos.Name = shelve.Desc.Trim();
}
ctuBean.PosInfos.Add(pos);
}
linefilePath = appPath + $"/Config/CTU/tbl_pathways.csv";
if (!File.Exists(linefilePath))
{
log.Error($"未找到【{ctuBean.Name}】路线配置路径:{linefilePath}");
return;
}
log.Info($"加载【{ctuBean.Name}】路线配置:{linefilePath}");
lines = File.ReadAllLines(linefilePath);
ctuBean.PathWays.Clear();
for (int i = 1; i < lines.Length; i++)
{
string[] fields = lines[i].Split(',');
if (fields.Length < 5)
continue;
var pathway = new PathWay();
uint.TryParse(fields[0], out uint id);
pathway.Id = id;
pathway.Name = fields[1];
string[] paths = fields[2].Split('#');
pathway.Paths = new List<uint>();
foreach (string s in paths)
{
uint landId = uint.Parse(s);
pathway.Paths.Add(landId);
}
pathway.IsTwoWayLanes = bool.Parse(fields[3]);
string isLimitOneCtu = fields[4];
if (string.IsNullOrEmpty(isLimitOneCtu))
{
pathway.IsLimitOneCtu = false;
}
else
{
if (bool.TryParse(isLimitOneCtu, out bool isLimitCtu))
{
pathway.IsLimitOneCtu = isLimitCtu;
}
}
ctuBean.PathWays.Add(pathway);
log.Debug($"【{ctuBean.Name}】 加载路线:Id={pathway.Id},Name={pathway.Name}," +
$"【{(pathway.IsTwoWayLanes ? string.Join("<->", pathway.Paths) : string.Join(">", pathway.Paths))}】");
}
var nbs = ctuBean.NeighboringPathWays;
nbs.Clear();
var pws = ctuBean.PathWays;
List<uint> keyPoints = new List<uint>();
foreach (var pw in pws)
{
if (!keyPoints.Contains(pw.Paths[0]))
{
keyPoints.Add(pw.Paths[0]);
}
else if (!keyPoints.Contains(pw.Paths[pw.Paths.Count - 1]))
{
keyPoints.Add(pw.Paths[pw.Paths.Count - 1]);
}
}
keyPoints.Sort();
keyPoints.ForEach(s => log.Debug($"【{ctuBean.Name}】 keyPoint={s}"));
foreach (var p in keyPoints)
{
var ways = pws.FindAll(s => s.Paths.Contains(p));
for (int i = 0; i < ways.Count - 1; i++)
{
for (int j = i + 1; j < ways.Count; j++)
{
var tmpNB = new NeighboringPathWay()
{
KeyLandmark = p,
StartPathWay = ways[i].Id,
EndPathWay = ways[j].Id,
IsTwoWayLanes = ways[i].IsTwoWayLanes && ways[j].IsTwoWayLanes,
};
var find = nbs.Find(s => tmpNB.IsSame(s));
if (find == null)
{
nbs.Add(tmpNB);
log.Debug($"【{ctuBean.Name}】 生成邻接路线:startWay={tmpNB.StartPathWay},stopWay={tmpNB.EndPathWay},keyPoint={tmpNB.KeyLandmark},is-two-way={tmpNB.IsTwoWayLanes}");
}
}
}
}
//var nb = new NeighboringPathWay();
log.Debug($"【{ctuBean.Name}】 生成邻接路线完成");
}
catch (Exception ex)
{
}
}
/// <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 Form1_Load(object sender, EventArgs e)
{
foreach (var pathPoint in ctuBean.PointCodes)
{
//float x = pathPoint.X;
//float y = pathPoint.Y;
//_agvPosition = new PointF(x, y);
_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, ratio, true, addreX, addreY));
}
// 通过Invoke调用更新UI的方法
//this.Invoke(new Action(UpdateUI));
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
//this.Invoke(new Action(UpdateUI));
//Graphics g = e.Graphics;
//g.DrawLine(Pens.Black, 0, 0, 50, 300); // 绘制一条黑色线条
// 重新绘制路径和AGV位置
Graphics g = panel1.CreateGraphics();
Pen pen = new Pen(Color.Blue, 5);
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, 10, 10);
}
else
{
g.FillEllipse(brushpoint, _pathPoints[i].X, _pathPoints[i].Y, 10, 10);
}
}
#region 绘制AGV小车,路线
foreach (var ctu in cTUs)
{
//小车
//Brush CTU = new SolidBrush(ctu.CTUColor);
//g.FillEllipse(CTU, _agvPosition.X - 5, _agvPosition.Y - 5, 20, 20);
//计划路径
Pen PlanWay = new Pen(ctu.CTUPlanWayColor, 5);
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);
}
//当前路径
Pen CurrentWay = new Pen(ctu.CTUCurrentWayColor, 5);
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);
}
//占用面积
// 创建一个半透明的Brush
Color semiTransparentColor = Color.FromArgb(128, ctu.CTUOccupyPointColor); // 128是透明度(0-255),后面是RGB颜色值
Brush semiTransparentBrush = new SolidBrush(semiTransparentColor);
float RectangleX = (ctu.OccupyPoint.First().X + ctu.OccupyPoint.Last().X) / 2;
float RectangleY = (ctu.OccupyPoint.First().Y + ctu.OccupyPoint.Last().Y) / 2;
//float RectangleHight = 50;
//float RectangleWidth = (float)Math.Sqrt(Math.Pow(ctu.OccupyPoint.Last().X - ctu.OccupyPoint.First().X, 2) + Math.Pow(ctu.OccupyPoint.Last().Y - ctu.OccupyPoint.First().Y, 2));
float RectangleHight = Math.Abs(ctu.OccupyPoint.Last().Y - ctu.OccupyPoint.First().Y);
float RectangleWidth = Math.Abs(ctu.OccupyPoint.Last().X - ctu.OccupyPoint.First().X);
//// 使用Math.Atan2计算倾斜角度(以弧度为单位)
//double angleInRadians = Math.Atan2(ctu.OccupyPoint.Last().Y - ctu.OccupyPoint.First().Y, ctu.OccupyPoint.Last().X - ctu.OccupyPoint.First().X);
//// 将弧度转换为度(1弧度 = 180/π度)
//double RectangleAngle = angleInRadians * (180.0 / Math.PI);
g.FillRectangle(semiTransparentBrush, RectangleX, RectangleY, RectangleWidth <= RectangleHight ? CTU_Width : RectangleWidth, RectangleHight <= RectangleWidth ? CTU_Width : RectangleHight);
//文字
// 设置字体和笔刷
Font font = new Font("Arial", 16);
Brush CTUNameBrush = Brushes.Black;
// 要显示的文字
string CTUNameText = ctu.Name;
// 测量文字的大小以获取其位置
//SizeF textSize = g.MeasureString(CTUNameText, font);
//PointF location = new PointF((panel1.Width - textSize.Width) / 2, (panel1.Height - textSize.Height) / 2);
PointF location = new PointF(RectangleX, RectangleY);
// 绘制文字
g.DrawString(CTUNameText, font, CTUNameBrush, location);
// 释放字体资源(可选,如果使用IDisposable的对象,最好进行释放)
font.Dispose();
}
#endregion
}
}
}