LineExtension.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.SystemExtensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FWindSoft.Revit
  9. {
  10. public static class LineExtension
  11. {
  12. /// <summary>
  13. /// 两直线平行
  14. /// </summary>
  15. /// <param name="line1"></param>
  16. /// <param name="line2"></param>
  17. /// <returns></returns>
  18. public static bool IsParallel(this Line line1, Line line2)
  19. {
  20. return line1.Direction.IsParallel(line2.Direction);
  21. }
  22. /// <summary>
  23. /// 获取线的端点与指定点 最远的点
  24. /// </summary>
  25. /// <param name="line"></param>
  26. /// <param name="point"></param>
  27. /// <returns></returns>
  28. public static XYZ GetFarnessPoint(this Line line, XYZ point)
  29. {
  30. XYZ start = line.StartPoint();
  31. XYZ end = line.EndPoint();
  32. return start.DistanceTo(point).Less(end.DistanceTo(point)) ? end : start;
  33. }
  34. /// <summary>
  35. /// 获取线的端点与指定点 最近的点
  36. /// </summary>
  37. /// <param name="line"></param>
  38. /// <param name="point"></param>
  39. /// <returns></returns>
  40. public static XYZ GetNearnessPoint(this Line line, XYZ point)
  41. {
  42. XYZ start = line.StartPoint();
  43. XYZ end = line.EndPoint();
  44. return start.DistanceTo(point).Less(end.DistanceTo(point)) ? start : end;
  45. }
  46. /// <summary>
  47. /// 获取空间直线的垂足点
  48. /// </summary>
  49. /// <param name="baseLine"></param>
  50. /// <param name="line2"></param>
  51. /// <returns></returns>
  52. public static XYZ GetSpatialIntersection(this Line baseLine, Line line2)
  53. {
  54. //两条法向量构建,和baseLine上一点构建平面,line2中的点在平面内的投影线。求投影线和baseLine1两条直线的夹角
  55. XYZ basePoint = null;
  56. do
  57. {
  58. if (baseLine.IsParallel(line2))
  59. {
  60. break;
  61. }
  62. var normal = baseLine.Direction.CrossProduct(line2.Direction);
  63. var origin = baseLine.Origin;
  64. var start = line2.Origin;
  65. var end = start + 10 * line2.Direction;
  66. if (line2.IsBound)
  67. {
  68. start = line2.StartPoint();
  69. end = line2.EndPoint();
  70. }
  71. //取投影点
  72. XYZ point1 = start.GetPlaneProject(normal, origin);
  73. XYZ point2 = end.GetPlaneProject(normal, origin);
  74. if (point1.IsEqual(point2))
  75. {
  76. basePoint = point1;
  77. }
  78. else
  79. {
  80. var unBoundLine1 = baseLine.CloneUnbound();
  81. var unboundLine2 = point1.NewLine(point2).CloneUnbound();
  82. basePoint = unBoundLine1.GetIntersection(unboundLine2);
  83. }
  84. } while (false);
  85. return basePoint;
  86. }
  87. /// <summary>
  88. /// 是否共线
  89. /// </summary>
  90. /// <param name="baseLine"></param>
  91. /// <param name="line2"></param>
  92. /// <returns></returns>
  93. public static bool IsCollinear(this Line baseLine,Line line2)
  94. {
  95. return baseLine.IsParallel(line2) && baseLine.Direction.IsParallel(line2.Origin - baseLine.Origin);
  96. }
  97. /// <summary>
  98. /// 获取输入点在直线上的投影点
  99. /// </summary>
  100. /// <param name="line"></param>
  101. /// <param name="input"></param>
  102. /// <returns></returns>
  103. public static XYZ GetProject(this Line line, XYZ input)
  104. {
  105. var origin = line.Origin;
  106. var direction = line.Direction;
  107. var useVector = input - origin;
  108. var project = useVector.DotProduct(direction) * direction + origin;
  109. return project;
  110. }
  111. }
  112. }