Existing shared parameters can be attached programmatically to some categories as specified by the BuiltInCategory and the CategorySet, and can be programmatically put into parameter groups as specified by the BuiltInParameterGroup, just as what can be done through the user interface.
As what we can do manually, shared parameters can also apply to types or instances programmatically. It is achieved through the Binding and BindingMap classes. The NewTypeBinding() method creates a Binding kind of shared parameter to element type; the NewInstanceBinding() a Binding kind of shared parameter to element instance.
The following help method can attach an existing shared parameter specified by its name to a CategorySet, put it under a parameter group (BuiltInParameterGroup), and apply it to element instance or type:
Public Shared Sub RawAttachSharedParameter(ByVal app As RvtApplication, ByVal name As String, ByVal cats As CategorySet, ByVal group As BuiltInParameterGroup, ByVal inst As Boolean)
Dim defFile As DefinitionFile = app.OpenSharedParameterFile()
If defFile Is Nothing Then
Throw New Exception("No SharedParameter File!")
End If
Dim v As List(Of Definition) = (From dg As DefinitionGroup In defFile.Groups From d In dg.Definitions Where d.Name = name)
If v Is Nothing OrElse v.Count() < 1 Then
Throw New Exception("Invalid Name Input!")
End If
Dim def As ExternalDefinition = TryCast(v.First(), ExternalDefinition)
Dim binding As Autodesk.Revit.DB.Binding = app.Create.NewTypeBinding(cats)
If inst Then
binding = app.Create.NewInstanceBinding(cats)
End If
Dim map As BindingMap = (New UIApplication(app)).ActiveUIDocument.Document.ParameterBindings
map.Insert(def, binding, group)
End Sub
The following test code can exercise the above help method.
…
If CachedApp.OpenSharedParameterFile() IsNot Nothing Then
Dim paramsInfo As List(Of RawSharedParameterInfo) = RawGetSharedParametersInfo(CachedApp.OpenSharedParameterFile())
Using sw As New StreamWriter("c:\temp\SharedParametersInfo.csv")
Dim title As String = String.Empty
Dim rows As String = RawParametersInfoToCSVString(paramsInfo, title)
sw.WriteLine(title)
sw.Write(rows)
End Using
End If
Dim wall As Autodesk.Revit.DB.Category = CachedDoc.Settings.Categories.Item(BuiltInCategory.OST_Walls)
Dim door As Autodesk.Revit.DB.Category = CachedDoc.Settings.Categories.Item(BuiltInCategory.OST_Doors)
Dim cats1 As CategorySet = CachedApp.Create.NewCategorySet()
cats1.Insert(wall)
cats1.Insert(door)
RawAttachSharedParameter(CachedApp, "Area11", cats1, BuiltInParameterGroup.PG_DATA, True)
RawAttachSharedParameter(CachedApp, "LengthTest11", cats1, BuiltInParameterGroup.PG_DATA, True)
Dim win As Autodesk.Revit.DB.Category = CachedDoc.Settings.Categories.Item(BuiltInCategory.OST_Windows)
Dim cats2 As CategorySet = CachedApp.Create.NewCategorySet()
cats2.Insert(win)
RawAttachSharedParameter(CachedApp, "URLTest31", cats2, BuiltInParameterGroup.PG_DATA, False)
RawAttachSharedParameter(CachedApp, "MaterialTest21", cats2, BuiltInParameterGroup.PG_DATA, False)
…
SharedParameter Attacher of RevitAddinWizard can create the code either in C# or VB.NET automatically in no time depending on what language of the current Visual Studio project is.
Recent Comments