LocationExtend.cs 2.6 KB

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