EditPointCodeHelper.cs 13.5 KB
using CtuDeviceLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ConveyorLine.EditPointCode
{
    public class EditPointCodeHelper
    {
        public static List<PointCode> pointCodes = new List<PointCode>();
        /// <summary>
        /// 基准码,用来生成坐标关系
        /// </summary>
        public static PointCode BasePointCode = null;
        static string appPath = System.Windows.Forms.Application.StartupPath;
        static short disBtwLandmark = ConfigHelper.Config.Get<short>(Mushiny.Common.Mushiny_ + "DefaultDisBtwLandmark", 1200);
        static string landmarkHeader = "";
        public static bool LoadPointCodes()
        {
            string dir = appPath + $"/CtuService/Config/CSV/CTU/";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string linefilePath = dir + $"tbl_landmarks.csv";
            if (!File.Exists(linefilePath))
            {
                MessageBox.Show($"未找到路标配置路径:{linefilePath}");
                return false;
            }
            string[] lines = File.ReadAllLines(linefilePath);
            pointCodes.Clear();
            landmarkHeader = lines[0];
            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))
                {
                    MessageBox.Show($"{landmark.Name} 加载异常 {above}");
                    return false;
                }
                string right = fields[5];
                if (!string.IsNullOrEmpty(right))
                {
                    landmark.Right = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Right, right, 90))
                {
                    MessageBox.Show($"{landmark.Name} 加载异常 {right}");
                    return false;
                }
                string below = fields[6];
                if (!string.IsNullOrEmpty(below))
                {
                    landmark.Below = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Below, below, 180))
                {
                    MessageBox.Show($"{landmark.Name} 加载异常 {below}");
                    return false;
                }
                string left = fields[7];
                if (!string.IsNullOrEmpty(left))
                {
                    landmark.Left = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Left, left, 270))
                {
                    MessageBox.Show($"{landmark.Name} 加载异常 {left}");
                    return false;
                }
                landmark.X = int.Parse(fields[8]);
                landmark.Y = int.Parse(fields[9]);
                //landmark.Reserved = fields[8];
                var find = pointCodes.Find(s => s.Id == landmark.Id);
                if (find != null)
                {
                    MessageBox.Show($"该地标{landmark.Id}已存在,请确保地标唯一");
                    continue;
                }
                pointCodes.Add(landmark);
            }
            //地标的附近点检查,不存在的清除掉
            foreach (var code in pointCodes)
            {
                if (code.Above != null)
                {
                    var find = pointCodes.Find(s => s.Id == code.Above.Id);
                    if (find == null)//没找到点
                    {
                        code.Above = null;
                    }
                }
                if (code.Right != null)
                {
                    var find = pointCodes.Find(s => s.Id == code.Right.Id);
                    if (find == null)//没找到点
                    {
                        code.Right = null;
                    }
                }
                if (code.Below != null)
                {
                    var find = pointCodes.Find(s => s.Id == code.Below.Id);
                    if (find == null)//没找到点
                    {
                        code.Below = null;
                    }
                }
                if (code.Left != null)
                {
                    var find = pointCodes.Find(s => s.Id == code.Left.Id);
                    if (find == null)//没找到点
                    {
                        code.Left = null;
                    }
                }

            }
            return true;
        }

        public static void SavePointsCodes()
        {
            string dir = appPath + $"/CtuService/Config/CSV/CTU/Edit/{DateTime.Now.ToString("yyyyMMddHHmmss")}/";
            string linefilePath = dir + $"tbl_landmarks.csv";
            StringBuilder stringBuilder = new StringBuilder();
            if (pointCodes.Count > 0)
            {
                stringBuilder.AppendLine(landmarkHeader);
                foreach (var code in pointCodes)
                {
                    List<string> str = new List<string>()
                    {
                        code.Id.ToString(),
                        code.Name?.ToString(),
                        ((int)code.Type).ToString(),
                        code.CanTurning.ToString(),
                    };
                    if (code.Above != null)
                    {
                        str.Add($"{code.Above.Id}#{code.Above.Distance}#{code.Above.Andle}");
                    }
                    else
                    {
                        str.Add("");
                    }
                    if (code.Right != null)
                    {
                        str.Add($"{code.Right.Id}#{code.Right.Distance}#{code.Right.Andle}");
                    }
                    else
                    {
                        str.Add("");
                    }
                    if (code.Below != null)
                    {
                        str.Add($"{code.Below.Id}#{code.Below.Distance}#{code.Below.Andle}");
                    }
                    else
                    {
                        str.Add("");
                    }
                    if (code.Left != null)
                    {
                        str.Add($"{code.Left.Id}#{code.Left.Distance}#{code.Left.Andle}");
                    }
                    else
                    {
                        str.Add("");
                    }
                    str.Add(code.X.ToString());
                    str.Add(code.Y.ToString());
                    stringBuilder.AppendLine(string.Join(",", str));
                }
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            File.WriteAllText(linefilePath, stringBuilder.ToString());
            MessageBox.Show($"保存地标完成:{linefilePath}");
        }
        public static void SavePathWays()
        {
            
            //string dir = appPath + $"/Edit/{DateTime.Now.ToString("yyyyMMddHHmmss")}/CTU/";
            //string linefilePath = dir + $"tbl_pathways.csv";
            //string header = "id,name,paths,isTwoWayLanes,lanewayCode";
            //StringBuilder stringBuilder = new StringBuilder();
            //var turns = pointCodes.FindAll(s => s.Type == LandmarkType.Turning);
            //List<List<uint>> pathways = new List<List<uint>>();
            //foreach ( var turn in turns )
            //{
            //    if(turn.Above!=null)
            //    {
            //        var path = new List<uint>
            //        {
            //            turn.Id,
            //            turn.Above.Id
            //        };
            //        var above = pointCodes.Find(s=>s.Id==turn.Above.Id);
            //        while(!(above ==null || above.Type== LandmarkType.Turning))
            //        {
            //            if(above.Above!=null)
            //            {
            //                above = pointCodes.Find(s => s.Id == above.Above.Id);
            //                path.Add(above.Id);
            //            }
            //            else
            //            {
            //                above = null;
            //            }
            //        }
            //        pathways.Add(path);
            //    }
            //    if (turn.Right != null)
            //    {
            //        var path = new List<uint>
            //        {
            //            turn.Id,
            //            turn.Right.Id
            //        };
            //        var above = pointCodes.Find(s => s.Id == turn.Right.Id);
            //        while (!(above == null || above.Type == LandmarkType.Turning))
            //        {
            //            if (above.Right != null)
            //            {
            //                above = pointCodes.Find(s => s.Id == above.Right.Id);
            //                path.Add(above.Id);
            //            }
            //            else
            //            {
            //                above = null;
            //            }
            //        }
            //        pathways.Add(path);
            //    }
            //    if (turn.Below != null)
            //    {
            //        var path = new List<uint>
            //        {
            //            turn.Id,
            //            turn.Below.Id
            //        };
            //        var above = pointCodes.Find(s => s.Id == turn.Below.Id);
            //        while (!(above == null || above.Type == LandmarkType.Turning))
            //        {
            //            if (above.Below != null)
            //            {
            //                above = pointCodes.Find(s => s.Id == above.Below.Id);
            //                path.Add(above.Id);
            //            }
            //            else
            //            {
            //                above = null;
            //            }
            //        }
            //        pathways.Add(path);
            //    }

            //    if (turn.Left != null)
            //    {
            //        var path = new List<uint>
            //        {
            //            turn.Id,
            //            turn.Left.Id
            //        };
            //        var above = pointCodes.Find(s => s.Id == turn.Left.Id);
            //        while (!(above == null || above.Type == LandmarkType.Turning))
            //        {
            //            if (above.Left != null)
            //            {
            //                above = pointCodes.Find(s => s.Id == above.Left.Id);
            //                path.Add(above.Id);
            //            }
            //            else
            //            {
            //                above = null;
            //            }
            //        }
            //        pathways.Add(path);
            //    }
            //}



            //if (pathways.Count > 0)
            //{
            //    stringBuilder.AppendLine(header);
            //    //id,name,paths,isTwoWayLanes,lanewayCode
            //    int id = 1;
            //    foreach (var path in pathways)
            //    {
            //        List<string> str = new List<string>()
            //        {
            //            id.ToString(),
            //            id.ToString(),
            //            string.Join("#", path),
            //            "false",
            //            ""
            //        };
            //        id++;
            //        stringBuilder.AppendLine(string.Join(",", str));
            //    }
            //}
            //if (!Directory.Exists(dir))
            //{
            //    Directory.CreateDirectory(dir);
            //}
            //File.WriteAllText(linefilePath, stringBuilder.ToString());
            //MessageBox.Show($"保存路线完成:{linefilePath}");
        }

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