RevitUtil.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:RevitUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/24 9:55:09
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using Autodesk.Revit.DB;
  8. using SAGA.RevitUtils.Extends;
  9. using SAGA.RevitUtils.Extends.Graphic;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Autodesk.Revit.DB.Mechanical;
  16. using RevitToJBim.Extension;
  17. using RevitToJBim.MBI;
  18. using SAGA.DotNetUtils;
  19. using Parameter=JBIM.Component.Parameter;
  20. using SAGA.RevitUtils;
  21. using SAGA.DotNetUtils.Extend;
  22. namespace RevitToJBim.Common
  23. {
  24. public class RevitUtil
  25. {
  26. /// <summary>
  27. /// 获取FamilyInstance顶面轮廓
  28. /// </summary>
  29. /// <param name="fi"></param>
  30. /// <returns></returns>
  31. public static List<XYZ> GetTopPolygon(FamilyInstance fi)
  32. {
  33. List<XYZ> path = new List<XYZ>();
  34. var faces = fi.GetOriginalFaces(XYZ.BasisZ);
  35. var topface = faces.FirstOrDefault();
  36. //传递的path中没有重复点
  37. if (topface != null)
  38. {
  39. var curves = topface.GetCurves();
  40. for (int i = 0; i < curves.Count; i++)
  41. {
  42. var current = curves[i];
  43. var points = current.GetPoints();
  44. points.RemoveAt(points.Count - 1);
  45. path.AddRange(points);
  46. }
  47. }
  48. return path;
  49. }
  50. /// <summary>
  51. /// 获取FamilyInstance box底面轮廓
  52. /// </summary>
  53. /// <param name="fi"></param>
  54. /// <returns></returns>
  55. public static List<XYZ> GetBottomPolygon(FamilyInstance fi)
  56. {
  57. List<XYZ> path=new List<XYZ>();
  58. var box = fi.get_BoundingBox(null);
  59. if (box == null) return path;
  60. var boxMin = box.Min;
  61. var boxMax = box.Max;
  62. path.Add(boxMin);
  63. path.Add(boxMin.NewX(boxMax.X));
  64. path.Add(boxMin.NewX(boxMax.X).NewY(boxMax.Y));
  65. path.Add(boxMin.NewY(boxMax.Y));
  66. path.Add(boxMin);
  67. return path;
  68. }
  69. /// <summary>
  70. /// 获取设备设施的参数
  71. /// </summary>
  72. /// <param name="fi"></param>
  73. /// <returns></returns>
  74. public static List<Parameter> GetFacilityParameters(FamilyInstance fi)
  75. {
  76. List<string> parameterNames=new List<string>(){ MBIBuiltInParameterName.EquipLocalName,MBIBuiltInParameterName.EquipLocalID };
  77. List<Parameter> parameters=new List<Parameter>();
  78. foreach (var parameterName in parameterNames)
  79. {
  80. var revitParameter = fi.GetParameter(parameterName);
  81. if (revitParameter != null)
  82. {
  83. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  84. parameter.Value = revitParameter.AsString();
  85. parameters.Add(parameter);
  86. }
  87. }
  88. return parameters;
  89. }
  90. /// <summary>
  91. /// 获取设备设施的参数
  92. /// </summary>
  93. /// <param name="space"></param>
  94. /// <returns></returns>
  95. public static List<Parameter> GetSpaceParameters(Space space)
  96. {
  97. List<string> parameterNames = new List<string>() { MBIBuiltInParameterName.SpaceName, MBIBuiltInParameterName.SpaceNumber };
  98. List<Parameter> parameters = new List<Parameter>();
  99. foreach (var parameterName in parameterNames)
  100. {
  101. var revitParameter = space.GetParameter(parameterName);
  102. if (revitParameter != null)
  103. {
  104. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  105. parameter.Value = revitParameter.AsString();
  106. parameters.Add(parameter);
  107. }
  108. }
  109. return parameters;
  110. }
  111. /// <summary>
  112. /// 获取部件所关联的设备
  113. /// </summary>
  114. /// <param name="element"></param>
  115. /// <returns></returns>
  116. public static Element GetEquipPartParent(Element element)
  117. {
  118. if (!element.IsEquipmentPart()) return null;
  119. string code = element.GetFamilyName()?.Substring(0, 4); ;
  120. if (code.IsNullOrEmpty()) return null;
  121. //构件所关联的设备
  122. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  123. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  124. return parentInst;
  125. }
  126. /// <summary>
  127. /// 获取门窗到墙线上的投影线
  128. /// </summary>
  129. /// <param name="fi"></param>
  130. /// <returns></returns>
  131. public static List<XYZ> GetWindowDoorLocation(FamilyInstance fi)
  132. {
  133. //取几何实体顶点信息,将点到指定方向做投影
  134. List<XYZ> xyzs = GetVertexPoints(fi);
  135. XYZ handDirection = (fi.Host as Wall)?.Orientation;
  136. if (handDirection != null)
  137. {
  138. handDirection = handDirection.CrossProduct(XYZ.BasisZ);
  139. }
  140. else if (handDirection == null)
  141. {
  142. handDirection = fi.HandOrientation;
  143. }
  144. var location = fi.GetLocationPoint();
  145. if (location == null)
  146. {
  147. location = xyzs.FirstOrDefault();
  148. }
  149. double min = 0, max = 0;
  150. XYZ minXYZ = XYZ.Zero, maxXYZ = XYZ.Zero;
  151. for (int i = 0; i < xyzs.Count; i++)
  152. {
  153. var usePoint = xyzs[i];
  154. var refDirection = usePoint - location;
  155. var offset = refDirection.DotProduct(handDirection);
  156. var projectPoint = location.OffsetPoint(handDirection, offset);
  157. #region 初始化逻辑
  158. if (i == 0)
  159. {
  160. minXYZ = maxXYZ = projectPoint;
  161. min = max = offset;
  162. }
  163. #endregion
  164. #region 判定逻辑
  165. else
  166. {
  167. if (offset < min)
  168. {
  169. minXYZ = projectPoint;
  170. min = offset;
  171. continue;
  172. }
  173. if (max < offset)
  174. {
  175. maxXYZ = projectPoint;
  176. max = offset;
  177. continue;
  178. }
  179. }
  180. #endregion
  181. }
  182. return new List<XYZ>() { minXYZ, maxXYZ };
  183. }
  184. /// <summary>
  185. /// 获取元素顶点集合
  186. /// </summary>
  187. /// <returns></returns>
  188. public static List<XYZ> GetVertexPoints(Element element)
  189. {
  190. List<XYZ> xyzs = new List<XYZ>();
  191. //var solids = Extension.GeometryElementExtension.GetSolids(element,element.Document.GetUseView());
  192. //foreach (var solid in solids)
  193. //{
  194. // foreach (Edge edge in solid.Edges)
  195. // {
  196. // xyzs.AddRange(edge.Tessellate());
  197. // }
  198. //}
  199. var curves= Extension.GeometryElementExtension.GetGeometryObjects<Curve>(element, element.Document.GetUseView());
  200. foreach (var curve in curves)
  201. {
  202. xyzs.AddRange(curve.Tessellate());
  203. }
  204. return xyzs.Distinct(new XyzEqualComparer()).ToList();
  205. }
  206. /// <summary>
  207. /// 获取墙的轮廓线
  208. /// </summary>
  209. /// <param name="wall"></param>
  210. /// <returns></returns>
  211. public static List<List<XYZ>> GetWallPolygon(Wall wall)
  212. {
  213. /* 方案1:
  214. * 1找到墙的定位线,
  215. * 2将定位线拆分成多段
  216. * 3按照宽度,将多段定位线生成闭合区域
  217. * 方案2:
  218. * 直接去墙的顶面,过滤相关face形成轮廓
  219. */
  220. List<List<XYZ>> polygons = new List<List<XYZ>>();
  221. #region 过滤墙的顶面
  222. List<PlanarFace> faces = wall.GetTopFaces();
  223. #endregion
  224. foreach (PlanarFace face in faces)
  225. {
  226. foreach (CurveLoop curveLoop in face.GetEdgesAsCurveLoops())
  227. {
  228. if (curveLoop == null)
  229. {
  230. continue;
  231. }
  232. var polygon = curveLoop.GetPolygon();
  233. if (polygon.Any())
  234. {
  235. polygons.Add(polygon);
  236. }
  237. }
  238. }
  239. return polygons;
  240. }
  241. }
  242. }