Form1.cs 19.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
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.Timers;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;

namespace TheMachine
{
   
    public partial class Form1 : 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 = -250;//整体偏移X
        float addreY = 1640;//整体偏移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 = 1;
        List<CTURobot> cTUs = new List<CTURobot>();
        object Lock_Update_CTU = 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>();
       
      
       
       
       
        public Form1()
        {
            InitializeComponent();
            Init();         
        }
        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)
            {

            }

        }
       
        
        private void Form1_Load(object sender, EventArgs e)
        {
           
            {
                #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);
                int maxPointX = int.MinValue;
                int maxPointY = int.MinValue;
                int minPointX = int.MaxValue;
                int minPointY = int.MaxValue;
                int RangX = int.MaxValue;
                int RangY = int.MaxValue;
                foreach (var point in traversal.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;
                    }
                }
                List<Point_CTU> tempPointCodes = new List<Point_CTU>();
                Point_CTU temp = null;
                foreach (var point in traversal.PointCodes)
                {
                    temp = point;
                    temp.X = point.X - minPointX;
                    temp.Y = point.Y - minPointY;
                    tempPointCodes.Add(temp);
                }

                foreach (var point in tempPointCodes)
                {
                    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);
                Application.Exit();
                System.Environment.Exit(0);
                #endregion
            }
        }
        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;
        }
       
    }
}