ElementExtend.cs 8.1 KB

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