123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /* ==============================================================================
- * 功能描述:ElementExtend
- * 创 建 者:Garrett
- * 创建日期:2018/5/28 16:41:22
- * ==============================================================================*/
- using System.Linq;
- using System.Text.RegularExpressions;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Mechanical;
- using MBIUtility.Tool;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.RevitUtils.Extends;
- namespace MBIUtility.Extend
- {
- /// <summary>
- /// ElementExtend
- /// </summary>
- public static class ElementExtend
- {
- //信标族名称
- public static string BeaconFamilyName = "Beacon";
- /// <summary>
- /// 判断是否为设备 设备族为4位
- /// ATVR - 多联机 - 室内机 - 双向气流 - 天花板嵌入式
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- public static bool IsEquipment(this FamilyInstance fi)
- {
- var family = fi.GetFamily().Name;
- return Regex.IsMatch(family, @"^[A-Z]{4}\s*-\s*\S*");
- }
- /// <summary>
- /// 判断是否为设备部件 设备族为6位
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- public static bool IsEquipmentPart(this FamilyInstance fi)
- {
- var family = fi.GetFamily().Name;
- return Regex.IsMatch(family, @"^[A-Z]{6}\s*-\s*\S*");
- }
- /// <summary>
- /// 设备mbi设备名称解析
- /// </summary>
- /// <param name="family"></param>
- /// <returns></returns>
- public static bool IsMbiEquipment(this Family family)
- {
- var familyName = family.Name;
- if (string.IsNullOrEmpty(familyName))
- return false;
- return Regex.IsMatch(familyName, @"^[A-Z]{4}\s*-\s*\S*") || Regex.IsMatch(familyName, @"^[A-Z]{6}\s*-\s*\S*");
- }
- /// <summary>
- /// 判断是否为信标
- /// </summary>
- /// <param name="elem"></param>
- /// <returns></returns>
- public static bool IsBeacon(this FamilyInstance elem)
- {
- var family = elem.GetFamily().Name;
- return (Regex.IsMatch(family, BeaconFamilyName));
- }
- /// <summary>
- /// 判断是否为空间,判断周长是否为零
- /// 如果周长为零,是删除的空间
- /// </summary>
- /// <param name="elem"></param>
- /// <param name="ischeckzero">是否检查周长为零</param>
- /// <returns></returns>
- public static bool IsSpace(this Element elem, bool ischeckzero = true)
- {
- var isspace = false;
- if (elem is Space space)
- {
- //空间比较特殊,周长为零就相当于删除
- isspace = !ischeckzero || !(space.IsDeleteSpace());
- //限制所用空间的阶段
- //isspace = isspace && space.IsPhase1Space();
- }
- return isspace;
- }
- /// <summary>
- /// 获取云平台存储的BimId
- /// 文件名:Id
- /// </summary>
- /// <param name="elem"></param>
- /// <returns></returns>
- public static string GetCloudBIMId(this Element elem)
- {
- var doc = elem.Document;
- var pathName = doc.PathName;
- //楼层文件名称,无后缀
- var docName = pathName.GetFileName();
- var id = elem.Id.ToString();
- return CommonTool.GetCloudBIMId(docName, id);
- }
- /// <summary>
- /// 获取MBI的定位点
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static XYZ GetLocationPointMBIXYZ(this Element element)
- {
- ////定位点不可靠,未来可能会更改为Box的中心点
- //XYZ bimXyz = element.GetLocationPoint();
- //if (element is FamilyInstance fi)
- //{
- // var family = fi.GetFamily();
- // if (family.IsInPlace)
- // {
- // bimXyz = fi.GetBoxCenter();
- // }
- //}
- //定位点改为Box中心点
- XYZ bimXyz = element.GetBoxCenter();
- return bimXyz;
- }
- /// <summary>
- /// 获取MBI存储的位置信息
- /// </summary>
- /// <returns></returns>
- public static string GetLocationPointMBI(this Element element)
- {
- string str = ",,";
- XYZ bimXyz = element.GetLocationPointMBIXYZ();
- if (bimXyz != null)
- {
- str = bimXyz.FromApi().ToString(null);
- };
- //JObject jObject = new JObject();
- //jObject.Add("X", bimXyz.X);
- //jObject.Add("Y", bimXyz.Y);
- //jObject.Add("Z", bimXyz.Z);
- //return (new JArray(jObject)).ToString();
- return str;
- }
- /// <summary>
- /// 获取MBI存储的位置信息
- /// </summary>
- /// <returns></returns>
- public static XYZ ToXyz(this string xyzstr)
- {
- XYZ xyz = null;
- var strs = xyzstr.Split(',');
- if (strs.Length == 3)
- {
- xyz = new XYZ(strs[0].ToDouble(), strs[1].ToDouble(), strs[2].ToDouble());
- }
- //JObject jObject = new JObject();
- //jObject.Add("X", bimXyz.X);
- //jObject.Add("Y", bimXyz.Y);
- //jObject.Add("Z", bimXyz.Z);
- //return (new JArray(jObject)).ToString();
- return xyz;
- }
- /// <summary>
- /// 获取设备的种族类型编码 ATFC
- /// 族名称的命名规则:ATFC-风机盘管
- /// </summary>
- /// <returns></returns>
- public static string GetFamilyCode(this Element fi)
- {
- string code = "";
- string familyName = fi.GetFamily()?.Name;
- if (familyName == null) return code;
- //族名称的命名规则:ATFC-风机盘管
- int index = familyName.IndexOf('-');
- if (index != -1 && index + 1 != familyName.Length)
- code = familyName.Substring(0, familyName.IndexOf('-'));
- //移除前面和后面的空格
- return code.Trim();
- }
- /// <summary>
- /// 获取关联的空间
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- public static Space GetReferenceSpace(this FamilyInstance fi)
- {
- Space space = fi.Space;
- if (space != null) return space;
- var spaces = fi.Document.GetSpaces().Where(t => t.IsValidObject).ToList();
- Space temSpace2 = null;
- var origin1 = fi.GetLocationPointMBIXYZ();
- var origin2 = fi.GetBoxCenter();
- foreach (Space tempSpace in spaces)
- {
- //没有Space属性,取定位点,判断定位点所在空间
- if (tempSpace.IsPointInSpace(origin1))
- {
- space = tempSpace;
- break;
- }
- //还没有找到空间,取Box中心点,判断点所在空间
- if (temSpace2 != null) continue;
- if (tempSpace.IsPointInSpace(origin2))
- {
- temSpace2 = tempSpace;
- }
- }
- return space ?? (temSpace2);
- }
- /// <summary>
- /// 获取部件所关联的设备
- /// </summary>
- /// <param name="fi"></param>
- /// <returns></returns>
- public static FamilyInstance GetPartParentInstance(this FamilyInstance fi)
- {
- Document doc = fi.Document;
- string family = fi.GetFamily().Name.Substring(0, 4);
- //构件所关联的设备
- var parentInst = doc.GetElements(new ElementIntersectsElementFilter(fi))
- .FirstOrDefault(t => t is FamilyInstance && !t.Id.IsEqual(fi.Id) && t.GetFamilyCode() == family);
-
- return parentInst as FamilyInstance;
- }
- }
- }
|