ElementRangeCheck.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* ==============================================================================
  2. * 功能描述:SagaCheck
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/11 16:09:09
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using Autodesk.Revit.DB;
  12. using Autodesk.Revit.DB.Mechanical;
  13. using SAGA.DotNetUtils.Extend;
  14. using ServiceRevitLib.Common;
  15. using SAGA.RevitUtils.Extends;
  16. using ServiceRevitLib.Extend;
  17. namespace ServiceRevitLib.Mode
  18. {
  19. /// <summary>
  20. /// SagaCheck
  21. /// </summary>
  22. class ElementRangeCheck : CheckBase
  23. {
  24. #region 序列化属性
  25. private double m_Redundant = 500;
  26. /// <summary>
  27. /// 高度冗余
  28. /// </summary>
  29. public double Redundant
  30. {
  31. get { return m_Redundant; }
  32. set { m_Redundant = value; }
  33. }
  34. private double m_baseLevel;
  35. /// <summary>
  36. /// 底部标高
  37. /// </summary>
  38. public double BaseLevel
  39. {
  40. get { return m_baseLevel; }
  41. }
  42. private List<double> m_topLevels = new List<double>();
  43. /// <summary>
  44. /// 顶部标高(夹层可能涉及多个顶)
  45. /// </summary>
  46. public string TopLevels
  47. {
  48. get { return string.Join(";", m_topLevels); }
  49. }
  50. #endregion
  51. public override void Check()
  52. {
  53. base.Check();
  54. #region
  55. var document = m_Doc;
  56. if (!SetFloorBaseTopRange()) return;
  57. var elements = document.GetAllElements();
  58. foreach (var element in elements)
  59. {
  60. var result = GetCheckResult(element);
  61. if (result != null)
  62. Content.Add(result);
  63. }
  64. #endregion
  65. }
  66. /// <summary>
  67. /// 设置当前楼层底部和顶部范围
  68. /// </summary>
  69. /// <param name="doc"></param>
  70. /// <param name="result"></param>
  71. private bool SetFloorBaseTopRange()
  72. {
  73. bool rt = true;
  74. var document = m_Doc;
  75. try
  76. {
  77. var sagaPlans = document.GetElements<ViewPlan>()
  78. .Where(t => t.ViewType == ViewType.FloorPlan && t.Name.Contains("-saga")).ToList();
  79. var curFloor = sagaPlans.FirstOrDefault();
  80. if (sagaPlans.Count != 1 || curFloor == null)
  81. {
  82. Result = ResultState.Failure;
  83. ResultMsg = "Saga标记不合法,请修正后再进行检查";
  84. return false;
  85. }
  86. //设置楼层底部高度
  87. m_baseLevel = curFloor.GenLevel.Elevation.FromApi().Round(2);
  88. //设置楼层顶部高度
  89. var levels = m_Doc.GetLevels();
  90. var mbiLevels = levels.Where(t => Regex.IsMatch(t.Name, $"{RegexConstPattern.IsMBILevel}") && t.Elevation.IsThan(m_baseLevel)).ToList();
  91. foreach (Level level in mbiLevels)
  92. {
  93. m_topLevels.Add(level.Elevation.FromApi().Round(2));
  94. if (!Regex.IsMatch(level.Name, $"{RegexConstPattern.IsSandwich}"))
  95. {
  96. break;
  97. }
  98. }
  99. //当前层为屋顶或屋顶夹层时,顶部限制设置为正无穷
  100. if (Regex.IsMatch(curFloor.GenLevel.Name, $"{RegexConstPattern.IsRF}|{RegexConstPattern.IsRFM}"))
  101. {
  102. m_topLevels.Add(double.MaxValue);
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. Console.WriteLine(e);
  108. rt = false;
  109. }
  110. return rt;
  111. }
  112. /// <summary>
  113. /// 获取检测结果
  114. /// </summary>
  115. /// <param name="element"></param>
  116. /// <returns></returns>
  117. private ElementRangeCheckResult GetCheckResult(Element element)
  118. {
  119. var result = new ElementRangeCheckResult();
  120. result.Id = element.Id.ToString();
  121. result.FamilyName = element.GetFamilyName();
  122. try
  123. {
  124. double zb = 0, zt = 0;
  125. bool rb = false, rt = false;
  126. //需要识别判定的构件
  127. DCElementType type = DCElementType.None;
  128. bool isBoxInst = false;
  129. if (element is Wall wall)
  130. {
  131. isBoxInst = true;
  132. zb = wall.GetBaseStaticHeight();
  133. zt = wall.GetTopStaticHeight();
  134. type = DCElementType.Wall;
  135. }
  136. else if (element.IsSpace())
  137. {
  138. var space = element as Space;
  139. isBoxInst = true;
  140. zb = space.GetBaseStaticHeight();
  141. zt = space.GetTopStaticHeight();
  142. type = DCElementType.Space;
  143. }
  144. else if (element is FamilyInstance)
  145. {
  146. if (element.IsAllColumn())
  147. {
  148. var fi = element as FamilyInstance;
  149. isBoxInst = true;
  150. zb = fi.GetBaseStaticHeight();
  151. zt = fi.GetTopStaticHeight();
  152. type = DCElementType.Column;
  153. }
  154. else if (element.IsEquipment() || element.IsEquipmentPart() || element.IsBeacon())
  155. {
  156. zb = element.GetLocationPointMBIXYZ().Z;
  157. type = GetRType(element);
  158. }
  159. }
  160. if (type==DCElementType.None) return null;
  161. //冗余,使用时,统一使用单位mm
  162. double w = Redundant;
  163. zb = zb.FromApi().Round(2);
  164. zt = zt.FromApi().Round(2);
  165. //构件类型
  166. result.Type = type.GetDescription();
  167. if (isBoxInst)
  168. {
  169. rb = zb.IsBetween(m_baseLevel - w, m_baseLevel);
  170. rt = m_topLevels.Any(t => zt.IsBetween(t - w, t));
  171. string ttip = rb ? "" : "底部";
  172. string ttop = rt ? "" : rb ? "和顶部" : "顶部";
  173. result.HeightRange = $"{zb},{zt}";
  174. result.Result = (rb && rt)?ResultState.Success:ResultState.Failure;
  175. result.ResultMsg = rb && rt ? "" : $"构件范围不满足要求;请检查构件{ttip}{ttop}";
  176. }
  177. else
  178. {
  179. rb = m_topLevels.Any(t => zb.IsBetween(m_baseLevel, t));
  180. result.HeightRange = $"{zb}";
  181. result.Result = (rb) ? ResultState.Success : ResultState.Failure;
  182. result.ResultMsg = rb? "" : $"构件范围不满足要求;请检查构件位置";
  183. }
  184. }
  185. catch (Exception e)
  186. {
  187. result.Result = ResultState.Failure;
  188. result.ResultMsg = "构件范围检查异常";
  189. }
  190. return result;
  191. }
  192. /// <summary>
  193. /// 获取构件类型
  194. /// </summary>
  195. /// <param name="fi"></param>
  196. /// <returns></returns>
  197. private DCElementType GetRType(Element fi)
  198. {
  199. DCElementType name = DCElementType.None;
  200. if (fi.IsEquipment())
  201. {
  202. name = DCElementType.Equipment;
  203. }
  204. else if (fi.IsEquipmentPart())
  205. {
  206. name = DCElementType.EuipmentPart;
  207. }
  208. else if (fi.IsBeacon())
  209. {
  210. name = DCElementType.Beacon;
  211. }
  212. return name;
  213. }
  214. /// <summary>
  215. /// DCElementType
  216. /// </summary>
  217. public enum DCElementType
  218. {
  219. [Description("未知")]
  220. None,
  221. [Description("墙")]
  222. Wall,
  223. [Description("柱")]
  224. Column,
  225. [Description("空间")]
  226. Space,
  227. [Description("设备")]
  228. Equipment,
  229. [Description("部件")]
  230. EuipmentPart,
  231. [Description("信标")]
  232. Beacon
  233. }
  234. }
  235. }