Revit shared parameters are defined in an external text file and once the file is created and set up it will be used for all models unless the setting is changed later on.
From API point of view, the Revit Application object provides a method, OpenSharedParameterFile(), to get the DefinitionFile for all shared parameters. The DefinitionFile contains DefinitionGroup and ExternalDefinition instances. Each shared parameter has both name and GUID identifiers in it definition object (ExternalDefinition) and belongs to a specific share parameter group (DefinitionGroup).
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
In this post, let’s see how to collect the information of shared parameters from Revit 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 shared parameter information regarding a Revit application:
public class RawSharedParameterInfo
{
public static string FileName { get; set; }
public string Name { get; set; }
public string GUID { get; set; }
public string Owner { get; set; }
public BuiltInParameterGroup Group { get; set; }
public ParameterType Type { get; set; }
public bool ReadOnly { get; set; }
public bool Visible { get; set; }
}
public static List<RawSharedParameterInfo> RawGetSharedParametersInfo(DefinitionFile defFile)
{
RawSharedParameterInfo.FileName = defFile.Filename;
List<RawSharedParameterInfo> paramList =
(from DefinitionGroup dg in defFile.Groups
from ExternalDefinition d in dg.Definitions
select new RawSharedParameterInfo
{
Name = d.Name,
GUID = d.GUID.ToString(),
Owner = d.OwnerGroup.Name,
Group = d.ParameterGroup,
Type = d.ParameterType,
ReadOnly = d.IsReadOnly,
Visible = d.Visible,
}).ToList();
return paramList;
}
And the following code will convert the informative object List into a single string:
public static string RawParametersInfoToCSVString(List<RawSharedParameterInfo> infoList, ref string title)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propInfoArrary = typeof(RawSharedParameterInfo).GetProperties();
foreach (PropertyInfo pi in propInfoArrary)
{
title += pi.Name + ",";
}
title = title.Remove(title.Length - 1);
foreach (RawSharedParameterInfo 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();
}
As a bonus, the following generic method can apply to any type which wants to export its public properties:
public static string RawParametersInfoToCSVString<T>(List<T> infoList, ref string title)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propInfoArrary = typeof(T).GetProperties();
foreach (PropertyInfo pi in propInfoArrary)
{
title += pi.Name + ",";
}
title = title.Remove(title.Length - 1);
foreach (T 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 shared parameter information of a Revit application to a CSV file.
…
if (CachedApp.OpenSharedParameterFile() != null )
{
List<RawSharedParameterInfo> paramsInfo = RawGetSharedParametersInfo(CachedApp.OpenSharedParameterFile());
using (StreamWriter sw = new StreamWriter(@"c:\temp\SharedParametersInfo.csv"))
{
string title = string.Empty;
string rows = RawParametersInfoToCSVString(paramsInfo, ref title);
sw.WriteLine(title);
sw.Write(rows);
}
}
…
Finally the CSV file can be read into a spreadsheet of Excel.
By the way the sample shared parameter external definition file is appended below for reference:
# This is a Revit shared parameter file.
# Do not edit manually.
*META VERSION MINVERSION
META 2 1
*GROUP ID NAME
GROUP 1 Group1
GROUP 2 Group2
GROUP 3 Group3
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE
PARAM 5d4dcb0d-49db-4ac3-a618-98431e8e5f39 Area11 AREA 1 1
PARAM 18daaa5a-1a42-45b0-8330-14eccaa3283c LengthTest11 LENGTH 1 1
PARAM cec3106d-5a45-4191-91e1-f18f3f8c58dd AngleTest21 FORCE 2 1
PARAM a2143977-eecc-4e87-9953-6afaf1bcbddf ForceTest21 FORCE 2 1
PARAM 23e79d90-7f17-4f02-bcdd-aee0fa41f22e Force11 FORCE 1 1
PARAM b3bbc191-68d6-4992-b9d8-c9b81006711b ColorTemprature11 COLOR_TEMPERATURE 1 1
PARAM d29fac95-7a0e-439f-97bd-124a2100ba62 WattageTest21 ELECTRICAL_WATTAGE 2 1
PARAM 7e689e9f-d2ac-46bc-a04f-3741518586f1 YesNoTest31 YESNO 3 1
PARAM e08272bb-d2e1-4be8-8955-50e27f560649 TextTest31 LENGTH 3 1
PARAM a5a433d4-11bd-48c3-8fa0-a59af5281c8a MaterialTest21 MATERIAL 2 1
PARAM 966d79df-9baa-45ec-9334-f49e395ca903 URLTest31 URL 3 1
SharedParameter Infoer of RevitAddinWizard can help do all of these in a configurable and flexible way.
Recent Comments