Form4.cs 40.4 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
using CtuDeviceLib;
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.IO;
using System.Linq;
using System.Net.Sockets;
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
{
    public partial class Form4 : 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.019f;//比例
        float addreX = 50;//整体偏移X
        float addreY = 50;//整体偏移Y
        //float addreX = 150f;//整体偏移X
        //float addreY = -250f;//整体偏移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>();
        object Lock_Update_CTU = new object();
        object Lock_Update_CTUUI = new object();
        List<Color> colors = new List<Color>();
        float CTU_Width = 50f;
        System.Timers.Timer CTU_timer = new System.Timers.Timer();
        System.Timers.Timer CTU_test_timer = new System.Timers.Timer();
        List<PointCode> PointCodes = new List<PointCode>();
        static short disBtwLandmark = ConfigHelper.Config.Get<short>(Mushiny.Common.Mushiny_ + "DefaultDisBtwLandmark", 1000);
        Bitmap MapBmp;
       
        float distanceRatio = 0.04f;
        float markRatio = 5.0f;
        int markFontRatio = 6;
        float LineRatio = 2.0f;
        float CTULineRatio = 2.0f;
        int CTUFontRatio = 16;
        event EventHandler<Bitmap> setimage;
        private float _scale = 1.0f; // 当前的缩放比例  
        private PointF _translation = new PointF(0, 0); // 当前的平移量  
        private bool _isDragging = false; // 是否正在拖动图像  
        private PointF _lastMousePosition; // 上一个鼠标位置,用于拖动计算   
        Graphics mapDraw = null;
        Graphics CTUDraw = null;
        //箭头
        // 角度A(以度为单位)  
        float angleA = 135;
        // 长度B  
        float lengthB = 5;
        Dictionary<uint, CTUPointCode> CTUMap = new Dictionary<uint, CTUPointCode>();

        #region 测试
        private void CTU_test_timer_Tick(object sender, EventArgs e)
        {
            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;

            Update_CTU(3037, planway, currentway, occupyway, ctuclor, planclor, currentclor, occupyclor, "正常");

        }
        #endregion
        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);
                    }
                }
            }
        }
        /// <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;
                    }
                }
                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);
                }
            }
        }
        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;
        }
        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;
        }
        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);
        }
        /// <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);
        }
        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)
        {
            lock (Lock_Update_CTU) { 
            #region 绘制AGV小车,路线
            foreach (var ctu in cTUs)
            {
                if (ctu.PlanWay.Count == 0 || ctu.CurrentWay.Count == 0 || ctu.OccupyPoint.Count == 0 || ctu.CTUPlanWayColor == null || ctu.CTUCurrentWayColor == null || ctu.CTUOccupyPointColor == null)
                {
                    return;
                }
                //小车(以固定半透明框代表)
                Color semiTransparentColor = Color.FromArgb(128, ctu.CTUColor); // 128是透明度(0-255),后面是RGB颜色值  
                Brush semiTransparentBrush = new SolidBrush(semiTransparentColor);
                    LogUtil.debug($"{ctu.OccupyPoint.Count / 2 - 1}");
                    var CTUPoint = ctu.OccupyPoint[ctu.OccupyPoint.Count / 2-1];
                   
                    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;
                // 测量文字的大小以获取其位置  
                //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();


                //计划路径
                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);
                }
                //当前路径
                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);
                }


                //占用路径
                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
        }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            lock (Lock_Update_CTUUI)
            {
                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;
                    // 绘制背景,填充为白色  
                    //CTUDraw.Clear(Color.White);
                    UpdateCTU(CTUDraw);
                    setimage?.Invoke(this, DrawMapBmp);

                }
            }
           
        }
        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()
        {
            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;
        }
        CTUPointCode PointProcess(CTUPointCode point, bool overturn = true, float addreX = 0, float addreY = 0)
        {
            float normalizedX = 0;
            float normalizedY = 0;
            float scaledX = 0;
            float scaledY = 0;
            if (!overturn)
            {
                normalizedX = (point.X - minPointX) / RangX;
                normalizedY = (point.Y - minPointY) / RangY;
                scaledX = normalizedX * (screenWidth * screenRatio);
                scaledY = normalizedY * (screenHeight * screenRatio);
                point.X = scaledX;
                point.Y = scaledY;
            }
            else
            {
                normalizedX = (point.X - minPointX) / RangX;
                normalizedY = (point.Y - minPointY) / RangY;
                scaledX = normalizedX * screenHeight * screenRatio;
                scaledY = normalizedY * screenWidth * screenRatio;
                point.X = scaledY;
                point.Y = scaledX;
            }
            return point;
        }

        static void WriteCsv(string filePath, List<string[]> rows)
        {
            using (var writer = new StreamWriter(filePath))
            {
                foreach (var row in rows)
                {
                    writer.WriteLine(string.Join(",", row));
                }
            }
        }
        static List<string[]> ReadCsv(string filePath)
        {
            List<string[]> rows = new List<string[]>();

            using (var reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');
                    rows.Add(values);
                }
            }

            return rows;
        }
        private void AlgoMatch_setimage(object sender, Bitmap e)
        {
            Bitmap temp = e.Clone() as Bitmap;
            this.chartView1.Invoke(new Action(() =>
            {
                chartView1.ImgData = temp;
            }));
        }

        public Form4()
        {
            InitializeComponent();
            Init();
            //CreationCTUColor(48);//预设48种颜色
            CTU_test_timer.Enabled = true;
            CTU_test_timer.Elapsed += CTU_test_timer_Tick;
            CTU_test_timer.Interval = 100;
            CTU_test_timer.Start();
        }
        bool debug = false;
        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.Above.Id, ctuPoint.Below.Id, ctuPoint.Left.Id, ctuPoint.Right.Id, ctuPoint.Above.Distance, ctuPoint.Below.Distance, ctuPoint.Left.Distance, ctuPoint.Right.Distance, ctuPoint.X, ctuPoint.Y, ctuPoint.Id);
                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
            if (!debug)
            {
                #region 自动缩放(弃用)
                //foreach (var point in ctuBean.PointCodes)
                //{
                //    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;
                #endregion

                foreach (var pathPoint in 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, distanceRatio, false, addreX, addreY));
                    //_pathPoints.Add(PointProcess(_agvPosition, false, 0, 0));
                }

                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);
                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);

                setimage?.Invoke(this, MapBmp);

                CTU_timer.Enabled = true;
                CTU_timer.Elapsed += timer1_Tick;
                CTU_timer.Interval = 500;
                CTU_timer.Start();
               
            }
            else
            {
                #region 修改地码坐标问题(弃用)
                //PointCode tempPoint;

                //foreach (var pathPoint in ctuBean.PointCodes)
                //{
                //    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.Above.Id, ctuPoint.Below.Id, ctuPoint.Left.Id, ctuPoint.Right.Id, ctuPoint.Above.Distance, ctuPoint.Below.Distance, ctuPoint.Left.Distance, ctuPoint.Right.Distance, ctuPoint.X, ctuPoint.Y, ctuPoint.Id);
                //    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)开始遍历  


                ////修改并保存文件
                //string linefilePath = appPath + $"/Config/CTU/tbl_landmarks.csv";
                //string outputFilePath = appPath + $"/Config/CTU/tbl_landmarksBack.csv";
                //List<string[]> rows = ReadCsv(linefilePath);
                //foreach (var point in traversal.PointCodes)
                //{
                //    string[] row = rows.First(m => m[0].Trim() == point.Id.ToString());
                //    if (row != null)
                //    {
                //        row[8] = point.X.ToString();
                //        row[9] = point.Y.ToString();
                //    }
                //}
                //WriteCsv(outputFilePath, rows);
                //this.Close();
                #endregion
            }
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
            System.Environment.Exit(0);
        }
    }
}