We talked about getting information of project parameters and creating them with various ways before. Now let’s look at how to erase project parameters in this post.
Let’s see how to erase a specific project parameter first. In fact, we provided the method before but it’s mainly to show some misconceptions there. Here is the updated version which can specifically address a project parameter in a Revit Document of interest:
Public Shared Function RawEraseProjectParameter(ByVal doc As RvtDocument, ByVal name As String, ByVal type As ParameterType) As Boolean
Dim map As BindingMap = doc.ParameterBindings
Dim it As DefinitionBindingMapIterator = map.ForwardIterator()
it.Reset()
Dim def As Definition = Nothing
While it.MoveNext()
If it.Key IsNot Nothing AndAlso it.Key.Name = name AndAlso type = it.Key.ParameterType Then
def = it.Key
Exit While
End If
End While
If def IsNot Nothing Then
map.Remove(def)
End If
Return False
End Function
The following test code can exercise it:
…
RawEraseProjectParameter(CachedDoc, "Volume41", ParameterType.Volume)
…
Next, is there a strightfoward API way to remove all project prameters from a Revit Document?
The BindingMap.Clear() seems to do so:
Public Shared Sub RawEraseAllProjectParameters(ByVal doc As RvtDocument)
Dim map As BindingMap = doc.ParameterBindings
map.Clear()
End Sub
The following code tests it:
…
RawEraseAllProjectParameters(CachedDoc)
…
but the following exception would occur:
Autodesk.Revit.Exceptions.InvalidOperationException
at Autodesk.Revit.DB.BindingMap.Clear()
Q: Why?
A: No idea!
How could it be done programmatically then? People may wonder. Obviously, we have to iterate through the BindingMap of interest and remove its Bindings one by one:
Public Shared Sub RawTruelyEraseAllProjectParameters(ByVal doc As RvtDocument)
Dim map As BindingMap = doc.ParameterBindings
map.TruelyClear()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Sub TruelyClear(ByVal map As BindingMap)
Dim it As DefinitionBindingMapIterator = map.ForwardIterator()
it.Reset()
While it.MoveNext()
map.Remove(it.Key)
End While
End Sub
The following code can test the method:
…
RawTruelyEraseAllProjectParameters(CachedDoc)
…
And it works well.
ProjectParameter Eraser of Revit Addin Coder can generate the code in either VB.NET or C# automatically in no time.
Posted by: |