ElementExtend.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /// 获取MBI的定位点
  108. /// </summary>
  109. /// <param name="element"></param>
  110. /// <returns></returns>
  111. public static XYZ GetLocationPointMBIXYZ(this Element element)
  112. {
  113. ////定位点不可靠,未来可能会更改为Box的中心点
  114. //XYZ bimXyz = element.GetLocationPoint();
  115. //if (element is FamilyInstance fi)
  116. //{
  117. // var family = fi.GetFamily();
  118. // if (family.IsInPlace)
  119. // {
  120. // bimXyz = fi.GetBoxCenter();
  121. // }
  122. //}
  123. //定位点改为Box中心点
  124. XYZ bimXyz = element.GetBoxCenter();
  125. return bimXyz;
  126. }
  127. /// <summary>
  128. /// 获取MBI存储的位置信息
  129. /// </summary>
  130. /// <returns></returns>
  131. public static string GetLocationPointMBI(this Element element)
  132. {
  133. string str = ",,";
  134. XYZ bimXyz = element.GetLocationPointMBIXYZ();
  135. if (bimXyz != null)
  136. {
  137. str = bimXyz.FromApi().ToString(null);
  138. };
  139. //JObject jObject = new JObject();
  140. //jObject.Add("X", bimXyz.X);
  141. //jObject.Add("Y", bimXyz.Y);
  142. //jObject.Add("Z", bimXyz.Z);
  143. //return (new JArray(jObject)).ToString();
  144. return str;
  145. }
  146. /// <summary>
  147. /// 获取MBI存储的位置信息
  148. /// </summary>
  149. /// <returns></returns>
  150. public static XYZ ToXyz(this string xyzstr)
  151. {
  152. XYZ xyz = null;
  153. var strs = xyzstr.Split(',');
  154. if (strs.Length == 3)
  155. {
  156. xyz = new XYZ(strs[0].ToDouble(), strs[1].ToDouble(), strs[2].ToDouble());
  157. }
  158. //JObject jObject = new JObject();
  159. //jObject.Add("X", bimXyz.X);
  160. //jObject.Add("Y", bimXyz.Y);
  161. //jObject.Add("Z", bimXyz.Z);
  162. //return (new JArray(jObject)).ToString();
  163. return xyz;
  164. }
  165. /// <summary>
  166. /// 获取设备的种族类型编码 ATFC
  167. /// 族名称的命名规则:ATFC-风机盘管
  168. /// </summary>
  169. /// <returns></returns>
  170. public static string GetFamilyCode(this Element element)
  171. {
  172. string code = "";
  173. if (element is FamilyInstance fi)
  174. {
  175. string familyName = fi.GetFamilyName();
  176. if (familyName == null) return code;
  177. //族名称的命名规则:ATFC-风机盘管
  178. int index = familyName.IndexOf('-');
  179. if (index != -1 && index + 1 != familyName.Length)
  180. code = familyName.Substring(0, familyName.IndexOf('-'));
  181. //移除前面和后面的空格
  182. code = code.Trim();
  183. }
  184. return code;
  185. }
  186. public static string GetFamilyName(this Element element)
  187. {
  188. return element.GetFamily()?.Name;
  189. }
  190. /// <summary>
  191. /// 获取关联的空间
  192. /// </summary>
  193. /// <param name="fi"></param>
  194. /// <returns></returns>
  195. public static Space GetReferenceSpace(this Element element, List<Space> spaces = null)
  196. {
  197. Space space = null;
  198. if (element is FamilyInstance fi)
  199. {
  200. space = fi.Space;
  201. if (space != null) return space;
  202. if (spaces == null)
  203. spaces = fi.Document.GetSpaces().Where(t => t.IsValidObject).ToList();
  204. var origin1 = fi.GetLocationPointMBIXYZ();
  205. foreach (Space tempSpace in spaces)
  206. {
  207. //没有Space属性,取定位点,判断定位点所在空间
  208. if (tempSpace.IsPointInSpace(origin1))
  209. {
  210. space = tempSpace;
  211. break;
  212. }
  213. }
  214. }
  215. else if (element is Wall wall)
  216. {
  217. if (wall.IsCurtaiWall())
  218. space = null;
  219. }
  220. return space;
  221. }
  222. /// <summary>
  223. /// 获取部件所关联的设备
  224. /// </summary>
  225. /// <param name="element"></param>
  226. /// <returns></returns>
  227. public static Element GetEquipPartParent(this Element element)
  228. {
  229. string code = element?.GetFamily().Name.Substring(0, 4); ;
  230. if (code.IsNullOrEmpty()) return null;
  231. //构件所关联的设备
  232. var parentInst = element.Document.GetElements(new ElementIntersectsElementFilter(element))
  233. .FirstOrDefault(t => !t.Id.IsEqual(element.Id) && t.GetFamilyCode() == code);
  234. return parentInst;
  235. }
  236. }
  237. }