/* ==============================================================================
* 功能描述: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
{
///
/// ElementExtend
///
public static class ElementExtend
{
//信标族名称
public static string BeaconFamilyName = "Beacon";
///
/// 判断是否为设备 设备族为4位
/// ATVR - 多联机 - 室内机 - 双向气流 - 天花板嵌入式
///
///
///
public static bool IsEquipment(this FamilyInstance fi)
{
var family = fi.GetFamily().Name;
return Regex.IsMatch(family, @"^[A-Z]{4}\s*-\s*\S*");
}
///
/// 判断是否为设备部件 设备族为6位
///
///
///
public static bool IsEquipmentPart(this FamilyInstance fi)
{
var family = fi.GetFamily().Name;
return Regex.IsMatch(family, @"^[A-Z]{6}\s*-\s*\S*");
}
///
/// 设备mbi设备名称解析
///
///
///
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*");
}
///
/// 判断是否为信标
///
///
///
public static bool IsBeacon(this FamilyInstance elem)
{
var family = elem.GetFamily().Name;
return (Regex.IsMatch(family, BeaconFamilyName));
}
///
/// 判断是否为空间,判断周长是否为零
/// 如果周长为零,是删除的空间
///
///
/// 是否检查周长为零
///
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;
}
///
/// 获取云平台存储的BimId
/// 文件名:Id
///
///
///
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);
}
///
/// 获取MBI的定位点
///
///
///
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;
}
///
/// 获取MBI存储的位置信息
///
///
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;
}
///
/// 获取MBI存储的位置信息
///
///
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;
}
///
/// 获取设备的种族类型编码 ATFC
/// 族名称的命名规则:ATFC-风机盘管
///
///
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();
}
///
/// 获取关联的空间
///
///
///
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);
}
///
/// 获取部件所关联的设备
///
///
///
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;
}
}
}