GeometryElementExtension.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. namespace FWindSoft.Revit
  8. {
  9. public static class GeometryElementExtension
  10. {
  11. /// <summary>
  12. /// 查找原始的几何对象
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="geoElement"></param>
  16. /// <returns></returns>
  17. public static List<T> GetGeometryOriginalObjects<T>(this GeometryElement geoElement) where T : GeometryObject
  18. {
  19. List<T> listRtn = new List<T>();
  20. List<GeometryElement> useElements = new List<GeometryElement>() { geoElement };
  21. for (int i = 0; i < useElements.Count; i++)
  22. {
  23. var useGeoElement = useElements[i];
  24. foreach (var geoObject in useGeoElement)
  25. {
  26. //系统族、相交切割后的外部族
  27. if (geoObject is T)
  28. {
  29. listRtn.Add(geoObject as T);
  30. }
  31. else if (geoObject is GeometryInstance)
  32. {
  33. var childElment = (geoObject as GeometryInstance).GetInstanceGeometry();
  34. if (childElment != null)
  35. {
  36. useElements.Add(childElment);
  37. }
  38. }
  39. else if (geoObject is GeometryElement)
  40. {
  41. useElements.Add(geoObject as GeometryElement);
  42. }
  43. }
  44. }
  45. return listRtn;
  46. }
  47. public static List<T> GetGeometryObjects<T>(this GeometryElement geoElement) where T : GeometryObject
  48. {
  49. List<T> listRtn = geoElement.GetGeometryOriginalObjects<T>();
  50. if (listRtn.Count==0)
  51. {
  52. //计算为几何信息复制,可能引用相关有问题
  53. var geoElementNew = geoElement.GetTransformed(Transform.Identity);
  54. listRtn = geoElementNew.GetGeometryOriginalObjects<T>();
  55. }
  56. return listRtn;
  57. }
  58. /// <summary>
  59. /// 获取指定元素的几何描述
  60. /// </summary>
  61. /// <param name="element"></param>
  62. /// <param name="visibleObj"></param>
  63. /// <param name="view"></param>
  64. /// <returns></returns>
  65. public static GeometryElement GetGeometryElement(this Element element, bool visibleObj = false, View view = null)
  66. {
  67. var options = new Options();
  68. options.ComputeReferences = true;
  69. options.IncludeNonVisibleObjects = visibleObj;
  70. if (view == null)
  71. {
  72. options.DetailLevel = ViewDetailLevel.Fine;
  73. }
  74. else
  75. {
  76. options.View = view;
  77. }
  78. return element.get_Geometry(options);
  79. }
  80. /// <summary>
  81. /// 获取几何Solid
  82. /// </summary>
  83. /// <param name="elem"></param>
  84. /// <param name="view"></param>
  85. /// <param name="visibleObj"></param>
  86. /// <returns></returns>
  87. public static List<Solid> GetSolids(this Element elem, View view = null, bool visibleObj = false)
  88. {
  89. var geoElement = elem.GetGeometryElement(visibleObj, view);
  90. return geoElement.GetGeometryObjects<Solid>();
  91. }
  92. /// <summary>
  93. /// 获取几何Solid;最大的默认第一个
  94. /// </summary>
  95. /// <param name="elem"></param>
  96. /// <returns></returns>
  97. public static Solid GetSolid(this Element elem)
  98. {
  99. Solid solid = null;
  100. List<Solid> listRtn = elem.GetSolids();
  101. if (listRtn != null && listRtn.Count > 0)
  102. {
  103. solid = listRtn[0];
  104. }
  105. return solid;
  106. }
  107. #region 当前集合信息
  108. /// <summary>
  109. /// 获取元素当前的面信息
  110. /// </summary>
  111. /// <param name="elem"></param>
  112. /// <param name="view"></param>
  113. /// <returns></returns>
  114. public static List<Face> GetFaces(this Element elem, View view = null)
  115. {
  116. var listRtn = new List<Face>();
  117. List<Solid> listSoild = elem.GetSolids(view);
  118. foreach (var solid in listSoild)
  119. {
  120. listRtn.AddRange(solid.Faces.OfType<Face>());
  121. }
  122. return listRtn;
  123. }
  124. #endregion
  125. #region 原始为切割几何信息
  126. /// <summary>
  127. /// 未切割的Edge
  128. /// </summary>
  129. /// <param name="instance"></param>
  130. /// <returns></returns>
  131. public static List<Edge> GetOriginalEdges(this FamilyInstance instance)
  132. {
  133. var listRtn = new List<Edge>();
  134. List<Solid> listSoild = instance.GetOriginalSolids();
  135. foreach (var solid in listSoild)
  136. {
  137. listRtn.AddRange(solid.Edges.OfType<Edge>());
  138. }
  139. return listRtn;
  140. }
  141. /// <summary>
  142. /// 法向相等的某一面 ljy
  143. /// </summary>
  144. /// <param name="instance"></param>
  145. /// <param name="normal"></param>
  146. /// <returns></returns>
  147. public static List<Face> GetOriginalFaces(this FamilyInstance instance, XYZ normal)
  148. {
  149. var faces = instance.GetOriginalFaces();
  150. return faces.Where(p => p is PlanarFace&& (p as PlanarFace).FaceNormal.IsSameDirection(normal)).ToList();
  151. }
  152. /// <summary>
  153. /// 未切割的Face
  154. /// </summary>
  155. /// <param name="instance"></param>
  156. /// <returns></returns>
  157. public static List<Face> GetOriginalFaces(this FamilyInstance instance)
  158. {
  159. var listRtn = new List<Face>();
  160. List<Solid> listSoild = instance.GetOriginalSolids();
  161. foreach (var solid in listSoild)
  162. {
  163. listRtn.AddRange(solid.Faces.OfType<Face>());
  164. }
  165. return listRtn;
  166. } /// <summary>
  167. /// 未切割的Solid
  168. /// </summary>
  169. /// <param name="instance"></param>
  170. /// <returns></returns>
  171. public static List<Solid> GetOriginalSolids(this FamilyInstance instance)
  172. {
  173. var options = new Options();
  174. //不能计算reference
  175. options.ComputeReferences = false;
  176. var geoElement = instance.GetOriginalGeometry(options);
  177. if (geoElement == null)
  178. {
  179. return null;
  180. }
  181. var transform = instance.GetTransform();
  182. geoElement = geoElement.GetTransformed(transform);
  183. var listSoild = geoElement.GetGeometryObjects<Solid>();
  184. if (listSoild.Count == 0)
  185. {
  186. listSoild = new List<Solid>();
  187. }
  188. return listSoild;
  189. }
  190. #endregion
  191. }
  192. }