ElementExtend.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* ==============================================================================
  2. * 功能描述:ElementExtend
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/10/18 9:19:31
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Autodesk.Revit.DB.Mechanical;
  14. using SAGA.DotNetUtils.Extend;
  15. using SAGA.RevitUtils.Extends;
  16. using SAGA.DotNetUtils;
  17. namespace Saga.PlugIn.ModelCheck
  18. {
  19. /// <summary>
  20. /// ElementExtend
  21. /// </summary>
  22. public static class ElementExtend
  23. {
  24. /// <summary>
  25. /// 判断是否为设备 设备族为4位
  26. /// ATVR - 多联机 - 室内机 - 双向气流 - 天花板嵌入式
  27. /// </summary>
  28. /// <param name="fi"></param>
  29. /// <returns></returns>
  30. public static bool IsEquipment(this Element element)
  31. {
  32. bool result = false;
  33. //if (element is Wall wall)
  34. //{//添加幕墙识别
  35. // result = wall.WallType.Kind == WallKind.Curtain;
  36. //}
  37. //else
  38. if (element is FamilyInstance fi)
  39. {
  40. var family = fi.GetFamilyName();
  41. result = Regex.IsMatch(family, $"{ModelCheckConst.IsEquip}");
  42. }
  43. return result;
  44. }
  45. /// <summary>
  46. /// 判断是否为设备部件 设备族为6位
  47. /// </summary>
  48. /// <param name="fi"></param>
  49. /// <returns></returns>
  50. public static bool IsEquipmentPart(this Element element)
  51. {
  52. bool result = false;
  53. if (element is FamilyInstance fi)
  54. {
  55. var family = fi.GetFamilyName();
  56. result = Regex.IsMatch(family, $"{ModelCheckConst.IsEquipPart}");
  57. }
  58. return result;
  59. }
  60. /// <summary>
  61. /// 设备mbi设备名称解析
  62. /// </summary>
  63. /// <param name="family"></param>
  64. /// <returns></returns>
  65. public static bool IsMbiEquipment(this Element element)
  66. {
  67. return element.IsEquipment() || element.IsEquipmentPart();
  68. }
  69. /// <summary>
  70. /// 判断是否为信标
  71. /// </summary>
  72. /// <param name="elem"></param>
  73. /// <returns></returns>
  74. public static bool IsBeacon(this Element elem)
  75. {
  76. var family = elem.GetFamilyName();
  77. return family != null && (Regex.IsMatch(family, ModelCheckConst.IsBeacon));
  78. }
  79. /// <summary>
  80. /// 判断是否为空间,判断周长是否为零
  81. /// 如果周长为零,是删除的空间
  82. /// </summary>
  83. /// <param name="elem"></param>
  84. /// <param name="ischeckzero">是否检查周长为零</param>
  85. /// <returns></returns>
  86. public static bool IsSpace(this Element elem, bool ischeckzero = true)
  87. {
  88. var isspace = false;
  89. if (elem is Space space)
  90. {
  91. //空间比较特殊,周长为零就相当于删除
  92. isspace = !ischeckzero || !(space.IsDeleteSpace());
  93. //限制所用空间的阶段
  94. if (isspace)
  95. {
  96. //增加过滤效率,如果上一步正确菜继续往下执行。
  97. isspace = isspace && space.IsPhase1Space();
  98. }
  99. if (isspace)
  100. {
  101. // isspace = isspace && space.IsViewLevel();
  102. }
  103. }
  104. return isspace;
  105. }
  106. /// <summary>
  107. /// 手动删除的空间,仅在明细表中可见
  108. /// </summary>
  109. /// <param name="space"></param>
  110. /// <returns></returns>
  111. public static bool IsDeleteSpace(this Space space)
  112. {
  113. //周长
  114. double perimeter = space.Perimeter;
  115. //面积
  116. double area = space.Area;
  117. //空间比较特殊,周长为零就相当于删除
  118. return (perimeter.IsZero(ModelCheckConst.SpacePerimeterTolerance)) || (area.IsZero(ModelCheckConst.SpaceAreaTolerance));
  119. }
  120. /// <summary>
  121. /// 获取MBI的定位点
  122. /// </summary>
  123. /// <param name="element"></param>
  124. /// <returns></returns>
  125. public static XYZ GetLocationPointMBIXYZ(this Element element)
  126. {
  127. ////定位点不可靠,未来可能会更改为Box的中心点
  128. //XYZ bimXyz = element.GetLocationPoint();
  129. //if (element is FamilyInstance fi)
  130. //{
  131. // var family = fi.GetFamily();
  132. // if (family.IsInPlace)
  133. // {
  134. // bimXyz = fi.GetBoxCenter();
  135. // }
  136. //}
  137. //定位点改为Box中心点
  138. XYZ bimXyz = element.GetBoxCenter();
  139. return bimXyz;
  140. }
  141. /// <summary>
  142. /// 获取MBI存储的位置信息
  143. /// </summary>
  144. /// <returns></returns>
  145. public static string GetLocationPointMBI(this Element element)
  146. {
  147. string str = ",,";
  148. XYZ bimXyz = element.GetLocationPointMBIXYZ();
  149. if (bimXyz != null)
  150. {
  151. str = bimXyz.FromApi().ToString(null);
  152. };
  153. //JObject jObject = new JObject();
  154. //jObject.Add("X", bimXyz.X);
  155. //jObject.Add("Y", bimXyz.Y);
  156. //jObject.Add("Z", bimXyz.Z);
  157. //return (new JArray(jObject)).ToString();
  158. return str;
  159. }
  160. /// <summary>
  161. /// 获取MBI存储的位置信息
  162. /// </summary>
  163. /// <returns></returns>
  164. public static XYZ ToXyz(this string xyzstr)
  165. {
  166. XYZ xyz = null;
  167. var strs = xyzstr.Split(',');
  168. if (strs.Length == 3)
  169. {
  170. xyz = new XYZ(strs[0].ToDouble(), strs[1].ToDouble(), strs[2].ToDouble());
  171. }
  172. //JObject jObject = new JObject();
  173. //jObject.Add("X", bimXyz.X);
  174. //jObject.Add("Y", bimXyz.Y);
  175. //jObject.Add("Z", bimXyz.Z);
  176. //return (new JArray(jObject)).ToString();
  177. return xyz;
  178. }
  179. /// <summary>
  180. /// 获取设备的种族类型编码 ATFC
  181. /// 族名称的命名规则:ATFC-风机盘管
  182. /// </summary>
  183. /// <returns></returns>
  184. public static string GetFamilyCode(this Element element)
  185. {
  186. string code = "";
  187. if (element is FamilyInstance fi)
  188. {
  189. string familyName = fi.GetFamilyName();
  190. if (familyName == null) return code;
  191. //族名称的命名规则:ATFC-风机盘管
  192. int index = familyName.IndexOf('-');
  193. if (index != -1 && index + 1 != familyName.Length)
  194. code = familyName.Substring(0, familyName.IndexOf('-'));
  195. //移除前面和后面的空格
  196. code = code.Trim();
  197. }
  198. return code;
  199. }
  200. public static string GetFamilyName(this Element element)
  201. {
  202. return element.GetFamily()?.Name;
  203. }
  204. /// <summary>
  205. /// 获取关联的空间
  206. /// </summary>
  207. /// <param name="fi"></param>
  208. /// <returns></returns>
  209. public static Space GetReferenceSpace(this Element element, List<Space> spaces = null)
  210. {
  211. Space space = null;
  212. if (element is FamilyInstance fi)
  213. {
  214. space = fi.Space;
  215. if (space != null) return space;
  216. if (spaces == null)
  217. spaces = fi.Document.GetSpaces().Where(t => t.IsValidObject).ToList();
  218. var origin1 = fi.GetLocationPointMBIXYZ();
  219. foreach (Space tempSpace in spaces)
  220. {
  221. //没有Space属性,取定位点,判断定位点所在空间
  222. if (tempSpace.IsPointInSpace(origin1))
  223. {
  224. space = tempSpace;
  225. break;
  226. }
  227. }
  228. }
  229. else if (element is Wall wall)
  230. {
  231. if (wall.IsCurtaiWall())
  232. space = null;
  233. }
  234. return space;
  235. }
  236. /// <summary>
  237. /// 获取部件所关联的设备
  238. /// </summary>
  239. /// <param name="element"></param>
  240. /// <returns></returns>
  241. public static Element GetEquipPartParent(this Element element)
  242. {
  243. string code = element?.GetFamily().Name.Substring(0, 4); ;
  244. if (code.IsNullOrEmpty()) return null;
  245. //构件所关联的设备
  246. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  247. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  248. return parentInst;
  249. }
  250. }
  251. }