RevitUtil.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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="space"></param>
  115. /// <returns></returns>
  116. public static List<Parameter> GetMEPCurveParameters(MEPCurve curve)
  117. {
  118. List<string> parameterNames = new List<string>() { MBIBuiltInParameterName.MEPCurveComments};
  119. List<Parameter> parameters = new List<Parameter>();
  120. foreach (var parameterName in parameterNames)
  121. {
  122. var revitParameter = curve.GetParameter(parameterName);
  123. if (revitParameter != null)
  124. {
  125. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  126. parameter.Value = revitParameter.AsString();
  127. parameters.Add(parameter);
  128. }
  129. }
  130. return parameters;
  131. }
  132. /// <summary>
  133. /// 获取部件所关联的设备
  134. /// </summary>
  135. /// <param name="element"></param>
  136. /// <returns></returns>
  137. public static Element GetEquipPartParent(Element element)
  138. {
  139. if (!element.IsEquipmentPart()) return null;
  140. string code = element.GetFamilyName()?.Substring(0, 4); ;
  141. if (code.IsNullOrEmpty()) return null;
  142. //构件所关联的设备
  143. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  144. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  145. return parentInst;
  146. }
  147. /// <summary>
  148. /// 获取门窗到墙线上的投影线
  149. /// </summary>
  150. /// <param name="fi"></param>
  151. /// <returns></returns>
  152. public static List<XYZ> GetWindowDoorLocation(FamilyInstance fi)
  153. {
  154. //取几何实体顶点信息,将点到指定方向做投影
  155. List<XYZ> xyzs = GetVertexPoints(fi);
  156. XYZ handDirection = (fi.Host as Wall)?.Orientation;
  157. if (handDirection != null)
  158. {
  159. handDirection = handDirection.CrossProduct(XYZ.BasisZ);
  160. }
  161. else if (handDirection == null)
  162. {
  163. handDirection = fi.HandOrientation;
  164. }
  165. var location = fi.GetLocationPoint();
  166. if (location == null)
  167. {
  168. location = xyzs.FirstOrDefault();
  169. }
  170. double min = 0, max = 0;
  171. XYZ minXYZ = XYZ.Zero, maxXYZ = XYZ.Zero;
  172. for (int i = 0; i < xyzs.Count; i++)
  173. {
  174. var usePoint = xyzs[i];
  175. var refDirection = usePoint - location;
  176. var offset = refDirection.DotProduct(handDirection);
  177. var projectPoint = location.OffsetPoint(handDirection, offset);
  178. #region 初始化逻辑
  179. if (i == 0)
  180. {
  181. minXYZ = maxXYZ = projectPoint;
  182. min = max = offset;
  183. }
  184. #endregion
  185. #region 判定逻辑
  186. else
  187. {
  188. if (offset < min)
  189. {
  190. minXYZ = projectPoint;
  191. min = offset;
  192. continue;
  193. }
  194. if (max < offset)
  195. {
  196. maxXYZ = projectPoint;
  197. max = offset;
  198. continue;
  199. }
  200. }
  201. #endregion
  202. }
  203. return new List<XYZ>() { minXYZ, maxXYZ };
  204. }
  205. /// <summary>
  206. /// 获取元素顶点集合
  207. /// </summary>
  208. /// <returns></returns>
  209. public static List<XYZ> GetVertexPoints(Element element)
  210. {
  211. List<XYZ> xyzs = new List<XYZ>();
  212. //var solids = Extension.GeometryElementExtension.GetSolids(element,element.Document.GetUseView());
  213. //foreach (var solid in solids)
  214. //{
  215. // foreach (Edge edge in solid.Edges)
  216. // {
  217. // xyzs.AddRange(edge.Tessellate());
  218. // }
  219. //}
  220. var curves= Extension.GeometryElementExtension.GetGeometryObjects<Curve>(element, element.Document.GetUseView());
  221. foreach (var curve in curves)
  222. {
  223. xyzs.AddRange(curve.Tessellate());
  224. }
  225. return xyzs.Distinct(new XyzEqualComparer()).ToList();
  226. }
  227. /// <summary>
  228. /// 获取墙的轮廓线
  229. /// </summary>
  230. /// <param name="wall"></param>
  231. /// <returns></returns>
  232. public static List<List<XYZ>> GetWallPolygon(Wall wall)
  233. {
  234. /* 方案1:
  235. * 1找到墙的定位线,
  236. * 2将定位线拆分成多段
  237. * 3按照宽度,将多段定位线生成闭合区域
  238. * 方案2:
  239. * 直接去墙的顶面,过滤相关face形成轮廓
  240. */
  241. List<List<XYZ>> polygons = new List<List<XYZ>>();
  242. #region 过滤墙的顶面
  243. List<PlanarFace> faces = wall.GetBottomFaces();
  244. #endregion
  245. foreach (PlanarFace face in faces)
  246. {
  247. foreach (CurveLoop curveLoop in face.GetEdgesAsCurveLoops())
  248. {
  249. if (curveLoop == null)
  250. {
  251. continue;
  252. }
  253. var polygon = curveLoop.GetPolygon();
  254. if (polygon.Any())
  255. {
  256. polygons.Add(polygon);
  257. }
  258. }
  259. }
  260. return polygons;
  261. }
  262. }
  263. }