SegmentLink.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Linq;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. namespace SHJX.Service.WorkFlowEdit
  5. {
  6. public class SegmentLink : LinkBase
  7. {
  8. static SegmentLink()
  9. {
  10. DefaultStyleKeyProperty.OverrideMetadata(
  11. typeof(SegmentLink), new FrameworkPropertyMetadata(typeof(LinkBase)));
  12. }
  13. public override void UpdatePath()
  14. {
  15. var linePoints = CalculateSegments();
  16. if (CheckPoints(linePoints))
  17. {
  18. CalculatePositions(linePoints);
  19. PathGeometry geometry = new PathGeometry();
  20. PathFigure figure = new PathFigure
  21. {
  22. StartPoint = linePoints[0]
  23. };
  24. figure.Segments.Add(new PolyLineSegment(linePoints.Skip(1), true));
  25. geometry.Figures.Add(figure);
  26. this.PathGeometry = geometry;
  27. }
  28. else
  29. this.PathGeometry = null;
  30. }
  31. protected virtual Point[] CalculateSegments()
  32. {
  33. var res = GetEndPoinds();
  34. if (res != null)
  35. UpdateEdges(res);
  36. return res;
  37. }
  38. protected Point[] GetEndPoinds()
  39. {
  40. Point tc, sc;
  41. if (Target != null)
  42. tc = Target.Center;
  43. else if (TargetPoint != null)
  44. tc = TargetPoint.Value;
  45. else
  46. return null;
  47. if (Source != null)
  48. sc = Source.Center;
  49. else if (SourcePoint != null)
  50. sc = SourcePoint.Value;
  51. else
  52. return null;
  53. var linePoints = new Point[2];
  54. linePoints[0] = sc;
  55. linePoints[1] = tc;
  56. return linePoints;
  57. }
  58. protected void UpdateEdges(Point[] linePoints)
  59. {
  60. if (linePoints.Length >= 2)
  61. {
  62. if (Source != null)
  63. linePoints[0] = Source.GetEdgePoint(linePoints[1]);
  64. if (Target != null)
  65. linePoints[linePoints.Length - 1] = Target.GetEdgePoint(linePoints[linePoints.Length - 2]);
  66. }
  67. }
  68. protected virtual void CalculatePositions(Point[] linePoints)
  69. {
  70. StartPoint = linePoints[0];
  71. EndPoint = linePoints[linePoints.Length - 1];
  72. StartCapAngle = GeometryHelper.NormalAngle(linePoints[0], linePoints[1]);
  73. EndCapAngle = GeometryHelper.NormalAngle(linePoints[linePoints.Length - 2], linePoints[linePoints.Length - 1]);
  74. var mid = (int)(linePoints.Length / 2);
  75. var p = GeometryHelper.SegmentMiddlePoint(linePoints[mid - 1], linePoints[mid]);
  76. LabelPosition = new Point(p.X, p.Y - 15);
  77. }
  78. private bool CheckPoints(Point[] linePoints)
  79. {
  80. if (linePoints != null && linePoints.Length >= 2)
  81. {
  82. foreach (var p in linePoints)
  83. if (double.IsNaN(p.X) || double.IsNaN(p.Y))
  84. return false;
  85. return true;
  86. }
  87. return false;
  88. }
  89. }
  90. }