OrthogonalLink.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Windows;
  3. using SHJX.Service.WorkFlowEdit;
  4. using System.Collections.Generic;
  5. namespace Flowchart
  6. {
  7. public class OrthogonalLink : SegmentLink
  8. {
  9. public OrthogonalLink() { }
  10. protected override Point[] CalculateSegments()
  11. {
  12. if (Source == null || Target == null)
  13. return base.CalculateSegments();
  14. var points = new List<Point>();
  15. var ends = GetEndPoinds();
  16. if (ends == null)
  17. return ends;
  18. points.Add(ends[0]);
  19. points.AddRange(GetMiddlePoints(ends[0], ends[1]));
  20. points.Add(ends[1]);
  21. var res = points.ToArray();
  22. UpdateEdges(res);
  23. return res;
  24. }
  25. private IEnumerable<Point> GetMiddlePoints(Point start, Point end)
  26. {
  27. var view = VisualHelper.FindParent<DiagramView>(this);
  28. if (view == null) return Array.Empty<Point>();
  29. var points = new List<Point>();
  30. var p0 = GetFirstSegment(Source, start, view.GridCellSize);
  31. var p1 = GetFirstSegment(Target, end, view.GridCellSize);
  32. if (p0 == p1) return points;
  33. var p2 = new Point(GetNearestCross(p0.X, p1.X), GetNearestCross(p0.Y, p1.Y));
  34. var p3 = new Point(GetNearestCross(p1.X, p0.X), GetNearestCross(p1.Y, p0.Y));
  35. if (p2 == p3)
  36. {
  37. points.Add(p0);
  38. points.Add(p2);
  39. points.Add(p1);
  40. }
  41. else
  42. {
  43. points.Add(p0);
  44. points.Add(p2);
  45. if (!GeometryHelper.AreEquals(p2.X, p3.X) && !GeometryHelper.AreEquals(p2.Y, p3.Y))
  46. {
  47. points.Add(new Point(p2.X, p3.Y));
  48. }
  49. points.Add(p3);
  50. points.Add(p1);
  51. }
  52. DoScale(points, view.GridCellSize);
  53. return points;
  54. }
  55. private void DoScale(List<Point> points, Size cellSize)
  56. {
  57. for (int i = 0; i < points.Count; i++)
  58. {
  59. points[i] = new Point(points[i].X * cellSize.Width,
  60. points[i].Y * cellSize.Height);
  61. }
  62. }
  63. private double GetNearestCross(double a, double b)
  64. {
  65. if (GeometryHelper.AreEquals(a, b) && (int)a == a)
  66. return a;
  67. else if (a < b)
  68. return Math.Ceiling(a);
  69. else
  70. return Math.Floor(a);
  71. }
  72. private Point GetFirstSegment(IPort iPort, Point point, Size cellSize)
  73. {
  74. var port = iPort as PortBase;
  75. double x = (int)(point.X / cellSize.Width) + 0.5;
  76. double y = (int)(point.Y / cellSize.Height) + 0.5;
  77. if (port.VerticalAlignment == VerticalAlignment.Top)
  78. return new Point(x, y - 0.5);
  79. else if (port.VerticalAlignment == VerticalAlignment.Bottom)
  80. return new Point(x, y + 0.5);
  81. else if (port.HorizontalAlignment == HorizontalAlignment.Left)
  82. return new Point(x - 0.5, y);
  83. else
  84. return new Point(x + 0.5, y);
  85. }
  86. }
  87. }