Sharp.cs 22.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
using RCS.Model.Comm;
using RCS.Model.Entity;
using System.Drawing.Printing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace RCS.WinClient.Common
{
    public class Sharp
    {

        LinearGradientBrush linearGradientBrush = new LinearGradientBrush();
        ColorAnimation animation = new ColorAnimation();
        /// <summary>
        /// 初始化监控界面
        /// </summary>
        public int[] MoniterAreaInit()
        {
            //获取xy方向的最大值
            App.WidthMax = App.PointList.Max(a => a.IntX + 1);
            App.WidthMin = App.PointList.Min(a => a.IntX - 1);
            App.HeightMax = App.PointList.Max(a => a.IntY + 1);
            App.HeightMin = App.PointList.Min(a => a.IntY - 1);

            //设置画布大小
            int pointXCount = App.WidthMax - App.WidthMin;
            int AreaWidth = pointXCount * App.GsWidth + pointXCount;
            int pointYCount = App.HeightMax - App.HeightMin;
            int AreaHeight = pointYCount * App.GsHeight + pointYCount;

            return new int[] { AreaWidth, AreaHeight };
        }

        /// <summary>
        /// 货架绘制
        /// </summary>
        public void ShelfSet()
        {
            foreach (Base_Point point in App.PointList.FindAll(x => x.PointType == EnumMsg.PointType.充电点 || x.PointType == EnumMsg.PointType.站台点))
            {
                var station = App.StationList.FirstOrDefault(x => x.Barcode == point.Barcode);
                var rectangle = new Rectangle();
                rectangle.ToolTip = point.Barcode;
                ToolTipService.SetInitialShowDelay(rectangle, 100);
                ToolTipService.SetShowDuration(rectangle, 5000);    // 显示时长为 5000 毫秒
                if (station != null)
                {
                    rectangle.ToolTip += string.Format("\n名称:{0}\n防撞:{1}\n状态:{2}\n长度:{3}CM\n", (station.Name), station.Tim, station.StationState, station.LowHeight);
                }
                var dlAgvOri = point.GetAgvAngle(point.LockedAgv?.AgvNo);
                if (dlAgvOri != 0)
                {
                    rectangle.ToolTip += string.Format($"\n车头角度: {dlAgvOri}");
                }
                rectangle.Width = App.GsWidth;
                rectangle.Height = App.GsHeight;
                rectangle.Fill = new SolidColorBrush(Colors.Transparent);
                rectangle.Stroke = new SolidColorBrush(Colors.DarkGray);
                double[] xy = DrawSharp(point.IntX, point.IntY);
                rectangle.SetValue(Canvas.TopProperty, xy[1]);
                rectangle.SetValue(Canvas.LeftProperty, xy[0]);
                rectangle.SetValue(Panel.ZIndexProperty, 10);

                App.Canvas_Monit.Children.Add(rectangle);
                App.ShelfSharp.Add(rectangle);
            }
        }

        /// <summary>
        /// 小车绘制
        /// </summary>
        public void AgvPointSet()
        {
            foreach (Base_Agv agv in App.AgvList)
            {
                var ellipse = App.AgvSharp.FirstOrDefault(new Func<Ellipse, bool>(a => a.Name == agv.AgvNo));
                if (ellipse != null)
                {
                    throw new Exception(agv.AgvNo + "小车已存在地图中");
                }

                ellipse = new Ellipse();
                ellipse.Name = agv.AgvNo;
                ellipse.ToolTip = ellipse.Name;
                ellipse.Width = App.GsWidth;
                ellipse.Height = App.GsHeight;
                ellipse.Fill = new SolidColorBrush(Colors.Transparent);
                ellipse.Stroke = new SolidColorBrush(Colors.Tan);
                ellipse.StrokeThickness = 12;
                ToolTipService.SetInitialShowDelay(ellipse, 100);
                ToolTipService.SetShowDuration(ellipse, 50000);    // 显示时长为 5000 毫秒
                ellipse.ToolTipOpening += Ellipse_ToolTipOpening;
                var point = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
                if (point != null)
                {
                    double[] xy = DrawSharp(point.IntX, point.IntY);
                    ellipse.SetValue(Canvas.TopProperty, xy[1]);
                    ellipse.SetValue(Canvas.LeftProperty, xy[0]);
                    ellipse.SetValue(Panel.ZIndexProperty, 50);
                }
                App.Canvas_Monit.Children.Add(ellipse);
                App.AgvSharp.Add(ellipse);
                if (!agv.IsEnable)
                {
                    ellipse.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    ellipse.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        private void Ellipse_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            if (sender is Ellipse ellipse)
            {
                var agv = App.AgvList.Find(x => x.AgvNo == ellipse.Name) ?? throw new Exception($"agv{ellipse.Name}不存在");
                var toolTip = @$"小车编号:{agv.AgvNo}
是否在线:{(agv.IsOnline ? "是" : "否")}
运行状态:{agv.AgvState}
货物状态:{agv.SensorStatus}
顶升高度:{agv.AgvHeight}
当前坐标:{agv.XLength},{agv.YLength}
异常信息:{agv.Error}";
                ellipse.ToolTip = toolTip;
            }
        }

        /// <summary>
        /// 地图坐标点绘制
        /// </summary>
        public void PointSet()
        {
            foreach (Base_Point point in App.PointList.FindAll(a => a.PointType != EnumMsg.PointType.死点))
            {

                var sb = new StringBuilder();
                sb.AppendLine($" 地标编码::{point.Barcode}");
                sb.AppendLine($" 点位类型:{point.PointType}");
                sb.AppendLine($" 地图位置:{point.IntX}, {point.IntY}");
                sb.AppendLine($" 坐标:{point.XLength}, {point.YLength}");
                //区域类型
                //sb.AppendLine("AreaType:{0}", point.pointAreaType.ToString();
                //点方向锁
                if (point.XPosPoint != null)
                {
                    sb.AppendLine($" X+:{point.IsXPos} 间距:{Math.Abs(point.XPosPoint.YLength - point.YLength)}");

                }
                if (point.XNegPoint != null)
                {
                    sb.AppendLine($" X-:{point.IsXNeg} 间距:{Math.Abs(point.XNegPoint.YLength - point.YLength)}");

                }
                if (point.YPosPoint != null)
                {
                    sb.AppendLine($" Y+:{point.IsYPos} 间距:{Math.Abs(point.YPosPoint.XLength - point.XLength)}");

                }
                if (point.YNegPoint != null)
                {
                    sb.AppendLine($" Y-:{point.IsYNeg} 间距:{Math.Abs(point.YNegPoint.XLength - point.XLength)}");
                }
                if (!string.IsNullOrEmpty(point.RegionName))
                {
                    sb.AppendLine($" 限制区域:{point.RegionName}");
                }

                var ellipse = new Ellipse();
                ellipse.ToolTip = sb.ToString();
                ellipse.Name = "Code_" + point.Barcode;
                ToolTipService.SetInitialShowDelay(ellipse, 100);
                ToolTipService.SetShowDuration(ellipse, 50000);    // 显示时长为 5000 毫秒

                ellipse.Tag = sb.ToString();
                ellipse.ToolTipOpening += PointEllipse_ToolTipOpening;
                //大小
                ellipse.Width = 12;
                ellipse.Height = 12;
                switch (point.PointType)
                {
                    case EnumMsg.PointType.普通行走点:
                        ellipse.Fill = new SolidColorBrush(Colors.Maroon);
                        break;

                    case EnumMsg.PointType.充电点:
                        ellipse.Fill = new SolidColorBrush(Colors.Green);
                        break;

                    case EnumMsg.PointType.站台点:
                        ellipse.Fill = new SolidColorBrush(Colors.Blue);
                        break;

                    case EnumMsg.PointType.站台附属点:
                    case EnumMsg.PointType.暂停点:
                        ellipse.Fill = new SolidColorBrush(Colors.LightBlue);
                        break;

                    case EnumMsg.PointType.走弧点:
                    case EnumMsg.PointType.虚拟点:
                    case EnumMsg.PointType.电梯点:
                    case EnumMsg.PointType.电梯等待点:
                    case EnumMsg.PointType.电梯关门点:
                    case EnumMsg.PointType.对接点:
                    case EnumMsg.PointType.对接等待点:
                    case EnumMsg.PointType.托盘旋转点:
                        ellipse.Fill = new SolidColorBrush(Colors.Orange);
                        break;

                    case EnumMsg.PointType.避让点:
                    case EnumMsg.PointType.回家点:
                        ellipse.Fill = new SolidColorBrush(Colors.Orange);
                        break;

                    case EnumMsg.PointType.旋转点:
                    case EnumMsg.PointType.整体旋转点:
                        ellipse.Fill = new SolidColorBrush(Colors.Blue);
                        ellipse.Width = 14;
                        ellipse.Height = 14;
                        break;

                    case EnumMsg.PointType.旋转附属点:
                        ellipse.Fill = new SolidColorBrush(Colors.LightYellow);
                        break;

                    case EnumMsg.PointType.死点:
                        ellipse.Fill = new SolidColorBrush(Colors.Black);
                        ellipse.Width = 16;
                        ellipse.Height = 16;
                        break;

                    default:
                        //throw new Exception(point.strBarcode + "未配色点属性" + point.pointType);
                        break;
                }
                int[] xy = DrawPoint(point.IntX, point.IntY, (int)ellipse.Width);

                ellipse.SetValue(Canvas.TopProperty, (double)xy[1]);
                ellipse.SetValue(Canvas.LeftProperty, (double)xy[0]);
                ellipse.SetValue(Panel.ZIndexProperty, 100);

                App.Canvas_Monit.Children.Add(ellipse);
                App.PointSharp.Add(ellipse);
            }
        }

        private void PointEllipse_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            if (sender is Ellipse ellipse)
            {
                var point = App.PointList.Find(x => x.Barcode == ellipse.Name[5..]) ?? throw new Exception($"Point{ellipse.Name}不存在");
                var toolTip = ellipse.Tag.ToString();
                if (point.LockedAgv != null)
                {
                    toolTip += $" 被AGV锁定:{point.LockedAgv.AgvNo}";
                }
                ellipse.ToolTip = toolTip;
            }
        }

        /// <summary>
        /// 显示站台名称
        /// </summary>
        public void TextTostring()
        {
            var stationGroupList = App.StationList.GroupBy(t => t.Barcode).ToList();

            foreach (var stationGroup in stationGroupList)
            {
                var station = stationGroup.First();
                var point = App.PointList.Find(x => x.Barcode == station.Barcode) ?? throw new Exception($"站台{station.Name}对应的点{station.Barcode}为空");
                //取出当前站台的名称,和第一个比较,如果不一样旧显示名字,不显示重复的。
                var stationName = string.Empty;
                var direction = EnumMsg.Direction.无方向;
                if (station.Name.IsNullOrEmpty()) throw new Exception("站台名称为空");

                stationName = station.Name.Replace("-01", "").Replace("-", "");
                if (stationName.Length >= 5 && !stationName.StartsWith("B"))
                {
                    stationName = stationName[..5];
                }

                //if (point!.IsXNeg)
                //{
                //    direction = EnumMsg.Direction.X正方向;
                //}
                //else if (point!.IsXPos)
                //{
                //    direction = EnumMsg.Direction.X负方向;
                //}
                //else if (point!.IsYNeg)
                //{
                //    direction = EnumMsg.Direction.Y正方向;
                //}
                //else if (point!.IsYPos)
                //{
                //    direction = EnumMsg.Direction.Y负方向;
                //}

                if (point!.IsYNeg)
                {
                    direction = EnumMsg.Direction.Y正方向;
                }
                else
                {
                    direction = EnumMsg.Direction.Y负方向;
                }

                if (stationName != "" && direction != EnumMsg.Direction.无方向)
                {
                    double[] xy = new double[2];
                    var tempPoint = App.PointList.First(a => a.Barcode == station.Barcode);
                    switch (direction)
                    {
                        case EnumMsg.Direction.Y负方向:
                            xy = DrawSharp(tempPoint.IntX, tempPoint.IntY - 1);
                            break;
                        case EnumMsg.Direction.Y正方向:
                            xy = DrawSharp(tempPoint.IntX, tempPoint.IntY + 1);
                            break;
                        case EnumMsg.Direction.X正方向:
                            xy = DrawSharp(tempPoint.IntX + 1, tempPoint.IntY);
                            break;
                        case EnumMsg.Direction.X负方向:
                            xy = DrawSharp(tempPoint.IntX - 1, tempPoint.IntY);
                            break;
                    }
                    var tb = new TextBox();
                    if (direction == EnumMsg.Direction.Y正方向)
                    {
                        Thickness margin = new Thickness(0, App.GsWidth / 2 - 2, 0, 0);
                        tb.Margin = margin;
                    }
                    Version osVersion = Environment.OSVersion.Version;
                    //if (osVersion.Major >= 10)
                    //    tb.Height = App.GsWidth / 2 - 2;
                    tb.TextWrapping = System.Windows.TextWrapping.Wrap;
                    tb.Foreground = new SolidColorBrush(Color.FromRgb(200, 100, 100));
                    tb.FontSize = 10;
                    tb.FontFamily = new FontFamily("Segoe UI");
                    tb.IsReadOnly = true;
                    tb.Text = stationName;
                    tb.SetValue(Canvas.TopProperty, xy[1]);
                    tb.SetValue(Canvas.LeftProperty, xy[0]);
                    App.Canvas_Monit.Children.Add(tb);
                }
            }
        }

        /// <summary>
        /// 绘制地图点
        /// </summary>
        /// <param name="intX"></param>
        /// <param name="intY"></param>
        /// <returns></returns>
        public int[] DrawPoint(int intX, int intY, int flag)
        {
            int y = 0, x = 0;
            if (App.Rotote == 2)
            {
                y = ((int)App.HeightMax - intY) * App.GsHeight + App.GsHeight / 2 - flag / 2 + 1;
                x = (intX - (int)App.WidthMin) * App.GsWidth + App.GsWidth / 2 - flag / 2 + 1;
            }
            else
            {
                y = (intY - (int)App.HeightMin) * App.GsHeight + App.GsHeight / 2 - flag / 2 + 1;
                x = ((int)App.WidthMax - intX) * App.GsWidth + App.GsWidth / 2 - flag / 2 + 1;
            }
            return new int[] { x, y };
        }

        /// <summary>
        /// 绘制小车货架
        /// </summary>
        /// <param name="intX"></param>
        /// <param name="intY"></param>
        /// <returns></returns>
        public double[] DrawSharp(int intX, int intY)
        {
            int y = 0, x = 0;
            if (App.Rotote == 2)
            {
                y = ((int)App.HeightMax - intY) * App.GsHeight + 1;
                x = (intX - (int)App.WidthMin) * App.GsWidth + 1;
            }
            else
            {
                y = (intY - (int)App.HeightMin) * App.GsHeight + 1;
                x = ((int)App.WidthMax - intX) * App.GsWidth + 1;
            }
            return new double[] { x, y };
        }

        /// <summary>
        /// 小车移动绘制
        /// </summary>
        /// <param name="agv"></param>
        public void AgvChange(Base_Agv agv)
        {
            App.AppDispatcher.Invoke(new Action(() =>
            {
                var point = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
                var ellipse = App.AgvSharp.FirstOrDefault(new Func<Ellipse, bool>(a => a.Name == agv.AgvNo));
                if (ellipse == null)
                {
                    throw new Exception(agv.AgvNo + "在地图中未添加");
                }
                if (ellipse.Visibility != System.Windows.Visibility.Visible)
                {
                    ellipse.Visibility = System.Windows.Visibility.Visible;
                }
                if (!agv.IsEnable)
                {
                    ellipse.Visibility = System.Windows.Visibility.Hidden;
                }
                double[] xy = { 0, 0 };
                if (point != null)
                {
                    xy = DrawSharp(point.IntX, point.IntY);
                }
                ellipse.SetValue(Canvas.LeftProperty, xy[0]);
                ellipse.SetValue(Canvas.TopProperty, xy[1]);
            }));
        }

        /// <summary>
        /// 小车状态绘制
        /// </summary>
        /// <param name="Agv"></param>
        public void AgvStateChange(Base_Agv agv)
        {
            App.AppDispatcher.Invoke(new Action(() =>
            {
                var ellipse = App.AgvSharp.FirstOrDefault(new Func<Ellipse, bool>(a => a.Name == agv.AgvNo));
                if (ellipse == null)
                {
                    throw new Exception(agv.AgvNo + "在地图中未添加");
                }
                if (agv.Error != "")
                {
                    ellipse.Stroke = new SolidColorBrush(Colors.Red);
                }
                else if (!agv.IsOnline)
                {
                    ellipse.Stroke = new SolidColorBrush(Colors.Tan);
                }
                else if (agv.AgvState == EnumMsg.AGVState.自动空闲)
                {

                    if (!agv.StopReason.IsNullOrEmpty())
                    {
                        //// 创建一个线性渐变画刷
                        //if (linearGradientBrush == null)
                        //{
                        //    linearGradientBrush = new LinearGradientBrush();
                        //    linearGradientBrush.StartPoint = new Point(0, 0);
                        //    linearGradientBrush.EndPoint = new Point(1, 0);
                        //    linearGradientBrush.GradientStops.Add(new GradientStop(Colors.Blue, 0.0));
                        //    linearGradientBrush.GradientStops.Add(new GradientStop(Colors.Cyan, 1.0));
                        //}


                        //// 创建颜色动画

                        //if (animation == null)
                        //{
                        //    animation = new ColorAnimation();
                        //    animation.From = Colors.Blue;
                        //    animation.To = Colors.Cyan;
                        //    animation.Duration = TimeSpan.FromSeconds(2);
                        //    animation.AutoReverse = true;
                        //    animation.RepeatBehavior = RepeatBehavior.Forever;
                        //}
                        //if (ellipse.Stroke != linearGradientBrush)
                        //{
                        //    // 将渐变画刷应用到需要动画的对象上
                        //    ellipse.Stroke = linearGradientBrush;
                        //    // 将动画应用到渐变画刷的颜色属性
                        //    linearGradientBrush.BeginAnimation(LinearGradientBrush.GradientStopsProperty, animation);
                        //}
                        ellipse.Stroke = new SolidColorBrush(Colors.Cyan);
                    }
                    else
                    {
                        ellipse.Stroke = new SolidColorBrush(Colors.Cyan);
                    }
                }
                else if (agv.AgvState == EnumMsg.AGVState.自动忙碌)
                    ellipse.Stroke = new SolidColorBrush(Colors.Blue);
                else if (agv.AgvState == EnumMsg.AGVState.充电中)
                    ellipse.Stroke = new SolidColorBrush(Colors.Green);
                else
                    ellipse.Stroke = new SolidColorBrush(Colors.Yellow);
            }));
        }


        public void DrawMapLine(List<Base_Point> points)
        {
            var newPoints = new List<Base_Point>();
            Line line;

            //line = new Line
            //{
            //    X1 = 1,
            //    X2 = 1000,
            //    Y1 = 1,
            //    Y2 = 1000,
            //    StrokeThickness = 3,
            //    Stroke = Brushes.Black,
            //};

            //App.Canvas_Monit.Children.Add(line);

            foreach (var point in points)
            {
                var aroundPoints = ComnMethod.GetAroundPoint(point);
                foreach (var aroundPoint in aroundPoints)
                {

                    var start = DrawPoint(point.IntX, point.IntY, 0);
                    var end = DrawPoint(aroundPoint.IntX, aroundPoint.IntY, 0);
                    line = new Line
                    {
                        X1 = start[0],
                        X2 = end[0],
                        Y1 = start[1],
                        Y2 = end[1],
                        StrokeThickness = 1,
                        Stroke = Brushes.Gray,
                    };
                    if (!App.MapLines.Any(x => x.X1 == line.X2 && x.X2 == line.X1 && x.Y1 == line.Y2 && x.Y2 == line.Y1))
                    {
                        line.SetValue(Panel.ZIndexProperty, 1);
                        App.MapLines.Add(line);
                        App.Canvas_Monit.Children.Add(line);
                        //int[] xy = DrawPoint(point.IntX, point.IntY, (int)line.StrokeThickness);

                        //line.SetValue(Canvas.TopProperty, (double)xy[1]);
                        //line.SetValue(Canvas.LeftProperty, (double)xy[0]);

                    }

                }

            }
        }
    }
}