Revit Parameter has a lot of relevant information such as group, type, unit, storage type, shared or native/built-in, read only or writable, and the most important ones name, value and id (BuiltInParameter value or GUID), but they are scattered around in a few different API objects such as the Definition (which has two derivatives InternalDefinition and ExternalDefinition to address different kinds of Parameter instances).
In this post, let’s see how to collect all these pieces of information from an Element and put them together into a good place to make it ready to use for any possible purposes.
The following code can find all the information from a selected Element:
Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element; List<undefinedarameter> paramList = ParametersOf(element);
string str = string.Empty;
foreach (Parameter p in paramList)
{
//The following has to be done because the DUT will throw out exceptions many times!
string unitType = string.Empty;
try { unitType = p.DisplayUnitType.ToString(); }
catch { }
str += string.Format("{0}\tPG:{1}\tPT:{2}\tST:{3}\tDUT:{4}\n\tSH:{5}\tID:{6}\tRO:{7}\n",
p.Definition.Name,
p.Definition.ParameterGroup,
p.Definition.ParameterType,
p.StorageType,
unitType,
p.IsShared,
p.IsShared ? p.GUID.ToString()undefinedp.Definition as InternalDefinition).BuiltInParameter.ToString(),
p.IsReadOnly);
} MessageBox.Show(str, "Information of Element Parameters"); As commented in the code, the DisplayUnitType property of the Parameter object looks very special. It throws out exceptions very often and this will interrupt the whole process. The only way for programmers to address the issue is to use a try/catch block as demonstrated but that is definitely not something so cool. It is somewhat due to the fact that the DisplayUnitType enumeration lacks a value to indicate that DisplayUnitType does not make any sense in some situations just like the INVALID value in the BuiltInParameterGroup enumeration or Invalid in ParameterType.
Anyway we have a way to work around the issue and the above code can successfully print out the Parameter information like:
As mentioned earlier, we’d like to have a nice place to keep all the information so that we can use it in an easy way when necessary. The following help classes and methods will store all the Parameter information of an Element into a List of an informative object:
public class ParameterInfo
{
public string Name { get; set; }
public string Value { get; set; }
public BuiltInParameterGroup Group { get; set; }
public ParameterType Type { get; set; }
public StorageType Storage { get; set; }
public string Unit { get; set; } //DisplayUnitType doesn't work!
public bool Shared { get; set; }
public string ID { get; set; }
public bool ReadOnly { get; set; }
}
public static List<undefinedarameterInfo> GetParametersInfo(Element e)
{
List<undefinedarameterInfo> paramList =
(from Parameter p in e.Parameters
select new ParameterInfo
{
Name = p.Definition.Name,
Value = p.AsValueString(),
Group = p.Definition.ParameterGroup,
Type = p.Definition.ParameterType,
Storage = p.StorageType,
Unit = GetDUTString(p),
Shared = p.IsShared,
ReadOnly = p.IsReadOnly,
ID = p.IsShared ? p.GUID.ToString() :
(p.Definition as InternalDefinition).BuiltInParameter.ToString()
}).ToList(); return paramList;
}
And the following code will convert the informative object List into a single string:
public string ParametersInfoToCSVString(List<undefinedarameterInfo> infoList, ref string title)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propInfoArrary = typeof(ParameterInfo).GetProperties();
foreach (PropertyInfo pi in propInfoArrary)
{
title += pi.Name + ",";
}
title = title.Remove(title.Length - 1);
foreach (ParameterInfo info in infoList)
{
foreach (PropertyInfo pi in propInfoArrary)
{
object obj = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, info, null);
sb.Append( (obj == null ? string.Empty : obj.ToString()) + ",");
}
sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine);
}
return sb.ToString();
} Then we can write all the Parameter information of a selected Element to a CSV file, for example: public static Reference SelElement(Selection selection)
{
Reference picked = selection.PickObject(ObjectType.Element, "undefinedlease select an element");
return picked;
} …
Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element; List<undefinedarameterInfo> paramsInfo = GetParametersInfo(element);
using (StreamWriter sw = new StreamWriter(@"c:\ParametersInfo.csv"))
{
string title = string.Empty;
string rows = ParametersInfoToCSVString(paramsInfo, ref title);
sw.WriteLine(title);
sw.Write(rows);
}
… Finally the CSV file can be read into a spreadsheet of Excel for example:
The informative object list can be used for any other purposes as well for sure.
Parameter Infoer of RevitAddinWizardcan help do all of these in a configurable and flexible way. |