LocationExtend.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using Autodesk.Revit.DB;
  3. namespace SAGA.RevitUtils.Extends
  4. {
  5. public static class LocationExtend
  6. {
  7. /// <summary>
  8. /// 定位点
  9. /// </summary>
  10. /// <param name="loc"></param>
  11. /// <returns></returns>
  12. public static XYZ GetPoint(this Location loc)
  13. {
  14. XYZ point = null;
  15. if (loc is LocationPoint lp)
  16. {
  17. point = lp?.Point;
  18. }
  19. else if(loc is LocationCurve lc)
  20. {
  21. point = lc.Curve.MiddlePoint();
  22. }
  23. return point;
  24. }
  25. /// <summary>
  26. /// 转角度
  27. /// </summary>
  28. /// <param name="loc"></param>
  29. /// <returns></returns>
  30. public static double GetRotation(this Location loc)
  31. {
  32. double dRotate = 0;
  33. try
  34. {
  35. if (loc is LocationPoint lp)
  36. dRotate = lp.Rotation;//MEP中有一些族获取此参数时抛出不明异常
  37. }
  38. catch { }
  39. return dRotate;
  40. }
  41. /// <summary>
  42. /// 定位曲线
  43. /// </summary>
  44. /// <param name="loc"></param>
  45. /// <returns></returns>
  46. public static Curve GetCurve(this Location loc)
  47. {
  48. if (!(loc is LocationCurve)) return null;
  49. LocationCurve lc = loc as LocationCurve;
  50. return lc.Curve;
  51. }
  52. /// <summary>
  53. /// 定位线
  54. /// </summary>
  55. /// <param name="loc"></param>
  56. /// <returns></returns>
  57. public static Line GetLine(this Location loc)
  58. {
  59. return loc.GetCurve().GetLine();
  60. }
  61. public static bool MoveLength(this LocationCurve location, double length, bool isOuter)
  62. {
  63. Curve curve = location.Curve;
  64. XYZ translation;
  65. if (curve is Line)
  66. {
  67. Line line = curve as Line;
  68. translation = line.Direction;
  69. translation = translation.VectorRotate(isOuter ? -Math.PI / 2 : Math.PI / 2, line.StartPoint()) * length;
  70. return location.Move(translation);
  71. }
  72. else if (curve is Arc)
  73. {
  74. Arc arc = curve as Arc;
  75. XYZ center = arc.Center;
  76. XYZ middle = arc.MiddlePoint();
  77. Line line = Line.CreateBound(middle, center);
  78. translation = line.Direction;
  79. if (isOuter)
  80. translation = translation.VectorRotate(-Math.PI, line.StartPoint()) * length;
  81. else
  82. translation = translation * length;
  83. return location.Move(translation);
  84. }
  85. return false;
  86. }
  87. }
  88. }