Revit shared parameters are different from built-in or native parameters (BuiltInParameter). Shared parameters can be user created and actually all of them are supposed to be defined or created by users or third parties. BuiltInParameter instances however are hard coded and maintained by Revit itself.
In this post, let’s see how to create shared parameters programmatically with Revit API and VB.NET.
The following help methods can help achieve the goal:
Public Shared Function RawConvertSetToList(Of T)(ByVal setVar As IEnumerable) As List(Of T)
Dim list As List(Of T) = (From p In setVar Select p)
Return list
End Function
Public Shared Function RawCreateSharedParameter(ByVal app As RvtApplication, ByVal name As String, ByVal group As String, ByVal type As ParameterType, ByVal visible As Boolean) As Definition
Dim defFile As DefinitionFile = app.OpenSharedParameterFile()
If defFile Is Nothing Then
Throw New Exception("No SharedParameter File!")
End If
Dim dg As DefinitionGroup = RawConvertSetToList(Of DefinitionGroup)(defFile.Groups).FirstOrDefault(Function(g) g.Name = group)
If dg Is Nothing Then
dg = defFile.Groups.Create(group)
End If
Dim def As Definition = RawConvertSetToList(Of Definition)(dg.Definitions).FirstOrDefault(Function(d) d.Name = name)
If def IsNot Nothing Then
Return def
End If
'dg.Definitions.Erase(def); //ReadOnly Exception!!
def = dg.Definitions.Create(name, type, visible)
Return def
End Function
As the code indicates, the first step is to get the DefinitionFile object through calling the method OpenSharedParameterFile of the Revit Application, the second step is to find the DefinitionGroup of interest or create one when necessary, the last is to create the Definition (ExternalDefinition actually) into the Definitions collection from the given parameter name, type, and visibility.
Also a side finding as commented in the code: please do not try to erase an existing shared parameter represented by a Definition (actually ExternalDefinition) object with the straightforward DefinitionGroup.Definitions.Erase() method. Otherwise, an unexpected exception would just occur.
We will talk about this in a bit more detail later and see if there is a way to work around the issue.
The following test code can be used to exercise the help methods:
…
RawCreateSharedParameter(CachedApp, "Volume11", "Group1", ParameterType.Volume, True)
RawCreateSharedParameter(CachedApp, "Volume41", "Group4", ParameterType.Volume, True)
RawCreateSharedParameter(CachedApp, "Volume42", "Group4", ParameterType.Volume, False)
RawCreateSharedParameter(CachedApp, "LengthTest11", "Group1", ParameterType.Text, False)
…
If the external shared parameter definition file looks like the following before the test code is run:
# 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
The final shared parameter file will look like this:
# 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
GROUP 4 Group4
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE
PARAM 5d4dcb0d-49db-4ac3-a618-98431e8e5f39 Area11 AREA 1 1
PARAM 83c7b23d-c7dc-4894-a4a7-ad3f60d37477 Volume42 VOLUME 4 0
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 8f2c078c-81ff-462c-a581-df071f2e470b Volume11 VOLUME 1 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 d57fb9b5-42cd-4c72-b6f1-df97fcef1d71 Volume41 VOLUME 4 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 Creator of RevitAddinWizard can help create the VB.NET code (or C#) automatically in a configurable and flexible way in a second.
Recent Comments