We have addressed converting Parameter to Shared Parameter previously. Now let’s see how to convert FamilyParameter to Shared Parameter with VB.NET.
Before creating some specific code for the FamilyParameter 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 FamilyParameter Infoer"
Public Class RawFamilyParameterInfo
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 ElementID() As String
Get
Return m_ElementID
End Get
Set(ByVal value As String)
m_ElementID = Value
End Set
End Property
Private m_ElementID As String
Public Property GUID() As String
Get
Return m_GUID
End Get
Set(ByVal value As String)
m_GUID = Value
End Set
End Property
Private m_GUID As String
Public Property BuiltinID() As String
Get
Return m_BuiltinID
End Get
Set(ByVal value As String)
m_BuiltinID = Value
End Set
End Property
Private m_BuiltinID 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 Instance() As Boolean
Get
Return m_Instance
End Get
Set(ByVal value As Boolean)
m_Instance = Value
End Set
End Property
Private m_Instance As Boolean
Public Property Reporting() As Boolean
Get
Return m_Reporting
End Get
Set(ByVal value As Boolean)
m_Reporting = Value
End Set
End Property
Private m_Reporting As Boolean
Public Property FormulaDetermined() As Boolean
Get
Return m_FormulaDetermined
End Get
Set(ByVal value As Boolean)
m_FormulaDetermined = Value
End Set
End Property
Private m_FormulaDetermined As Boolean
Public Property CanAssignFormula() As Boolean
Get
Return m_CanAssignFormula
End Get
Set(ByVal value As Boolean)
m_CanAssignFormula = Value
End Set
End Property
Private m_CanAssignFormula As Boolean
End Class
Public Shared Function RawGetDUTString(ByVal p As FamilyParameter) As String
Dim unitType As String = String.Empty
Try
unitType = p.DisplayUnitType.ToString()
Catch
End Try
Return unitType
End Function
Public Shared Function RawGetFamilyParametersInfo(ByVal doc As Document) As List(Of RawFamilyParameterInfo)
Dim paramList As List(Of RawFamilyParameterInfo) = (From p In doc.FamilyManager.Parameters Select New RawFamilyParameterInfo() With { _
.Name = p.Definition.Name, _
.ElementID = p.Id.IntegerValue.ToString(), _
.GUID = If(TryCast(p.Definition, ExternalDefinition) IsNot Nothing, TryCast(p.Definition, ExternalDefinition).GUID.ToString(), String.Empty), _
.BuiltinID = If(TryCast(p.Definition, InternalDefinition) IsNot Nothing, TryCast(p.Definition, InternalDefinition).BuiltInParameter.ToString(), String.Empty), _
.Group = p.Definition.ParameterGroup, _
.Type = p.Definition.ParameterType, _
.Storage = p.StorageType, _
.Unit = RawGetDUTString(p), _
.Shared = TryCast(p.Definition, ExternalDefinition) IsNot Nothing, _
.ReadOnly = p.IsReadOnly, _
.Instance = p.IsInstance, _
.Reporting = p.IsReporting, _
.FormulaDetermined = p.IsDeterminedByFormula, _
.CanAssignFormula = p.CanAssignFormula _
}).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
With all the above code handy, it becomes a pretty easy task to create some code to convert a particular FamilyParameter or all FamilyParameter instances of a Revit Family Document to shared parameters:
Public Shared Sub ConvertFamilyParameterInfoToSharedParameter(ByVal familyDoc As Document, ByVal info As RawFamilyParameterInfo, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
If info.Type = ParameterType.Invalid Then
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" & Convert.ToString(familyDoc.Title), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" & Convert.ToString(familyDoc.Title), info.Type, visibilityOverride)
End If
End Sub
Public Shared Sub ConvertFamilyParameterToSharedParameter(ByVal familyDoc As Document, ByVal name As String, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawFamilyParameterInfo) = RawGetFamilyParametersInfo(familyDoc)
Dim pListOfInterest As List(Of RawFamilyParameterInfo) = pList.FindAll(Function(p) p.Name = name)
For Each info As RawFamilyParameterInfo In pListOfInterest
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride)
Next
End Sub
Public Shared Sub ConvertAllFamilyParametersToSharedParameters(ByVal familyDoc As Document, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawFamilyParameterInfo) = RawGetFamilyParametersInfo(familyDoc)
For Each info As RawFamilyParameterInfo In pList
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride)
Next
End Sub
A few points may be worth of mentioning here:
• The ParameterType of some valid FamilyParameter instances may be Invalid! We assign the most popular Text ParameterType to the FamilyParameter instances in this case to resolve the creation problem of the affected shared parameters.
• The name of the DefinitionGroup of all the shared parameters will be defaulted as For_<Family Document Name>.
• Since the visibility information of the original shared parameter definitions if any is not easy to find back, we give a chance in these methods to override the visibility for the shared parameters.
The following test code can exercise the methods:
ConvertFamilyParameterToSharedParameter(CachedDoc, "URL", False, True)
ConvertAllFamilyParametersToSharedParameters(CachedDoc, 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#.
FamilyParameter Converter of RevitAddinWizard can create all these in a second in C# or VB.NET automatically based on the language of the current addin project.
Recent Comments