GeometryElementExtension.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:GeometryElementExtension
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/26 16:17:26
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. namespace RevitToJBim.Extension
  14. {
  15. public static class GeometryElementExtension
  16. {
  17. /// <summary>
  18. /// 查找原始的几何对象
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. /// <param name="geoElement"></param>
  22. /// <returns></returns>
  23. public static List<T> GetGeometryOriginalObjects<T>(this GeometryElement geoElement) where T : GeometryObject
  24. {
  25. List<T> listRtn = new List<T>();
  26. List<GeometryElement> useElements = new List<GeometryElement>() { geoElement };
  27. for (int i = 0; i < useElements.Count; i++)
  28. {
  29. var useGeoElement = useElements[i];
  30. foreach (var geoObject in useGeoElement)
  31. {
  32. //系统族、相交切割后的外部族
  33. if (geoObject is T)
  34. {
  35. listRtn.Add(geoObject as T);
  36. }
  37. else if (geoObject is GeometryInstance)
  38. {
  39. var childElment = (geoObject as GeometryInstance).GetInstanceGeometry();
  40. if (childElment != null)
  41. {
  42. useElements.Add(childElment);
  43. }
  44. }
  45. else if (geoObject is GeometryElement)
  46. {
  47. useElements.Add(geoObject as GeometryElement);
  48. }
  49. }
  50. }
  51. return listRtn;
  52. }
  53. public static List<T> GetGeometryObjects<T>(this GeometryElement geoElement) where T : GeometryObject
  54. {
  55. List<T> listRtn = geoElement.GetGeometryOriginalObjects<T>();
  56. if (listRtn.Count == 0)
  57. {
  58. //计算为几何信息复制,可能引用相关有问题
  59. var geoElementNew = geoElement.GetTransformed(Transform.Identity);
  60. listRtn = geoElementNew.GetGeometryOriginalObjects<T>();
  61. }
  62. return listRtn;
  63. }
  64. /// <summary>
  65. /// 获取指定元素的几何描述
  66. /// </summary>
  67. /// <param name="element"></param>
  68. /// <param name="visibleObj"></param>
  69. /// <param name="view"></param>
  70. /// <returns></returns>
  71. public static GeometryElement GetGeometryElement(this Element element, bool visibleObj = false, View view = null)
  72. {
  73. var options = new Options();
  74. options.ComputeReferences = true;
  75. options.IncludeNonVisibleObjects = visibleObj;
  76. if (view == null)
  77. {
  78. options.DetailLevel = ViewDetailLevel.Fine;
  79. }
  80. else
  81. {
  82. options.View = view;
  83. }
  84. return element.get_Geometry(options);
  85. }
  86. /// <summary>
  87. /// 获取几何Solid
  88. /// </summary>
  89. /// <param name="elem"></param>
  90. /// <param name="view"></param>
  91. /// <param name="visibleObj"></param>
  92. /// <returns></returns>
  93. public static List<Solid> GetSolids(this Element elem, View view = null, bool visibleObj = false)
  94. {
  95. var geoElement = elem.GetGeometryElement(visibleObj, view);
  96. return geoElement.GetGeometryObjects<Solid>();
  97. }
  98. /// <summary>
  99. /// 获取几何Solid
  100. /// </summary>
  101. /// <param name="elem"></param>
  102. /// <param name="view"></param>
  103. /// <param name="visibleObj"></param>
  104. /// <returns></returns>
  105. public static List<T> GetGeometryObjects<T>(this Element elem, View view = null, bool visibleObj = false) where T : GeometryObject
  106. {
  107. var geoElement = elem.GetGeometryElement(visibleObj, view);
  108. return geoElement.GetGeometryObjects<T>();
  109. }
  110. }
  111. }