FrmPointCode.cs 24.0 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
using ConveyorLine.EditPointCode;
using CtuDeviceLib;
using DeviceLibrary;
using Mushiny;
using OneWay;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheMachine;

namespace ConveyorLine
{
    public partial class FrmPointCode : Form
    {
        public FrmPointCode()
        {
            InitializeComponent();
            ucPointCode1.PointCodeChanged += UcPointCode1_PointCodeChanged;
        }

        private void UcPointCode1_PointCodeChanged()
        {
            showPointCodes();
        }
        void showPointCodes()
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            EditPointCodeHelper.pointCodes.Sort((a, b) => { return ((int)a.Id - (int)b.Id); });

            EditPointCodeHelper.pointCodes.ForEach(s =>
            {
                if (s.X == -1 || s.Y == -1)
                {
                    listBox1.Items.Add($"{s.Id}");
                }
                else
                {
                    listBox2.Items.Add($"{s.Id}");
                }
            }
            );
            if (curSlelectIdx < listBox1.Items.Count)
            {
                listBox1.SelectedIndex = curSlelectIdx;
            }
            else if (curSlelectIdx < listBox2.Items.Count)
            {
                listBox2.SelectedIndex = curSlelectIdx;
            }
        }
        int curSlelectIdx;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var lstBox = (ListBox)sender;
            if (lstBox == null)
            {
                return;
            }
            var idStr = lstBox.SelectedItem.ToString();
            curSlelectIdx = lstBox.SelectedIndex;
            if (uint.TryParse(idStr, out var id))
            {
                var code = EditPointCodeHelper.pointCodes.Find(s => s.Id == id);
                ucPointCode1.CurPointCode = code;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EditPointCodeHelper.pointCodes.ForEach(s => { s.X = -1; s.Y = -1; });
            showPointCodes();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FrmReplace frmReplace = new FrmReplace();
            frmReplace.ShowDialog();
            showPointCodes();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FrmBasePoint frmBasePoint = new FrmBasePoint();
            frmBasePoint.ShowDialog();
            if (EditPointCodeHelper.BasePointCode != null)
            {
                showPointCodes();
            }
        }

