MepCurveExtension.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Autodesk.Revit.DB;
  2. using Autodesk.Revit.DB.Electrical;
  3. using Autodesk.Revit.DB.Mechanical;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FWindSoft.Revit
  10. {
  11. public static class MepCurveExtension
  12. {
  13. /// <summary>
  14. /// 是否是水平的
  15. /// </summary>
  16. /// <param name="mepCurve"></param>
  17. /// <returns></returns>
  18. public static bool IsHorizontal(this MEPCurve mepCurve)
  19. {
  20. var line = mepCurve.GetLocationCurve() as Line;
  21. if (line == null)
  22. return false;
  23. return line.Direction.IsVertical(XYZ.BasisZ);
  24. }
  25. /// <summary>
  26. /// 是否垂直的
  27. /// </summary>
  28. /// <param name="mepCurve"></param>
  29. /// <returns></returns>
  30. public static bool IsVertical(this MEPCurve mepCurve)
  31. {
  32. var line = mepCurve.GetLocationCurve() as Line;
  33. if (line == null)
  34. return false;
  35. return line.Direction.IsParallel(XYZ.BasisZ);
  36. }
  37. /// <summary>
  38. /// 复制指定的mepCurve
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <param name="mepCurve"></param>
  42. /// <param name="start"></param>
  43. /// <param name="end"></param>
  44. /// <returns></returns>
  45. public static T Copy<T>(this T mepCurve, XYZ start, XYZ end) where T : MEPCurve
  46. {
  47. var doc = mepCurve.Document;
  48. var ids = ElementTransformUtils.CopyElement(doc, mepCurve.Id, XYZ.Zero);
  49. T copy = null;
  50. if (ids.Count > 0)
  51. {
  52. copy = doc.GetElement(ids.FirstOrDefault()) as T;
  53. }
  54. copy.UpdateLocation(start, end);
  55. return copy;
  56. }
  57. /// <summary>
  58. /// 更新指定mepCurve的迹线
  59. /// </summary>
  60. /// 2015-09-15 xls
  61. /// <param name="mepCurve"></param>
  62. /// <param name="start"></param>
  63. /// <param name="end"></param>
  64. public static void UpdateLocation(this MEPCurve mepCurve, XYZ start, XYZ end)
  65. {
  66. if (start.IsEqual(end))
  67. return;
  68. var locationCurve = mepCurve.Location as LocationCurve;
  69. Line newLine =start.NewLine(end);
  70. locationCurve.Curve = newLine;
  71. //mepCurve.Rotate(newLine, Math.PI * 2);
  72. }
  73. /// <summary>
  74. /// 刷新mepCurve,mepCurve绕迹线旋转360度
  75. /// </summary>
  76. /// <param name="mepCurve"></param>
  77. public static void Refresh(this MEPCurve mepCurve)
  78. {
  79. var locationCurve = mepCurve.Location as LocationCurve;
  80. mepCurve.Rotate(mepCurve.GetLocationLine(), Math.PI * 2);
  81. }
  82. /// <summary>
  83. /// 替换mepCurve指定点位新的坐标
  84. /// </summary>
  85. /// <param name="mepCurve">要修改的MepCurve</param>
  86. /// <param name="oldPoint">将要被替换的点的位置</param>
  87. /// <param name="newPoint">新的点的位置</param>
  88. public static void ReplaceLocation(this MEPCurve mepCurve, XYZ oldPoint, XYZ newPoint)
  89. {
  90. //按最接近的点,找到需要替换的点的位置
  91. var oldLine = mepCurve.GetLocationLine();
  92. oldPoint = oldLine.GetNearnessPoint(oldPoint);
  93. if (oldPoint.IsEqual(newPoint))
  94. return;
  95. Line newLine = null;
  96. if (oldLine.StartPoint().IsEqual(oldPoint))
  97. {
  98. newLine = newPoint.NewLine(oldLine.EndPoint());
  99. }
  100. else
  101. {
  102. newLine = oldLine.StartPoint().NewLine(newPoint);
  103. }
  104. if (newLine != null)
  105. {
  106. var locationCurve = mepCurve.Location as LocationCurve;
  107. locationCurve.Curve = newLine;
  108. //mepCurve.Rotate(newLine, Math.PI * 2);
  109. }
  110. }
  111. /// <summary>
  112. /// 获取距离给定点最近的点connector
  113. /// </summary>
  114. /// <param name="element"></param>
  115. /// <param name="point"></param>
  116. /// <returns></returns>
  117. public static Connector GetNearnessConnector(this MEPCurve element, XYZ point)
  118. {
  119. var line = element.GetLocationCurve() as Line;
  120. return element.GetConnectorByOrigin(line.GetNearnessPoint(point));
  121. }
  122. /// <summary>
  123. /// 获取距离给定点最远的点connector
  124. /// </summary>
  125. /// <param name="element"></param>
  126. /// <param name="point"></param>
  127. /// <returns></returns>
  128. public static Connector GetFarnessConnector(this MEPCurve element, XYZ point)
  129. {
  130. var line = element.GetLocationCurve() as Line;
  131. return element.GetConnectorByOrigin(line.GetFarnessPoint(point));
  132. }
  133. /// <summary>
  134. /// 设置mepCurve的朝向(有效构件为方向风管或者桥架)
  135. /// </summary>
  136. /// <param name="mepCurve"></param>
  137. /// <param name="normal"></param>
  138. public static void SetCurveNormal(this MEPCurve mepCurve, XYZ normal)
  139. {
  140. if (mepCurve is CableTray cableTray)
  141. {
  142. cableTray.CurveNormal = normal;
  143. }
  144. else if (mepCurve is Duct duct)
  145. {
  146. duct.SetCurveNormal(normal);
  147. }
  148. }
  149. }
  150. }