using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using HStation.RevitDev.RevitDataExport;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Document = Autodesk.Revit.DB.Document;
namespace Glodon.Revit.Utility
{
public class ParameterOperator
{
///
/// 构件属性
///
///
///
///
///
public static Parameter GetParameterByEnum(Document doc, Element inputElement, string attributeName)
{
Parameter useParameter = null;
try
{
var fi = inputElement as FamilyInstance;
if (null != fi)
{
useParameter = fi.LookupParameter(attributeName);
if (useParameter != null)
{
return useParameter;
}
useParameter = fi.Symbol.LookupParameter(attributeName);
if (null != useParameter) { return useParameter; }
}
useParameter = inputElement.LookupParameter(attributeName);
if (null != useParameter) { return useParameter; }
Element element = doc.GetElement(inputElement.GetTypeId());
if (null != element)
{
useParameter = element.LookupParameter(attributeName);
if (null != useParameter) { return useParameter; }
}
}
catch (Exception)
{
}
return null;
}
public static PipingSystemType GetElementPipingSystemType(Element elem)
{
if (elem is Pipe pipe)
{
var mepSystem = pipe.MEPSystem;
if (mepSystem == null) { return null; }
var typeId = mepSystem.GetTypeId();
return elem.Document.GetElement(typeId) as PipingSystemType;
}
else if(elem is FamilyInstance fi)
{
var parameter = fi.LookupParameter("系统类型");
if (parameter == null) { return null; }
if (parameter.StorageType == StorageType.String)
{
var name = parameter.AsString();
var pst = new FilteredElementCollector(elem.Document).OfClass(typeof(PipingSystemType)).ToElements()?.Cast().ToList()?.Find(x=>x.Name == name);
return pst;
}
else if (parameter.StorageType == StorageType.ElementId)
{
var elemId = parameter.AsElementId();
return elem.Document.GetElement(elemId) as PipingSystemType;
}
}
return null;
}
///
/// 获取属性
///
///
///
///
///
public static Parameter GetParameter(Document doc, Element inputElement, string attributeName)
{
return GetParameterByEnum(doc, inputElement, attributeName);
}
///
/// 获取属性值
///
///
///
public static string GetParemeterValue(Parameter parameter)
{
if (parameter == null)
{
return string.Empty;
}
var result = string.Empty;
if (parameter.StorageType == StorageType.Integer)
{
result = parameter.AsInteger().ToString();
}
else if (parameter.StorageType == StorageType.Double)
{
result = parameter.AsDouble().ToString();
}
else if (parameter.StorageType == StorageType.ElementId)
{
result = parameter.AsValueString();
}
else
{
result = string.IsNullOrWhiteSpace(parameter.AsString()) ? parameter.AsValueString() : parameter.AsString();
}
if (result == null)
{
return string.Empty;
}
return result;
}
///
/// 获取名称属性值
///
///
///
///
///
public static string GetElementName(Element eachElement)
{
try
{
//获取实例 族与类型 参数值
Parameter param = eachElement.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM);
string value = GetParemeterValue(param);
if (!String.IsNullOrEmpty(value))
{
return value;
}
return eachElement.Name;
}
catch (Exception)
{
return String.Empty;
}
}
///
/// 获取族与类型参数
///
///
public static string GetElementFamilyAndTypeValue(Element eachElement)
{
try
{
//获取实例 族与类型 参数值
Parameter param = eachElement.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM);
string value = GetParemeterValue(param);
if (!string.IsNullOrEmpty(value))
{
return value;
}
//如 族与类型 获取失败,只提取类型名称
var type = eachElement.Document.GetElement(eachElement.GetTypeId());
return type == null ? string.Empty : type.Name;
}
catch (Exception)
{
return string.Empty;
}
}
///
/// 属性值是否可用
///
///
///
///
private static bool ExtractParameter(IList lstParametet, out string paraValue)
{
paraValue = string.Empty;
foreach (var item in lstParametet)
{
var value = GetParemeterValue(item);
var succeed = value != null
&& !string.IsNullOrWhiteSpace(value)
&& value != "0";
if (succeed)
{
paraValue = value;
return succeed;
}
}
return false;
}
///
/// 是否为mep构件
///
///
private static bool IsMEPInstace(BuiltInCategory instanceCategory)
{
switch (instanceCategory)
{
case BuiltInCategory.OST_DuctSystem:
case BuiltInCategory.OST_PipingSystem:
return true;
default:
return false;
}
}
public static string GetPipeSystemTypeName(Pipe pipe)
{
try
{
var parameter = pipe.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
return parameter.AsValueString();
}
catch (Exception)
{
return string.Empty;
}
}
public static string GetDuctSystemTypeName(Duct duct)
{
try
{
var parameter = duct.get_Parameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
if (parameter == null) { return null; }
return parameter.AsValueString();
}
catch (Exception)
{
return string.Empty;
}
}
///
/// 获取参数值 AsDouble
///
///
///
///
public static double? GetParameterValueAsDouble(Element elem, BuiltInParameter builtInParameter)
{
double? result = null;
if (elem != null && elem.get_Parameter(builtInParameter) is Parameter para)
{
if (para == null || !para.HasValue) { return result; }
result =para.AsDouble();
}
return result;
}
///
/// 获取参数值 AsDouble
///
///
///
/// 单位转换方法
///
public static double? GetParameterValueAsDouble(Element elem, string paramName, Func