RevitUtil.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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;
  9. using SAGA.RevitUtils.Extends;
  10. using SAGA.RevitUtils.Extends.Graphic;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace RevitToJBim.Common
  17. {
  18. public class RevitUtil
  19. {
  20. /// <summary>
  21. /// 获取FamilyInstance顶面轮廓
  22. /// </summary>
  23. /// <param name="fi"></param>
  24. /// <returns></returns>
  25. public static List<XYZ> GetTopPolygon(FamilyInstance fi)
  26. {
  27. List<XYZ> path = new List<XYZ>();
  28. var faces = fi.GetOriginalFaces(XYZ.BasisZ);
  29. var topface = faces.FirstOrDefault();
  30. //传递的path中没有重复点
  31. if (topface != null)
  32. {
  33. var curves = topface.GetCurves();
  34. for (int i = 0; i < curves.Count; i++)
  35. {
  36. var current = curves[i];
  37. var points = current.GetPoints();
  38. points.RemoveAt(points.Count - 1);
  39. path.AddRange(points);
  40. }
  41. }
  42. return path;
  43. }
  44. /// <summary>
  45. /// 获取门窗到墙线上的投影线
  46. /// </summary>
  47. /// <param name="fi"></param>
  48. /// <returns></returns>
  49. public static List<XYZ> GetWindowDoorLocation(FamilyInstance fi)
  50. {
  51. //取几何实体顶点信息,将点到指定方向做投影
  52. List<XYZ> xyzs = GetVertexPoints(fi);
  53. XYZ handDirection = (fi.Host as Wall)?.Orientation;
  54. if (handDirection != null)
  55. {
  56. handDirection = handDirection.CrossProduct(XYZ.BasisZ);
  57. }
  58. else if (handDirection == null)
  59. {
  60. handDirection = fi.HandOrientation;
  61. }
  62. var location = fi.GetLocationPoint();
  63. if (location == null)
  64. {
  65. location = xyzs.FirstOrDefault();
  66. }
  67. double min=0, max=0;
  68. XYZ minXYZ=XYZ.Zero, maxXYZ = XYZ.Zero;
  69. for (int i = 0; i < xyzs.Count; i++)
  70. {
  71. var usePoint = xyzs[i];
  72. var refDirection = usePoint - location;
  73. var offset= refDirection.DotProduct(handDirection);
  74. #region 初始化逻辑
  75. if (i == 0)
  76. {
  77. minXYZ = maxXYZ = usePoint;
  78. min = max = offset;
  79. }
  80. #endregion
  81. #region 判定逻辑
  82. else
  83. {
  84. if (offset < min)
  85. {
  86. minXYZ = usePoint;
  87. min = offset;
  88. continue;
  89. }
  90. if (max < offset)
  91. {
  92. maxXYZ = usePoint;
  93. max = offset;
  94. continue;
  95. }
  96. }
  97. #endregion
  98. }
  99. return new List<XYZ>(){minXYZ,maxXYZ};
  100. }
  101. /// <summary>
  102. /// 获取元素顶点集合
  103. /// </summary>
  104. /// <returns></returns>
  105. public static List<XYZ> GetVertexPoints(Element element)
  106. {
  107. List<XYZ> xyzs = new List<XYZ>();
  108. var solids=element.GetSolids();
  109. foreach (var solid in solids)
  110. {
  111. foreach (Edge edge in solid.Edges)
  112. {
  113. xyzs.AddRange(edge.Tessellate());
  114. }
  115. }
  116. return xyzs.Distinct(new XyzEqualComparer()).ToList();
  117. }
  118. /// <summary>
  119. /// 获取墙的轮廓线
  120. /// </summary>
  121. /// <param name="wall"></param>
  122. /// <returns></returns>
  123. public static List<List<XYZ>> GetWallPolygon(Wall wall)
  124. {
  125. /*
  126. * 1找到墙的定位线,
  127. * 2将定位线拆分成多段
  128. * 3按照宽度,将多段定位线生成闭合区域
  129. */
  130. var curve = wall.GetLocationCurve();
  131. List<List<XYZ>> polygons = new List<List<XYZ>>();
  132. var refs = HostObjectUtils.GetSideFaces(wall,ShellLayerType.Exterior).OfType<Face>();
  133. foreach (Face face in refs)
  134. {
  135. List<XYZ> polygon = new List<XYZ>();
  136. //foreach (Curve curve in face.ed)
  137. //{
  138. //}
  139. }
  140. //偏移位位置信息
  141. return polygons;
  142. }
  143. }
  144. }