ElementExtend.cs 8.4 KB

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