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.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
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 static bool RawEraseProjectParameter(RvtDocument doc, string name, ParameterType type)
{
BindingMap map = doc.ParameterBindings;
DefinitionBindingMapIterator it = map.ForwardIterator();
it.Reset();
Definition def = null;
while (it.MoveNext())
{
if (it.Key != null && it.Key.Name == name && type == it.Key.ParameterType)
{
def = it.Key;
break;
}
}
if (def != null)
{
map.Remove(def);
return true;
}
return false;
}
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 static void RawEraseAllProjectParameters(RvtDocument doc)
{
BindingMap map = doc.ParameterBindings;
map.Clear();
}
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 static void RawTruelyEraseAllProjectParameters(RvtDocument doc)
{
BindingMap map = doc.ParameterBindings;
map.TruelyClear();
}
public static void TruelyClear(this BindingMap map)
{
DefinitionBindingMapIterator it = map.ForwardIterator();
it.Reset();
while (it.MoveNext())
{
map.Remove(it.Key);
}
}
The following code can test the method:
…
RawTruelyEraseAllProjectParameters(CachedDoc);
…
And it works well.
ProjectParameter Eraser of RevitAddinWizard can generate the code automatically in no time.
Hi,
The above code removed shared parameters however the other project parameters were not removed. Is there a way to do this?
Thanks,
Dan
Posted by: Dan Tartaglia | 12/13/2011 at 08:07 PM
Good catch! You are right the code only erases Shared Project Parameters. In terms of erasing Real/Native Project Parameters, it is still a research topic.
When testing the TruelyClear extension method further, I found that the following is more reliable though a bit longer:
public static void TruelyClear(this BindingMap map)
{
DefinitionBindingMapIterator it = map.ForwardIterator();
it.Reset();
List defList = new List();
while (it.MoveNext())
{
defList.Add(it.Key);
}
foreach (Definition def in defList)
{
map.Remove(def);
}
}
Posted by: Spiderinnet | 12/13/2011 at 10:56 PM
Yep, I always add objects to be deleted to a ArrayList. I submitted this question to ADN Support. The Model Stripper utility claims to be able to do this. I'm going to test it now to verify (30 day eval).
Thanks,
Dan
Posted by: Dan Tartaglia | 12/14/2011 at 10:43 AM
It seems removing a key directly in the map iterator loop would interrupt something sometimes. That explains why it works sometimes but not some other times especially when debugging. Yes, it is a good practice to always collect those elements to be deleted into a separate list and remove them outside of the map/iterator/set/collection loop.
In terms of erasing native project parameters, there must exist some dirty workarounds like iterating through all elements in the model, inspecting parameter ones and removing those project parameter elements from the model. Not sure if doing so will bring up any side effects though and how the Model Stripper handles it. Let us see.
Posted by: Spiderinnet | 12/14/2011 at 03:43 PM