RevitUtil.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. try
  59. {
  60. var box = fi.get_BoundingBox(null);
  61. if (box == null) return path;
  62. var boxMin = box.Min;
  63. var boxMax = box.Max;
  64. path.Add(boxMin);
  65. path.Add(boxMin.NewX(boxMax.X));
  66. path.Add(boxMin.NewX(boxMax.X).NewY(boxMax.Y));
  67. path.Add(boxMin.NewY(boxMax.Y));
  68. path.Add(boxMin);
  69. }
  70. catch (Exception e)
  71. {
  72. Console.WriteLine(e);
  73. }
  74. return path;
  75. }
  76. /// <summary>
  77. /// 获取设备设施的参数
  78. /// </summary>
  79. /// <param name="fi"></param>
  80. /// <returns></returns>
  81. public static List<Parameter> GetFacilityParameters(FamilyInstance fi)
  82. {
  83. List<string> parameterNames=new List<string>(){ MBIBuiltInParameterName.EquipLocalName,MBIBuiltInParameterName.EquipLocalID,MBIBuiltInParameterName.PropertyID };
  84. List<Parameter> parameters=new List<Parameter>();
  85. foreach (var parameterName in parameterNames)
  86. {
  87. var revitParameter = fi.GetParameter(parameterName);
  88. if (revitParameter != null)
  89. {
  90. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  91. parameter.Value = revitParameter.AsString();
  92. parameters.Add(parameter);
  93. }
  94. }
  95. var typeParameter = fi?.GetElementType()?.Parameters;
  96. foreach (Autodesk.Revit.DB.Parameter p in typeParameter)
  97. {
  98. if (MBIBuiltInParameterName.EquipComponentCode.Equals(p.Definition.Name))
  99. {
  100. if(!p.HasValue || (p.AsString() == null && p.AsString().Length == 0))
  101. continue;
  102. var parameter =
  103. new Parameter(ParameterUtil.FindParameterDefine(MBIBuiltInParameterName.EquipComponentCode));
  104. parameter.Value = p.AsString();
  105. parameters.Add(parameter);
  106. }
  107. }
  108. return parameters;
  109. }
  110. public static ElementType GetElementType(Element element)
  111. {
  112. ElementId id = element.GetTypeId();
  113. if (id != null && id != ElementId.InvalidElementId)
  114. {
  115. Element elementType = element.Document.GetElement(id);
  116. return elementType as ElementType;
  117. }
  118. return null;
  119. }
  120. /// <summary>
  121. /// 获取设备设施的参数
  122. /// </summary>
  123. /// <param name="space"></param>
  124. /// <returns></returns>
  125. public static List<Parameter> GetSpaceParameters(Space space)
  126. {
  127. List<string> parameterNames = new List<string>() { MBIBuiltInParameterName.SpaceName, MBIBuiltInParameterName.SpaceNumber, MBIBuiltInParameterName.SpaceStandardTypeCode, MBIBuiltInParameterName.SpaceStandardType };
  128. List<Parameter> parameters = new List<Parameter>();
  129. foreach (var parameterName in parameterNames)
  130. {
  131. var revitParameter = space.GetParameter(parameterName);
  132. if (revitParameter != null)
  133. {
  134. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  135. parameter.Value = revitParameter.AsString();
  136. parameters.Add(parameter);
  137. }
  138. }
  139. return parameters;
  140. }
  141. /// <summary>
  142. /// 获取设备设施的参数
  143. /// </summary>
  144. /// <param name="space"></param>
  145. /// <returns></returns>
  146. public static List<Parameter> GetMEPCurveParameters(MEPCurve curve)
  147. {
  148. List<string> parameterNames = new List<string>() { MBIBuiltInParameterName.MEPCurveComments};
  149. List<Parameter> parameters = new List<Parameter>();
  150. foreach (var parameterName in parameterNames)
  151. {
  152. var revitParameter = curve.GetParameter(parameterName);
  153. if (revitParameter != null)
  154. {
  155. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  156. parameter.Value = revitParameter.AsString();
  157. parameters.Add(parameter);
  158. }
  159. }
  160. return parameters;
  161. }
  162. /// <summary>
  163. /// 获取部件所关联的设备
  164. /// </summary>
  165. /// <param name="element"></param>
  166. /// <returns></returns>
  167. public static Element GetEquipPartParent(Element element)
  168. {
  169. if (!element.IsEquipmentPart()) return null;
  170. string code = element.GetFamilyName()?.Substring(0, 4); ;
  171. if (code.IsNullOrEmpty()) return null;
  172. //构件所关联的设备
  173. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  174. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  175. return parentInst;
  176. }
  177. /// <summary>
  178. /// 获取门窗到墙线上的投影线
  179. /// </summary>
  180. /// <param name="fi"></param>
  181. /// <returns></returns>
  182. public static List<XYZ> GetWindowDoorLocation(FamilyInstance fi)
  183. {
  184. //取几何实体顶点信息,将点到指定方向做投影
  185. List<XYZ> xyzs = GetVertexPoints(fi);
  186. XYZ handDirection = (fi.Host as Wall)?.Orientation;
  187. if (handDirection != null)
  188. {
  189. handDirection = handDirection.CrossProduct(XYZ.BasisZ);
  190. }
  191. else if (handDirection == null)
  192. {
  193. handDirection = fi.HandOrientation;
  194. }
  195. var location = fi.GetLocationPoint();
  196. if (location == null)
  197. {
  198. location = xyzs.FirstOrDefault();
  199. }
  200. double min = 0, max = 0;
  201. XYZ minXYZ = XYZ.Zero, maxXYZ = XYZ.Zero;
  202. for (int i = 0; i < xyzs.Count; i++)
  203. {
  204. var usePoint = xyzs[i];
  205. var refDirection = usePoint - location;
  206. var offset = refDirection.DotProduct(handDirection);
  207. var projectPoint = location.OffsetPoint(handDirection, offset);
  208. #region 初始化逻辑
  209. if (i == 0)
  210. {
  211. minXYZ = maxXYZ = projectPoint;
  212. min = max = offset;
  213. }
  214. #endregion
  215. #region 判定逻辑
  216. else
  217. {
  218. if (offset < min)
  219. {
  220. minXYZ = projectPoint;
  221. min = offset;
  222. continue;
  223. }
  224. if (max < offset)
  225. {
  226. maxXYZ = projectPoint;
  227. max = offset;
  228. continue;
  229. }
  230. }
  231. #endregion
  232. }
  233. return new List<XYZ>() { minXYZ, maxXYZ };
  234. }
  235. /// <summary>
  236. /// 获取元素顶点集合
  237. /// </summary>
  238. /// <returns></returns>
  239. public static List<XYZ> GetVertexPoints(Element element)
  240. {
  241. List<XYZ> xyzs = new List<XYZ>();
  242. //string id = element.Id.ToString();
  243. //var solids = Extension.GeometryElementExtension.GetSolids(element, element.Document.GetUseView());
  244. //foreach (var solid in solids)
  245. //{
  246. // foreach (Edge edge in solid.Edges)
  247. // {
  248. // xyzs.AddRange(edge.Tessellate());
  249. // }
  250. //}
  251. var curves= Extension.GeometryElementExtension.GetGeometryObjects<Curve>(element, element.Document.GetUseView(), true);
  252. foreach (var curve in curves)
  253. {
  254. xyzs.AddRange(curve.Tessellate());
  255. }
  256. return xyzs.Distinct(new XyzEqualComparer()).ToList();
  257. }
  258. /// <summary>
  259. /// 获取墙的轮廓线
  260. /// </summary>
  261. /// <param name="wall"></param>
  262. /// <returns></returns>
  263. public static List<List<XYZ>> GetWallPolygon(Wall wall)
  264. {
  265. /* 方案1:
  266. * 1找到墙的定位线,
  267. * 2将定位线拆分成多段
  268. * 3按照宽度,将多段定位线生成闭合区域
  269. * 方案2:
  270. * 直接去墙的顶面,过滤相关face形成轮廓
  271. */
  272. List<List<XYZ>> polygons = new List<List<XYZ>>();
  273. #region 过滤墙的顶面
  274. List<PlanarFace> faces = wall.GetBottomFaces();
  275. #endregion
  276. foreach (PlanarFace face in faces)
  277. {
  278. foreach (CurveLoop curveLoop in face.GetEdgesAsCurveLoops())
  279. {
  280. if (curveLoop == null)
  281. {
  282. continue;
  283. }
  284. var polygon = curveLoop.GetPolygon();
  285. if (polygon.Any())
  286. {
  287. polygons.Add(polygon);
  288. }
  289. }
  290. }
  291. return polygons;
  292. }
  293. }
  294. }