        private void 加载ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (EditPointCodeHelper.LoadPointCodes())
            {
                showPointCodes();
            }
            else
            {
                textBox1.Text = ("加载地标失败");
            }
        }

        private void 保存ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            EditPointCodeHelper.SavePointsCodes();
        }
        /// <summary>
        /// 更新坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (EditPointCodeHelper.pointCodes.Count == 0) return;

            var waitUpdateCodes = EditPointCodeHelper.pointCodes.FindAll(s => s.X == -1 && s.Y == -1);
            var updatedCodes = EditPointCodeHelper.pointCodes.FindAll(s => s.X >= 0 && s.Y >= 0);
            int preWaitUpdateCnt = waitUpdateCodes.Count;

            while (waitUpdateCodes.Count > 0)
            {
                for (var i = updatedCodes.Count - 1; i >= 0; i--)
                {
                    var item = updatedCodes[i];
                    var side = item.Above;
                    if (side != null)
                    {
                        var tmp = waitUpdateCodes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            tmp.X = item.X;
                            tmp.Y = item.Y - side.Distance;
                        }
                    }
                    side = item.Right;
                    if (side != null)
                    {
                        var tmp = waitUpdateCodes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            tmp.X = item.X + side.Distance;
                            tmp.Y = item.Y;
                        }
                    }
                    side = item.Below;
                    if (side != null)
                    {
                        var tmp = waitUpdateCodes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            tmp.X = item.X;
                            tmp.Y = item.Y + side.Distance;
                        }
                    }
                    side = item.Left;
                    if (side != null)
                    {
                        var tmp = waitUpdateCodes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            tmp.X = item.X - side.Distance;
                            tmp.Y = item.Y;
                        }
                    }
                    waitUpdateCodes = EditPointCodeHelper.pointCodes.FindAll(s => s.X == -1 && s.Y == -1);
                    updatedCodes = EditPointCodeHelper.pointCodes.FindAll(s => s.X >= 0 && s.Y >= 0);
                }
                if (preWaitUpdateCnt == waitUpdateCodes.Count)
                {
                    break;
                }
                else
                {
                    preWaitUpdateCnt = waitUpdateCodes.Count;
                }
            }
            if (preWaitUpdateCnt > 0)
            {
                StringBuilder sb = new StringBuilder();
                waitUpdateCodes.ForEach(s => sb.AppendLine(s.Id.ToString()));
                textBox1.Text = ("坐标更新失败:\r\n" + sb.ToString());
            }
            else
            {
                textBox1.Text = ("坐标更新成功");
            }
            showPointCodes();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (EditPointCodeHelper.pointCodes.Count == 0)
            {
                textBox1.Text = ("无可检查的地码");
                return;
            }
            textBox1.Text = "";
            var finds = EditPointCodeHelper.pointCodes.FindAll(s => s.Above == null && s.Below == null && s.Left == null && s.Right == null);
            if (finds != null && finds.Count > 0)
            {
                StringBuilder sb = new StringBuilder("如下地码无相邻地码,请检查:");
                foreach (var find in finds)
                {
                    sb.AppendLine(find.Id.ToString());
                }
                textBox1.Text = (sb.ToString());
            }
            else
            {
                var codes = EditPointCodeHelper.pointCodes;
                StringBuilder sb = new StringBuilder();
                foreach (var item in codes)
                {
                    var side = item.Above;
                    if (side != null)
                    {
                        var tmp = codes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            if (tmp.Below?.Id != item.Id)
                            {
                                sb.AppendLine($"请检查:地码【{item.Id}】上方是【{side.Id}】,但【{side.Id}】下方是【{tmp.Below?.Id}】");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"请检查:地码【{item.Id}】上方是【{side.Id}】,但【{side.Id}】不存在");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"请确认:地码【{item.Id}】上方为空");
                    }
                    side = item.Right;
                    if (side != null)
                    {
                        var tmp = codes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            if (tmp.Left?.Id != item.Id)
                            {
                                sb.AppendLine($"请检查:地码【{item.Id}】右侧是【{side.Id}】,但【{side.Id}】左侧是【{tmp.Left?.Id}】");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"请检查:地码【{item.Id}】右侧是【{side.Id}】,但【{side.Id}】不存在");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"请确认:地码【{item.Id}】右为空");
                    }
                    side = item.Below;
                    if (side != null)
                    {
                        var tmp = codes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            if (tmp.Above?.Id != item.Id)
                            {
                                sb.AppendLine($"请检查:地码【{item.Id}】下方是【{side.Id}】,但【{side.Id}】上方是【{tmp.Above?.Id}】");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"请检查:地码【{item.Id}】下方是【{side.Id}】,但【{side.Id}】不存在");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"请确认:地码【{item.Id}】下方为空");
                    }
                    side = item.Left;
                    if (side != null)
                    {
                        var tmp = codes.Find(s => s.Id == side.Id);
                        if (tmp != null)
                        {
                            if (tmp.Right?.Id != item.Id)
                            {
                                sb.AppendLine($"请检查:地码【{item.Id}】左侧是【{side.Id}】,但【{side.Id}】右侧是【{tmp.Right?.Id}】");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"请检查:地码【{item.Id}】左侧是【{side.Id}】,但【{side.Id}】不存在");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"请确认:地码【{item.Id}】左侧为空");
                    }
                }
                var res = sb.ToString();
                if (string.IsNullOrEmpty(res))
                {
                    textBox1.Text = ("检查完成");
                }
                else
                {
                    textBox1.Text = ("检查结果:\r\n" + res);
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //int width = pictureBox1.Width;
            //int height = pictureBox1.Height;
            //            Bitmap bmp = new Bitmap(width, height);
            //            Brush noneBrush = new SolidBrush(Color.Black);
            //            Brush shelfBrush = new SolidBrush(Color.Blue);
            //            Brush turningBrush = new SolidBrush(Color.Brown);
            //            Brush dockBrufh = new SolidBrush(Color.Green);
            //            using (Graphics g = Graphics.FromImage(bmp))
            //            {
            //                int xRange = EditPointCodeHelper.pointCodes.Max(x => x.X) - EditPointCodeHelper.pointCodes.Min(x => x.X);
            //                int yRange = EditPointCodeHelper.pointCodes.Max(x => x.Y) - EditPointCodeHelper.pointCodes.Min(x => x.Y);
            //                var coefX = (float)(width * 0.96 / xRange);
            //                var coefY = (float)(height * 0.96 / yRange);
            //                var linePen = new Pen(Color.Black, 2);
            //                g.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));
            //                foreach (var item in EditPointCodeHelper.pointCodes)
            //                {
            //                    var point = new PointF() { X = item.X * coefX, Y = item.Y * coefY };
            //                    if (item.Type == LandmarkType.None)
            //                        g.DrawString($"{item.Id}", new Font("黑体", 9), noneBrush, point);
            //                    else if (item.Type == LandmarkType.Turning)
            //                        g.DrawString($"{item.Id}", new Font("黑体", 9), turningBrush, point);
            //                    else if (item.Type == LandmarkType.Shelves)
            //                    {
            //                        g.DrawString($"{item.Id}", new Font("黑体", 9), shelfBrush, point);
            //                    }
            //                    else if (item.Type == LandmarkType.Docking_0 ||
            //          item.Type == LandmarkType.Docking_90 ||
            //             item.Type == LandmarkType.Docking_180 ||
            //                item.Type == LandmarkType.Docking_270
            //)
            //                    {
            //                        g.DrawString($"{item.Id}", new Font("黑体", 9), dockBrufh, point);
            //                    }
            //                    var curPoint = new PointF(item.X, item.Y);
            //                    if (item.Above != null)
            //                    {
            //                        var side = item.Above;
            //                        var find = EditPointCodeHelper.pointCodes.Find(s => s.Id == side.Id);
            //                        if (find != null)
            //                        {
            //                            var nexPoint = new PointF(find.X, find.Y);
            //                            g.DrawLine(linePen, curPoint, nexPoint);
            //                        }

            //                    }
            //                    else if (item.Below != null)
            //                    {
            //                        var side = item.Below;
            //                        var find = EditPointCodeHelper.pointCodes.Find(s => s.Id == side.Id);
            //                        if (find != null)
            //                        {
            //                            var nexPoint = new PointF(find.X, find.Y);
            //                            g.DrawLine(linePen, curPoint, nexPoint);
            //                        }
            //                    }
            //                    else if (item.Left != null)
            //                    {
            //                        var side = item.Left;
            //                        var find = EditPointCodeHelper.pointCodes.Find(s => s.Id == side.Id);
            //                        if (find != null)
            //                        {
            //                            var nexPoint = new PointF(find.X, find.Y);
            //                            g.DrawLine(linePen, curPoint, nexPoint);
            //                        }
            //                    }
            //                    else if (item.Right != null)
            //                    {
            //                        var side = item.Right;
            //                        var find = EditPointCodeHelper.pointCodes.Find(s => s.Id == side.Id);
            //                        if (find != null)
            //                        {
            //                            var nexPoint = new PointF(find.X, find.Y);
            //                            g.DrawLine(linePen, curPoint, nexPoint);
            //                        }
            //                    }
            //                }
            //            }
            //            pictureBox1.Image = bmp;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //CTUBean ctuBean = new CTUBean("测试", null);
            //StringBuilder stringBuilder = new StringBuilder();
            //ctuBean.PointCodes.AddRange(EditPointCodeHelper.pointCodes);
            //foreach (var code1 in EditPointCodeHelper.pointCodes)
            //{
            //    foreach (var code2 in EditPointCodeHelper.pointCodes)
            //    {
            //        if (code1.Id == code2.Id) { continue; }
            //        var startPoint = ctuBean.GetPointCode(code1.Id);
            //        var start = new AStartPoint(startPoint);

            //        var endPoint = ctuBean.GetPointCode(code2.Id);
            //        var end = new AStartPoint(endPoint);
            //        if (startPoint == null || endPoint == null)
            //        {
            //            if (startPoint == null)
            //            {
            //                MessageBox.Show($"地码【{code1.Id}】未维护");
            //            }
            //            if (endPoint == null)
            //            {
            //                MessageBox.Show($"地码【{code2.Id}】未维护");
            //            }
            //            return;
            //        }
            //        var result = ctuBean.AStart.FindPath(start, end);
            //        if (result == null)
            //        {
            //            stringBuilder.AppendLine($"【{code1.Id}】->【{code2.Id}】:规划失败");
            //        }
            //        else
            //        {
            //            var ways = new List<PointCode>();
            //            while (result != null)
            //            {
            //                ways.Add(result.Landmark);
            //                Console.WriteLine($"{result.Id}({result.X},{result.Y})");
            //                result = result.ParentPoint;
            //            }
            //            ways.Reverse();
            //            ways.ForEach(s => stringBuilder.Append($"{s.Id}->"));
            //            stringBuilder.AppendLine();
            //        }
            //    }
            //}
            //string str = stringBuilder.ToString();
            //if (!string.IsNullOrEmpty(str))
            //    textBox1.Text = ($"{str}");
        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            EditPointCodeHelper.SavePathWays();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.InitialDirectory = Application.StartupPath + $"/CtuService/Config/CSV/CTU/Edit/";
            var rtn = fileDialog.ShowDialog();
            if (rtn == DialogResult.OK)
            {
                string file = fileDialog.FileName;
                var info = new FileInfo(file);
                FrmOneway frmOneway = new FrmOneway(info.Directory.FullName);
                frmOneway.ShowDialog();
            }

        }
        void calibCoord()
        {
            if (EditPointCodeHelper.pointCodes == null || EditPointCodeHelper.pointCodes.Count == 0) return;

            //if (EditPointCodeHelper.BasePointCode == null)
            //{
            //    MessageBox.Show("未设置基准地码");
            //    return;
            //}
            #region 修改地码坐标问题
            List<Point_CTU> points = new List<Point_CTU>();
            foreach (PointCode ctuPoint in EditPointCodeHelper.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)开始遍历  


            //修改并保存文件
            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);
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("修正的坐标信息:");
            foreach (var point in tempPointCodes)
            {
                var row = EditPointCodeHelper.pointCodes.First(m => m.Id == point.Id);
                if (row != null)
                {
                    sb.AppendLine($"[{row.Id}] ({row.X},{row.Y})->({point.X},{point.Y})");
                    row.X = point.X;
                    row.Y = point.Y;
                }
            }
            textBox1.Text = sb.ToString();
            MessageBox.Show("坐标修正完成");
            #endregion
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            calibCoord();
        }
    }
}