It was demonstrated how to Erase Project Parameter quite a while back. In this post, we’d like to revisit the topic a bit, clarify something and improve something.
As Don pointed out, the TrulyClear extension to the BindingMap can only remove custom (shared) project parameters. Though it’s not explicitly stated in the article, the point could be impliedly reached out since the native (real) project parameters have nothing to do with the BindingMap (Parameter Bindings). We’d like to make it clear here anyway.
Another matter is that with some more testing work done, it’s found that the version of the TruelyClear extension method introduced there is not reliable enough.
It seems removing a key directly in the map iteration loop would interrupt something sometimes. That explains why the code works fine sometimes but not some other especially when debugging. As we know, it is a good practice to always collect objects to be deleted into a separate list and remove them outside of the loop for object map, iterator, set or collection.
Here is the new and better version of the TruelyClear extension method:
public static void TruelyClear(this BindingMap map)
{
DefinitionBindingMapIterator it = map.ForwardIterator();
it.Reset();
List<Definition> defList = new List<Definition>();
while (it.MoveNext())
{
defList.Add(it.Key);
}
foreach (Definition def in defList)
{
map.Remove(def);
}
}
The same test method and code have also been appended here for convenience:
public static void RawTruelyEraseAllProjectParameters(RvtDocument doc)
{
BindingMap map = doc.ParameterBindings;
map.TruelyClear();
}
…
RawTruelyEraseAllProjectParameters(CachedDoc);
…
Newer version of RevitAddinWizard has reflected these in both its user interfaces and the auto-generated code.
Recent Comments