ElementExtend.cs 8.1 KB

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