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.
i'm having trouble with the eraseallprojectparameters method. it seems to fall over at While iterator.MoveNext() on the second pass when I know there are plenty of parameters to be removed.
any ideas what could be causing this?
Posted by: Russ Green | 12/27/2012 at 05:55 PM
It might be Revit model specific. I would add Try/Catch around the iterator.MoveNext() call and check the Exception information in detail to see what's really going wrong. Another idea is to turn the iterator into a List and access to each item through foreach or an index number. Adding Try/Catch around the item access code will still be helpful in this case for both continuing to run and diagnosing problems.
In terms of how to turn an iterator into a List, a few old posts have been addressed it in both C# and VB.NET.
Posted by: Spiderinnet | 12/27/2012 at 09:45 PM