ElementExtension.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.Revit.DB;
  7. using Autodesk.Revit.DB.Mechanical;
  8. using Autodesk.Revit.DB.Plumbing;
  9. namespace FWindSoft.Revit
  10. {
  11. public static class ElementExtension
  12. {
  13. /// <summary>
  14. /// 获取定位线
  15. /// </summary>
  16. /// <param name="element"></param>
  17. /// <returns></returns>
  18. public static Curve GetLocationCurve(this Element element)
  19. {
  20. return element.Location?.ConvertToCurve();
  21. }
  22. /// <summary>
  23. /// 获取定位线
  24. /// </summary>
  25. /// <param name="element"></param>
  26. /// <returns></returns>
  27. public static Line GetLocationLine(this Element element)
  28. {
  29. return element.Location?.ConvertToLine();
  30. }
  31. /// <summary>
  32. /// 获取元素定位点
  33. /// </summary>
  34. /// <param name="element"></param>
  35. /// <returns></returns>
  36. public static XYZ GetLocationPoint(this Element element)
  37. {
  38. return element.Location?.ConvertToPoint();
  39. }
  40. /// <summary>
  41. /// 获取元素开始点
  42. /// </summary>
  43. /// <param name="element"></param>
  44. /// <returns></returns>
  45. public static XYZ StartPoint(this Element element)
  46. {
  47. return element.GetLocationCurve()?.StartPoint();
  48. }
  49. /// <summary>
  50. /// 获取元素结束点
  51. /// </summary>
  52. /// <param name="element"></param>
  53. /// <returns></returns>
  54. public static XYZ EndPoint(this Element element)
  55. {
  56. return element.GetLocationCurve()?.EndPoint();
  57. }
  58. public static BuiltInCategory GetCategory(this Element elem)
  59. {
  60. var bic = BuiltInCategory.INVALID;
  61. if (elem.Category != null)
  62. {
  63. bic = (BuiltInCategory)elem.Category.Id.IntegerValue;
  64. }
  65. return bic;
  66. }
  67. /// <summary>
  68. /// 元素旋转信息
  69. /// </summary>
  70. /// <param name="element"></param>
  71. /// <param name="axis"></param>
  72. /// <param name="angle"></param>
  73. public static void Rotate(this Element element, Line axis, double angle)
  74. {
  75. ElementTransformUtils.RotateElement(element.Document, element.Id, axis, angle);
  76. }
  77. /// <summary>
  78. /// 获取元素的内置分类
  79. /// </summary>
  80. /// <param name="elem"></param>
  81. /// <returns></returns>
  82. public static BuiltInCategory GetBuiltInCategory(this Element elem)
  83. {
  84. var bic = BuiltInCategory.INVALID;
  85. if (!elem.IsValidObject)
  86. return BuiltInCategory.INVALID;
  87. if (elem.Category != null && elem.Category.Id != null)
  88. {
  89. bic = (BuiltInCategory)elem.Category.Id.IntegerValue;
  90. }
  91. return bic;
  92. }
  93. public static bool IsWaterComponment(this Element element)
  94. {
  95. return element is Pipe || element is FlexPipe || (element is FamilyInstance && element.GetBuiltInCategory() == BuiltInCategory.OST_PipeFitting);
  96. }
  97. public static bool IsHavcComponment(this Element element)
  98. {
  99. return element is Duct|| element is FlexDuct || (element is FamilyInstance && element.GetBuiltInCategory() == BuiltInCategory.OST_DuctFitting);
  100. }
  101. /// <summary>
  102. /// 获取图元类型
  103. /// </summary>
  104. /// <param name="element"></param>
  105. /// <returns></returns>
  106. public static ElementType GetElementType(this Element element)
  107. {
  108. ElementId id = element.GetTypeId();
  109. if (id != null && id != ElementId.InvalidElementId)
  110. {
  111. Element elementType = element.Document.GetElement(id);
  112. return elementType as ElementType;
  113. }
  114. return null;
  115. }
  116. /// <summary>
  117. /// 获取图元类型
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <param name="element"></param>
  121. /// <returns></returns>
  122. public static T GetElementType<T>(this Element element) where T : ElementType
  123. {
  124. ElementId id = element.GetTypeId();
  125. if (id != null && id != ElementId.InvalidElementId)
  126. {
  127. Element elementType = element.Document.GetElement(id);
  128. return elementType as T;
  129. }
  130. return null;
  131. }
  132. /// <summary>
  133. /// 获取参照标高
  134. /// </summary>
  135. /// <param name="element"></param>
  136. /// <returns></returns>
  137. public static Level GetLevel(this Element element)
  138. {
  139. Level level = null;
  140. ElementId eId = element.GetLevelId();
  141. if (eId != ElementId.InvalidElementId)
  142. {
  143. level = element.Document.GetElement(eId) as Level;
  144. }
  145. return level;
  146. }
  147. /// <summary>
  148. /// 获取参照标高Id
  149. /// </summary>
  150. /// <param name="element"></param>
  151. /// <returns></returns>
  152. public static ElementId GetLevelId(this Element element)
  153. {
  154. return element.GetParameterElementId(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
  155. }
  156. /// <summary>
  157. /// 参照标高
  158. /// </summary>
  159. /// <param name="element"></param>
  160. /// <param name="level"></param>
  161. public static void SetLevel(this Element element, Level level)
  162. {
  163. if (level != null)
  164. {
  165. element.SetLevel(level.Id);
  166. }
  167. }
  168. /// <summary>
  169. /// 设置参照标高参照标高
  170. /// </summary>
  171. /// <param name="element"></param>
  172. /// <param name="levelId"></param>
  173. public static void SetLevel(this Element element, ElementId levelId)
  174. {
  175. element.SetParameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM, levelId);
  176. }
  177. }
  178. }