ElementExtend.cs 7.1 KB

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