RevitUtil.cs 7.9 KB

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