Revit family parameter (FamilyParameter) has more information that the model Parameter. Besides those common parameter properties like BuiltInParameterGroup, ParameterType, StorageType, and DisplayUnitType, the FamilyParameter also has some information about whether it is for report purpose only (IsReporting), instance applicable or type applicable (IsInstance), formula assignable (CanAssignFormula), and so on.
In this post, let’s see how to collect the information of FamilyParameter from a family document and put it together into a good place which is ready to use for any possible purposes.
The following help classes and methods will store and handle the FamilyParameter information regarding a family document: public class FamilyParameterInfo
{
public string Name { get; set; }
//public string Value { get; set; } // Parameter Only!
public string ElementID { get; set; }
public string GUID { get; set; }
public string BuiltinID { 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; } // Both but different handling!
public bool Instance { get; set; } // FamilyParameter Only!
public bool Reporting { get; set; } // FamilyParameter Only!
public bool FormulaDetermined { get; set; } // FamilyParameter Only!
public bool CanAssignFormula { get; set; }
public bool ReadOnly { get; set; }
} public static string GetDUTString(FamilyParameter p)
{
string unitType = string.Empty;
try { unitType = p.DisplayUnitType.ToString(); }
catch { } return unitType;
} public static List<FamilyParameterInfo> GetFamilyParametersInfo(Document doc)
{
List<FamilyParameterInfo> paramList =
(from FamilyParameter p in doc.FamilyManager.Parameters
select new FamilyParameterInfo
{
Name = p.Definition.Name,
//Value = p.AsValueString(), //N/A
ElementID = p.Id.IntegerValue.ToString(),
GUID = (p.Definition as ExternalDefinition) != null ?
(p.Definition as ExternalDefinition).GUID.ToString() : string.Empty,
BuiltinID = (p.Definition as InternalDefinition) != null ?
(p.Definition as InternalDefinition).BuiltInParameter.ToString() : string.Empty,
Group = p.Definition.ParameterGroup,
Type = p.Definition.ParameterType,
Storage = p.StorageType,
Unit = GetDUTString(p),
Shared = (p.Definition as ExternalDefinition) != null,
Instance = p.IsInstance,
Reporting = p.IsReporting,
FormulaDetermined = p.IsDeterminedByFormula,
CanAssignFormula = p.CanAssignFormula,
ReadOnly = p.IsReadOnly,
}).ToList(); return paramList;
} And the following code will convert the informative object List into a single string:
public string ParametersInfoToCSVString(List<FamilyParameterInfo> infoList, ref string title)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propInfoArrary = typeof(FamilyParameterInfo).GetProperties();
foreach (PropertyInfo pi in propInfoArrary)
{
title += pi.Name + ",";
}
title = title.Remove(title.Length - 1);
foreach (FamilyParameterInfo 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 FamilyParameter information of an opened family document to a CSV file.
…
if (CachedDoc.IsFamilyDocument)
{
List<FamilyParameterInfo> famParamsInfo = GetFamilyParametersInfo(CachedDoc);
using (StreamWriter sw = new StreamWriter(@"c:\FamilyParametersInfo.csv"))
{
string title = string.Empty;
string rows = ParametersInfoToCSVString(famParamsInfo, ref title);
sw.WriteLine(title);
sw.Write(rows);
}
}
…
Finally the CSV file can be read into a spreadsheet of Excel.
From the output, we can notice something that is not straightforward in the FamilyParameter API. - A FamilyParameter can be neither a built-in parameter (as indicated by the INVALID value of the BuiltInParameter enumeration) nor a shared Parameter (as indicated by the null ExternalDefinition or empty GUID)!
- The sign of the ElementId integer value of the FamilyParameter can be used to indicate whether it has a valid BuiltInParameter value or not!
- The IsDeterminedByFormula seems to always return FALSE or has nothing to do with the CanAssignFormula property!
The informative object list can be used for any other purposes as well for sure. FamilyParameter Infoer of RevitAddinWizardcan help do all of these in a configurable and flexible way.
|