If you know the layer (ILayer
) that you want to delete the selected features from, perhaps instead of using IApplication.Document.ActiveView.Selection
as ISelection and filtering through the returned enumerated features, you could get your feature selection (IFeatureSelection) directly from the layer. Although there are some differences between using IEnumFeature and IFeatureCursor the end result is the same, try:
void DeleteSelectedFeatures(ILayer TargetLayer)
{
if (TargetLayer.Valid)
{
// the layer is correctly referenced
if (TargetLayer is IFeatureLayer)
{
// target layer is a Feature Layer
IFeatureLayer pFeatLayer = (IFeatureLayer)TargetLayer;
IFeatureSelection pFeatSel = (IFeatureSelection)pFeatLayer;
if (pFeatSel.SelectionSet.Count > 0)
{
// there is something in this layer that is selected
// using ISelectionSet2 as it has update where pFeatSel.SelectionSet does not
ISelectionSet2 pSelSet = (ISelectionSet2)pFeatSel.SelectionSet;
IFeatureCursor pFeatCur;
pSelSet.Update(null,false,pFeatCur); // using an update cursor because we're going to modify them
IFeature pFeature;
// loop through the features, removing as we go
while ((pFeature = pFeatCur.NextFeature()) != null)
{
pFeatCur.DeleteFeature(); // delete the feature at this row
}
}
}
}
}
and see if it's any improvement on your existing code.