/*------------------------------------------------------------------------- * 功能描述:ParameterExtension * 作者:xulisong * 创建时间: 2019/3/7 9:08:09 * 版本号:v1.0 * -------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.Revit.DB; namespace FWindSoft.Revit { public static class ParameterExtension { /// /// 获取参数名 /// /// /// public static string GetName(this Parameter p) { return p.Definition?.Name; } public static bool ExistParameter(this Element element,string parameterName) { return element.GetParameter(parameterName) != null; } /// /// 获取参数值 /// /// /// public static object GetValue(this Parameter parameter) { object result = null; if (parameter == null) { return null; } switch (parameter.StorageType) { case StorageType.Double: { result = parameter.AsDouble(); break; } case StorageType.ElementId: { result = parameter.AsElementId(); break; } case StorageType.Integer: { result = parameter.AsInteger(); break; } case StorageType.String: { result = parameter.AsString(); break; } case StorageType.None: { result = parameter.AsValueString(); break; } } return result; } public static bool SetValue(this Parameter parameter, object value) { bool result = false; if (value == null || parameter.IsReadOnly) { return result; } switch (parameter.StorageType) { case StorageType.Double: { result = parameter.Set(Convert.ToDouble(value)); break; } case StorageType.ElementId: { result = parameter.Set((ElementId) value); break; } case StorageType.Integer: { result = parameter.Set(Convert.ToInt32(value)); break; } case StorageType.None: { result = parameter.SetValueString(value as string); break; } case StorageType.String: { result = parameter.Set(value.ToString()); break; } } return result; } public static Parameter GetParameter(this Element element, string parameterName) { return (new ParameterDefinition(parameterName)).LookupParameter(element); } public static Parameter GetParameter(this Element element, BuiltInParameter builtInParameter) { return (new ParameterDefinition(builtInParameter)).LookupParameter(element); } public static Parameter GetParameter(this Element element, Guid guid) { return (new ParameterDefinition(guid)).LookupParameter(element); } #region 获取参数相关 /// /// 类型有要求,必须为ElementID,int,double ,string 四中其中的一个 /// /// /// /// /// public static T GetParameterValue(this Element element, string name) { return (T)element.GetParameter(name).GetValue(); } public static T GetParameterValue(this Element element, BuiltInParameter builtInParameter) { return (T)element.GetParameter(builtInParameter).GetValue(); } public static T GetParameterValue(this Element element, Guid guid) { return (T)element.GetParameter(guid).GetValue(); } public static string GetParameterString(this Element element, string name) { return element.GetParameterValue(name); } public static int GetParameterInteger(this Element element, string name) { return element.GetParameterValue(name); } public static double GetParameterDouble(this Element element, string name) { return element.GetParameterValue(name); } public static ElementId GetParameterElementId(this Element element, string name) { return element.GetParameterValue(name); } public static string GetParameterString(this Element element, BuiltInParameter builtInParameter) { return element.GetParameterValue(builtInParameter); } public static int GetParameterInteger(this Element element, BuiltInParameter builtInParameter) { return element.GetParameterValue(builtInParameter); } public static double GetParameterDouble(this Element element, BuiltInParameter builtInParameter) { return element.GetParameterValue(builtInParameter); } public static ElementId GetParameterElementId(this Element element, BuiltInParameter builtInParameter) { return element.GetParameterValue(builtInParameter)?? ElementId.InvalidElementId; } #endregion #region 设置参数值 public static bool SetParameterValue(this Element element, string name,T value) { return element.GetParameter(name).SetValue(value); } public static bool SetParameterValue(this Element element, BuiltInParameter builtInParameter,T value) { return element.GetParameter(builtInParameter).SetValue(value); } public static bool SetParameterValue(this Element element, Guid guid, T value) { return element.GetParameter(guid).SetValue(value); } public static bool SetParameter(this Element element, string name,string value) { return element.SetParameterValue(name,value); } public static bool SetParameter(this Element element, string name,int value) { return element.SetParameterValue(name,value); } public static bool SetParameter(this Element element, string name,double value) { return element.SetParameterValue(name,value); } public static bool SetParameter(this Element element, string name,ElementId value) { return element.SetParameterValue(name,value); } public static bool SetParameter(this Element element, BuiltInParameter builtInParameter, string value) { return element.SetParameterValue(builtInParameter,value); } public static bool SetParameter(this Element element, BuiltInParameter builtInParameter, int value) { return element.SetParameterValue(builtInParameter,value); } public static bool SetParameter(this Element element, BuiltInParameter builtInParameter, double value) { return element.SetParameterValue(builtInParameter,value); } public static bool SetParameter(this Element element, BuiltInParameter builtInParameter, ElementId value) { return element.SetParameterValue(builtInParameter,value); } #endregion } }