public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Construct a parameter filter to get only
// unnamed reference planes, i.e. reference
// planes whose name equals the empty string:
BuiltInParameter bip
= BuiltInParameter.DATUM_TEXT;
ParameterValueProvider provider
= new ParameterValueProvider(
new ElementId( bip ) );
FilterStringRuleEvaluator evaluator
= new FilterStringEquals();
FilterStringRule rule = new FilterStringRule(
provider, evaluator, "", false );
ElementParameterFilter filter
= new ElementParameterFilter( rule );
FilteredElementCollector col
= new FilteredElementCollector( doc )
.OfClass( typeof( ReferencePlane ) )
.WherePasses( filter );
int n = 0;
int nDeleted = 0;
// No need to cast ... this is pretty nifty,
// I find ... grab the elements as ReferencePlane
// instances, since the filter guarantees that
// only ReferencePlane instances are selected.
// In Revit 2014, this attempt to delete the
// reference planes while iterating over the
// filtered element collector throws an exception:
// Autodesk.Revit.Exceptions.InvalidOperationException:
// HResult=-2146233088
// Message=The iterator cannot proceed due to
// changes made to the Element table in Revit's
// database (typically, This can be the result
// of an Element deletion).
//
//foreach( ReferencePlane rp in col )
//{
// ++n;
// nDeleted += DeleteIfNotHosting( rp ) ? 1 : 0;
//}
ICollection<ElementId> ids = col.ToElementIds();
n = ids.Count();
if( 0 < n )
{
using( Transaction tx = new Transaction( doc ) )
{
tx.Start( string.Format(
"Delete {0} ReferencePlane{1}",
n, Util.PluralSuffix( n ) ) );
// This also causes the exception "One or more of
// the elementIds cannot be deleted. Parameter
// name: elementIds
//
//ICollection<ElementId> ids2 = doc.Delete(
// ids );
//nDeleted = ids2.Count();
List<ElementId> ids2 = new List<ElementId>(
ids );
foreach( ElementId id in ids2 )
{
try
{
ICollection<ElementId> ids3 = doc.Delete(
id );
nDeleted += ids3.Count;
}
catch( Autodesk.Revit.Exceptions.ArgumentException )
{
}
}
tx.Commit();
}
}
Util.InfoMsg( string.Format(
"{0} unnamed reference plane{1} examined, "
+ "{2} element{3} in total were deleted.",
n, Util.PluralSuffix( n ),
nDeleted, Util.PluralSuffix( nDeleted ) ) );
return Result.Succeeded;
}