We have talked about various aspects of each parameter kind such as element parameter, family parameter, shared parameter, and project parameter of Revit API. From now on, let’s see the programmatic interchangeability with VB.NET between any two kinds of these Revit parameters.
Before creating some specific code for the Parameter Converter, we need to put some Coders together first that we introduced and demonstrated before. They will save us a lot of precious time and avoid duplicate efforts.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
Here they are:
#Region "From Parameter Infoer"
Public Class RawParameterInfo
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Value() As String
Get
Return m_Value
End Get
Set(ByVal value As String)
m_Value = Value
End Set
End Property
Private m_Value As String
Public Property Group() As BuiltInParameterGroup
Get
Return m_Group
End Get
Set(ByVal value As BuiltInParameterGroup)
m_Group = Value
End Set
End Property
Private m_Group As BuiltInParameterGroup
Public Property Type() As ParameterType
Get
Return m_Type
End Get
Set(ByVal value As ParameterType)
m_Type = Value
End Set
End Property
Private m_Type As ParameterType
Public Property Storage() As StorageType
Get
Return m_Storage
End Get
Set(ByVal value As StorageType)
m_Storage = Value
End Set
End Property
Private m_Storage As StorageType
Public Property Unit() As String
Get
Return m_Unit
End Get
Set(ByVal value As String)
m_Unit = Value
End Set
End Property
Private m_Unit As String
Public Property [Shared]() As Boolean
Get
Return m_Shared
End Get
Set(ByVal value As Boolean)
m_Shared = Value
End Set
End Property
Private m_Shared As Boolean
Public Property [ReadOnly]() As Boolean
Get
Return m_ReadOnly
End Get
Set(ByVal value As Boolean)
m_ReadOnly = Value
End Set
End Property
Private m_ReadOnly As Boolean
Public Property ID() As String
Get
Return m_ID
End Get
Set(ByVal value As String)
m_ID = Value
End Set
End Property
Private m_ID As String
End Class
Public Shared Function RawGetDUTString(ByVal p As Parameter) As String
Dim unitType As String = String.Empty
Try
unitType = p.DisplayUnitType.ToString()
Catch
End Try
Return unitType
End Function
Public Shared Function RawGetParametersInfo(ByVal e As Autodesk.Revit.DB.Element) As List(Of RawParameterInfo)
Dim paramList As List(Of RawParameterInfo) = (From p In e.Parameters Select New RawParameterInfo() With { _
.Name = p.Definition.Name, _
.Value = p.AsValueString(), _
.Group = p.Definition.ParameterGroup, _
.Type = p.Definition.ParameterType, _
.Storage = p.StorageType, _
.Unit = RawGetDUTString(p), _
.Shared = p.IsShared, _
.ReadOnly = p.IsReadOnly, _
.ID = If(p.IsShared, p.GUID.ToString(), TryCast(p.Definition, InternalDefinition).BuiltInParameter.ToString()) _
}).ToList()
Return paramList
End Function
#End Region
#Region "From SharedParameter Creator"
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
#End Region
#Region "From Object Picker"
Public Shared Function SelectAnyElement(ByVal selection As Autodesk.Revit.UI.Selection.Selection) As Reference
Dim picked As Reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Select an Autodesk.Revit.DB.Element of any type")
Return picked
End Function
#End Region
With all the above code handy, it becomes a pretty easy task to create some code to convert a particular paramter or all parameters of a Revit Element to shared parameters:
Public Shared Sub ConvertParameterToSharedParameter(ByVal e As Autodesk.Revit.DB.Element, ByVal name As String, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawParameterInfo) = RawGetParametersInfo(e)
Dim pListOfInterest As List(Of RawParameterInfo) = pList.FindAll(Function(p) p.Name = name)
For Each info As RawParameterInfo In pListOfInterest
If info.Type = ParameterType.Invalid Then
'It's weird that a valid Parameter can have an invalid ParameterType! We set it as the most popular Text type in this case. Otherwise, the DefinitionGroup.Definitions.Create() would fail!
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(e.Document.Application, name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(e.Document.Application, name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Next
End Sub
Public Shared Sub ConvertAllParametersToSharedParameters(ByVal e As Autodesk.Revit.DB.Element, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawParameterInfo) = RawGetParametersInfo(e)
For Each info As RawParameterInfo In pList
If info.Type = ParameterType.Invalid Then
'It's weird that a valid Parameter can have an invalid ParameterType! We set it as the most popular Text type in this case. Otherwise, the DefinitionGroup.Definitions.Create() would fail!
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(e.Document.Application, info.Name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(e.Document.Application, info.Name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Next
End Sub
A few points may be worth of mentioning here:
• The ParameterType of some valid Parameter instances may be Invalid! We assign the most popular Text ParameterType to the Parameter instances in this case to resolve the creation problem of shared parameters.
• The name of the DefinitionGroup of all the shared parameters will be defaulted as For_<Category Name> of the picked element.
• Since the visibility information of the original shared parameter definitions is nowhere to find, we provide a chance in these methods to override it for the shared parameters going to be created.
The following test code can exercise the methods:
Dim e As Autodesk.Revit.DB.Element = SelectAnyElement(CachedUiApp.ActiveUIDocument.Selection).Element
'ConvertParameterToSharedParameter(e, "Volume41", true, true);
ConvertAllParametersToSharedParameters(e, False, True)
In terms of details of the various coders referenced in this post please refer to previous posts for ideas and code examples either in VB.NET or C#.
Parameter Converter of RevitAddinWizard can create all these in a second in either VB.NET or C# automatically based on the language of the current Revit Addin project.
Recent Comments