RevitUtil.cs 7.2 KB

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