SpaceExtension.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* ==============================================================================
  2. * 功能描述:SpaceExtension
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/26 17:40:25
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Autodesk.Revit.DB;
  12. using Autodesk.Revit.DB.Mechanical;
  13. using JBIM.Definition;
  14. using RevitToJBim.Common;
  15. using SAGA.RevitUtils;
  16. using SAGA.RevitUtils.Extends;
  17. using XYZ = Autodesk.Revit.DB.XYZ;
  18. namespace RevitToJBim.Extension
  19. {
  20. /// <summary>
  21. /// SpaceExtension
  22. /// </summary>
  23. public static class SpaceExtension
  24. {
  25. public readonly static string UseablePhaseName = "阶段1";
  26. /// <summary>
  27. /// 获取系统使用的阶段Id
  28. /// </summary>
  29. /// <param name="doc"></param>
  30. /// <returns></returns>
  31. public static ElementId GetUsePhaseId(this Document doc)
  32. {
  33. var phase = GetUsePhase(doc);
  34. if (phase == null)
  35. {
  36. return ElementId.InvalidElementId;
  37. }
  38. return phase.Id;
  39. }
  40. /// <summary>
  41. /// 获取系统使用的阶段
  42. /// </summary>
  43. /// <param name="doc"></param>
  44. /// <returns></returns>
  45. public static Phase GetUsePhase(this Document doc)
  46. {
  47. var elements = doc.GetElements<Phase>(BuiltInCategory.OST_Phases);
  48. foreach (var element in elements)
  49. {
  50. var tempName = element.Name.Replace(" ", "").Trim();
  51. if (UseablePhaseName == tempName)
  52. {
  53. return element;
  54. }
  55. }
  56. return null;
  57. }
  58. public static ElementId GetCurrentPhaseId(this Space space)
  59. {
  60. return space.GetParameterElementId(BuiltInParameter.ROOM_PHASE_ID) ?? ElementId.InvalidElementId;
  61. }
  62. /// <summary>
  63. /// 判断是否是阶段1的空间
  64. /// </summary>
  65. /// <param name="space"></param>
  66. /// <returns></returns>
  67. public static bool IsPhase1Space(this Space space)
  68. {
  69. var doc = space.Document;
  70. var useId = GetUsePhaseId(doc);
  71. return space.GetCurrentPhaseId() == useId;
  72. }
  73. /// <summary>
  74. /// 空间标高是否是当前使用视图标高
  75. /// </summary>
  76. /// <param name="space"></param>
  77. /// <returns></returns>
  78. public static bool IsViewLevel(this Space space)
  79. {
  80. var doc = space.Document;
  81. var useViewId = doc.GetUseView();
  82. if (useViewId == null)
  83. {
  84. return false;
  85. }
  86. return space.Level?.Id == useViewId.GenLevel?.Id;
  87. }
  88. /// <summary>
  89. /// 获取空间的外轮廓
  90. /// </summary>
  91. /// <param name="space"></param>
  92. /// <returns></returns>
  93. public static List<Polygon> GetSpaceOutline(this Space space)
  94. {
  95. List<Polygon> outlines=new List<Polygon>();
  96. try
  97. {
  98. SpatialElementBoundaryOptions options = new SpatialElementBoundaryOptions();
  99. //options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
  100. var segments = space.GetBoundarySegments(options);
  101. double tolerance = space.Document.Application.ShortCurveTolerance;
  102. foreach (var segment in segments)
  103. {
  104. #region 取外轮廓的点
  105. List<XYZ> xyzs = new List<XYZ>();
  106. foreach (BoundarySegment boundarySegment in segment)
  107. {
  108. var segmentElement = space.Document.GetElement(boundarySegment.ElementId);
  109. if (segmentElement is Wall wall)
  110. {
  111. var wallCurve = wall.GetLocationCurve();
  112. var parallelWalls = GetParallelWalls(wall);
  113. var segmentCurve = boundarySegment.GetCurve();
  114. var tessellates = segmentCurve.Tessellate();
  115. foreach (XYZ tessellate in tessellates)
  116. {
  117. var project = wallCurve.GetProjectPt(tessellate);
  118. var verticalVector = (project - tessellate).Normalize();
  119. var tempXyz = GetOuterXYZ(tessellate, verticalVector, segmentCurve, parallelWalls);
  120. if (tempXyz != null)
  121. xyzs.Add(tempXyz);
  122. }
  123. }
  124. else
  125. {
  126. foreach (XYZ tessellate in boundarySegment.GetCurve().Tessellate())
  127. {
  128. xyzs.Add(tessellate);
  129. }
  130. }
  131. }
  132. #endregion
  133. #region 处理内拐角
  134. int count = xyzs.Count;
  135. /*
  136. * 相邻两条线求交点
  137. * 处理内拐角
  138. */
  139. for (int i = 0; i < count; i++)
  140. {
  141. if (count < 4) continue;
  142. var p1 = xyzs[i];
  143. int j2 = i + 1 >= xyzs.Count ? i + 1 - count : i + 1;
  144. var p2 = xyzs[j2];
  145. int j3 = i + 2 >= xyzs.Count ? i + 2 - count : i + 2;
  146. var p3 = xyzs[j3];
  147. int j4 = i + 3 >= xyzs.Count ? i + 3 - count : i + 3;
  148. var p4 = xyzs[j4];
  149. if (p2.IsEqual(p3) || p1.DistanceTo(p2) < tolerance || p3.DistanceTo(p4) < tolerance)
  150. continue;
  151. Curve curve1 = Line.CreateBound(p1, p2);
  152. Curve curve2 = Line.CreateBound(p3, p4);
  153. XYZ intersection = curve1.GetIntersection(curve2);
  154. if (intersection == null) continue;
  155. xyzs[j2] = intersection;
  156. xyzs[j3] = intersection;
  157. }
  158. #endregion
  159. //添加起始点,形成闭合区域
  160. if (xyzs.Any())
  161. xyzs.Add(xyzs[0]);
  162. //整理数据
  163. Polygon outLine = new Polygon();
  164. var usePoints = xyzs.Select(xyz => BimConvert.ConvertToXYZ(xyz)).ToList();
  165. outLine.AddRange(usePoints.GetRange(0, usePoints.Count - 1));
  166. StandardUtil.ArrangeLoop(outLine);
  167. outlines.Add(outLine);
  168. }
  169. }
  170. catch (Exception e)
  171. {
  172. Console.WriteLine(e);
  173. throw;
  174. }
  175. return outlines;
  176. }
  177. private static double m_RedundancySpace = 10d;
  178. /// <summary>
  179. /// 获取平行墙,平行且投影有重叠部分
  180. /// </summary>
  181. /// <param name="wall"></param>
  182. /// <returns></returns>
  183. public static List<Wall> GetParallelWalls(Wall wall)
  184. {
  185. List<Wall> walls = new List<Wall>();
  186. walls.Add(wall);
  187. var doc = wall.Document;
  188. var allWalls = doc.GetElements<Wall>();
  189. var curve = wall.GetLocationCurve();
  190. var parallelWalls = allWalls.Where(t => t.GetLocationCurve().IsParallel(curve));
  191. int len = walls.Count;
  192. do
  193. {
  194. len = walls.Count;
  195. foreach (Wall parallelWall in parallelWalls)
  196. {
  197. var curve1 = parallelWall.GetLocationCurve();
  198. var startP11 = CurveExtend.StartPoint(curve1);
  199. var endP11 = CurveExtend.EndPoint(curve1);
  200. double width1 = parallelWall.Width.FromApi();
  201. for (int i = 0; i < walls.Count; i++)
  202. {
  203. Wall referenceWall = walls[i];
  204. if (walls.Contains(parallelWall)) continue;
  205. var curve2 = referenceWall.GetLocationCurve();
  206. var startP12 = CurveExtend.GetProjectPt(curve2, startP11);
  207. var endP12 = CurveExtend.GetProjectPt(curve2, endP11);
  208. var startP21 = CurveExtend.StartPoint(curve2);
  209. var endP21 = CurveExtend.EndPoint(curve2);
  210. var startP22 = CurveExtend.GetProjectPt(curve1, startP21);
  211. var endP22 = CurveExtend.GetProjectPt(curve1, endP21);
  212. //判断是否有重叠部分
  213. if (startP12.IsOnCurve(curve2) || endP12.IsOnCurve(curve2) || startP22.IsOnCurve(curve1) ||
  214. endP22.IsOnCurve(curve1))
  215. {
  216. double width2 = referenceWall.Width.FromApi();
  217. double distacne = curve1.Distance(curve2).FromApi();
  218. if (distacne < (width1 + width2) / 2.0 + m_RedundancySpace)
  219. walls.Add(parallelWall);
  220. }
  221. }
  222. }
  223. } while (len != walls.Count);
  224. return walls;
  225. }
  226. /// <summary>
  227. /// 获取外廓后的点
  228. /// </summary>
  229. /// <param name="xyz">外廓点</param>
  230. /// <param name="verticalVector">外廓向量</param>
  231. /// <param name="segmentCurve">参考Curve</param>
  232. /// <param name="walls">平行墙集合</param>
  233. /// <returns></returns>
  234. public static XYZ GetOuterXYZ(XYZ xyz, XYZ verticalVector, Curve segmentCurve, List<Wall> walls)
  235. {
  236. XYZ outerXyz = xyz;
  237. double distance = 0;
  238. foreach (Wall wall in walls)
  239. {
  240. /*
  241. * 注意幕墙,没有底面,直接取原始点xyz
  242. */
  243. List<PlanarFace> faces = wall.GetBottomFaces();
  244. foreach (PlanarFace face in faces)
  245. {
  246. foreach (CurveLoop curveLoop in face.GetEdgesAsCurveLoops())
  247. {
  248. foreach (Curve curve in curveLoop)
  249. {
  250. /*
  251. * 1. 获取平行边
  252. * 2. 平行边求投影,投影点形成向量为向外
  253. * 3. 投影点在线上
  254. */
  255. if (!segmentCurve.IsParallel(curve))
  256. continue;
  257. var projectPt = curve.GetProjectPt(xyz);
  258. var tempVector = (projectPt - xyz).Normalize();
  259. if (projectPt.IsOnCurve(curve) && !projectPt.IsEqual(xyz) && tempVector.IsEqual(verticalVector))
  260. {
  261. var tempDistance = xyz.DistanceTo(projectPt);
  262. if (tempDistance >= distance)
  263. {
  264. distance = tempDistance;
  265. outerXyz = projectPt;
  266. }
  267. }
  268. else
  269. {
  270. /*
  271. * 无投影点,给个默认投影点的情况
  272. * 处理斜边投影不到的情况边上的情况,
  273. * 目前任意取了一个,多个取最近的??
  274. */
  275. if (outerXyz == null || outerXyz.IsEqual(xyz))
  276. outerXyz = projectPt;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. return outerXyz;
  283. }
  284. }
  285. }