RevitUtil.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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="fi"></param>
  93. /// <returns></returns>
  94. public static List<Parameter> GetSpaceParameters(Space space)
  95. {
  96. List<string> parameterNames = new List<string>() { MBIBuiltInParameterName.SpaceName, MBIBuiltInParameterName.SpaceNumber };
  97. List<Parameter> parameters = new List<Parameter>();
  98. foreach (var parameterName in parameterNames)
  99. {
  100. var revitParameter = space.GetParameter(parameterName);
  101. if (revitParameter != null)
  102. {
  103. var parameter = new Parameter(ParameterUtil.FindParameterDefine(parameterName));
  104. parameter.Value = revitParameter.AsString();
  105. parameters.Add(parameter);
  106. }
  107. }
  108. return parameters;
  109. }
  110. /// <summary>
  111. /// 获取部件所关联的设备
  112. /// </summary>
  113. /// <param name="element"></param>
  114. /// <returns></returns>
  115. public static Element GetEquipPartParent(Element element)
  116. {
  117. if (!element.IsEquipmentPart()) return null;
  118. string code = element.GetFamilyName()?.Substring(0, 4); ;
  119. if (code.IsNullOrEmpty()) return null;
  120. //构件所关联的设备
  121. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  122. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  123. return parentInst;
  124. }
  125. /// <summary>
  126. /// 获取门窗到墙线上的投影线
  127. /// </summary>
  128. /// <param name="fi"></param>
  129. /// <returns></returns>
  130. public static List<XYZ> GetWindowDoorLocation(FamilyInstance fi)
  131. {
  132. //取几何实体顶点信息,将点到指定方向做投影
  133. List<XYZ> xyzs = GetVertexPoints(fi);
  134. XYZ handDirection = (fi.Host as Wall)?.Orientation;
  135. if (handDirection != null)
  136. {
  137. handDirection = handDirection.CrossProduct(XYZ.BasisZ);
  138. }
  139. else if (handDirection == null)
  140. {
  141. handDirection = fi.HandOrientation;
  142. }
  143. var location = fi.GetLocationPoint();
  144. if (location == null)
  145. {
  146. location = xyzs.FirstOrDefault();
  147. }
  148. double min = 0, max = 0;
  149. XYZ minXYZ = XYZ.Zero, maxXYZ = XYZ.Zero;
  150. for (int i = 0; i < xyzs.Count; i++)
  151. {
  152. var usePoint = xyzs[i];
  153. var refDirection = usePoint - location;
  154. var offset = refDirection.DotProduct(handDirection);
  155. #region 初始化逻辑
  156. if (i == 0)
  157. {
  158. minXYZ = maxXYZ = usePoint;
  159. min = max = offset;
  160. }
  161. #endregion
  162. #region 判定逻辑
  163. else
  164. {
  165. if (offset < min)
  166. {
  167. minXYZ = usePoint;
  168. min = offset;
  169. continue;
  170. }
  171. if (max < offset)
  172. {
  173. maxXYZ = usePoint;
  174. max = offset;
  175. continue;
  176. }
  177. }
  178. #endregion
  179. }
  180. return new List<XYZ>() { minXYZ, maxXYZ };
  181. }
  182. /// <summary>
  183. /// 获取元素顶点集合
  184. /// </summary>
  185. /// <returns></returns>
  186. public static List<XYZ> GetVertexPoints(Element element)
  187. {
  188. List<XYZ> xyzs = new List<XYZ>();
  189. var solids = element.GetSolids();
  190. foreach (var solid in solids)
  191. {
  192. foreach (Edge edge in solid.Edges)
  193. {
  194. xyzs.AddRange(edge.Tessellate());
  195. }
  196. }
  197. return xyzs.Distinct(new XyzEqualComparer()).ToList();
  198. }
  199. /// <summary>
  200. /// 获取墙的轮廓线
  201. /// </summary>
  202. /// <param name="wall"></param>
  203. /// <returns></returns>
  204. public static List<List<XYZ>> GetWallPolygon(Wall wall)
  205. {
  206. /*
  207. * 1找到墙的定位线,
  208. * 2将定位线拆分成多段
  209. * 3按照宽度,将多段定位线生成闭合区域
  210. */
  211. var curve = wall.GetLocationCurve();
  212. List<List<XYZ>> polygons = new List<List<XYZ>>();
  213. var refs = HostObjectUtils.GetSideFaces(wall, ShellLayerType.Exterior).OfType<Face>();
  214. foreach (Face face in refs)
  215. {
  216. List<XYZ> polygon = new List<XYZ>();
  217. //foreach (Curve curve in face.ed)
  218. //{
  219. //}
  220. }
  221. return polygons;
  222. }
  223. }
  224. }