CurveExtension.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 CurveExtension
  11. {
  12. public static XYZ StartPoint(this Curve curve)
  13. {
  14. return curve.GetEndPoint(0);
  15. }
  16. public static XYZ EndPoint(this Curve curve)
  17. {
  18. return curve.GetEndPoint(1);
  19. }
  20. public static Line NewLine(this XYZ start,XYZ end)
  21. {
  22. return Line.CreateBound(start, end);
  23. }
  24. public static Line NewUnBoundLine(this XYZ start, XYZ direction)
  25. {
  26. return Line.CreateUnbound(start, direction);
  27. }
  28. /// <summary>
  29. /// 将线克隆成无边界模式
  30. /// </summary>
  31. /// <typeparam name="T"></typeparam>
  32. /// <param name="curve"></param>
  33. /// <returns></returns>
  34. public static T CloneUnbound<T>(this T curve) where T : Curve
  35. {
  36. var clone = curve.Clone();
  37. if (clone.IsBound)
  38. {
  39. clone.MakeUnbound();
  40. }
  41. return clone as T;
  42. }
  43. /// <summary>
  44. /// 获取曲线相交点
  45. /// </summary>
  46. public static List<XYZ> GetIntersections(this Curve curve1, Curve curve2)
  47. {
  48. List<XYZ> result = new List<XYZ>();
  49. if(curve1 == null || curve2 == null)
  50. {
  51. return result;
  52. }
  53. var setResult = curve1.Intersect(curve2, out IntersectionResultArray interResult);
  54. if (null != interResult)
  55. {
  56. foreach (IntersectionResult iResult in interResult)
  57. {
  58. result.Add(iResult.XYZPoint);
  59. }
  60. }
  61. return result;
  62. }
  63. /// <summary>
  64. /// 求交点都用些方法,有问题请讨论
  65. /// </summary>
  66. /// <param name="curve1"></param>
  67. /// <param name="curve2"></param>
  68. /// <returns></returns>
  69. public static XYZ GetIntersection(this Curve curve1, Curve curve2)
  70. {
  71. List<XYZ> intersections = curve1.GetIntersections(curve2);
  72. if (intersections != null && intersections.Count > 0)
  73. {
  74. return intersections[0];
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// 判断点是否在Curve上
  80. /// </summary>
  81. public static bool InCurve(this Curve curve, XYZ input, double tolerance = 0)
  82. {
  83. //当Curve上的,存在多个到input点距离相等,且同时为最短距离是,Distance函数可能出现异常
  84. try
  85. {
  86. if (curve is Arc arc && arc.Center.IsEqual(input, tolerance))
  87. {
  88. return false;
  89. }
  90. }
  91. catch (Exception)
  92. {
  93. return false;
  94. }
  95. return curve.Distance(input).IsEqual(0, tolerance);
  96. }
  97. }
  98. }