ElementExtend.cs 7.2 KB

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