Revit project parameters are different from built-in parameters (BuiltInParameter), family parameters (FamilyParameter), and shared parameters. Project parameters may relate to shared parameters in some way as we talked about previously.
As can be seen from the Project Parameters dialog in Revit, project parameters can be defined from both ‘project parameters’ themselves and shared parameters.
The former is not achievable programmatically due to the fact that the InternalDefinition or native Parameter cannot be created with the Revit API. Though there is an InternalDefinitions.Create() method in the API and it looks like being supposed to fulfill the task but the point is that there is nowhere and no way to get an instance of the InternalDefinitions.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
The latter is doable programmatically through different ways, from existing shared parameters, from newly created shared parameters, or from temporary shared parameters in some temporary external definition files. In fact, in another post, Attach Shared Parameter with VB.NET, we have already introduced the first approach, creating a project parameter from an existing shared parameter or attaching a shared parameter to some categories (indirectly to elements).
In this post, let’s append that method here again along with two more methods to make the whole story complete.
Public Shared Sub RawCreateProjectParameterFromExistingSharedParameter(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 Select dg 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 = v.First()
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
Public Shared Sub RawCreateProjectParameterFromNewSharedParameter(ByVal app As RvtApplication, ByVal defGroup As String, ByVal name As String, ByVal type As ParameterType, ByVal visible As Boolean, ByVal cats As CategorySet, _
ByVal paramGroup 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 def As ExternalDefinition = TryCast(app.OpenSharedParameterFile().Groups.Create(defGroup).Definitions.Create(name, type, visible), 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, paramGroup)
End Sub
Public Shared Sub RawCreateProjectParameter(ByVal app As RvtApplication, ByVal name As String, ByVal type As ParameterType, ByVal visible As Boolean, ByVal cats As CategorySet, ByVal group As BuiltInParameterGroup, _
ByVal inst As Boolean)
'InternalDefinition def = new InternalDefinition();
'Definition def = new Definition();
Dim oriFile As String = app.SharedParametersFilename
Dim tempFile As String = Path.GetTempFileName() & ".txt"
Using File.Create(tempFile)
End Using
app.SharedParametersFilename = tempFile
Dim def As ExternalDefinition = TryCast(app.OpenSharedParameterFile().Groups.Create("TemporaryDefintionGroup").Definitions.Create(name, type, visible), ExternalDefinition)
app.SharedParametersFilename = oriFile
File.Delete(tempFile)
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
It may be worth of a few more words about the third method. It creates a temporary shared parameter file and switch the current shared parameter file over regardless of what it is, creates the named shared parameter in a temporary DefinitionGroup, creates a type binding or instance binding accordingly for the shared parameter, adds the Binding to the BindingMap got from the ParameterBindings property of the Revit Document of concern, and finally switches back to the original shared parameter file.
The following test code can be used to exercise the help methods:
…
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)
RawCreateProjectParameterFromExistingSharedParameter(CachedApp, "ExistingParameter1", cats1, BuiltInParameterGroup.PG_DATA, False)
RawCreateProjectParameterFromNewSharedParameter(CachedApp, "NewDefinitionGroup1", "NewParameter1", ParameterType.Text, True, cats1, BuiltInParameterGroup.PG_DATA, False)
RawCreateProjectParameter(CachedApp, "TemporarySharedParameter", ParameterType.Text, True, cats1, BuiltInParameterGroup.PG_DATA, True)
…
ProjectParameter Creator of RevitAddinWizard can help create the code in VB.NET or C# automatically in a configurable and flexible way in a second.
Recent Comments