3

I have a IEnumFeature list of selected Features obtained through IApplication.Document.ActiveView.Selection.

From this selection I want to delete some feature, more specifically the features from one layer. Iteration through the IEnumFeature list is no problem but i cant figure out how to get the layername of each IFeature.

How can I obtain the layername from a IFeature?

I know how to get the ITable object from each IFeature but there seems to be no property that holds the name of the table, wich would suffice for my purpose. Because is know witch layer is created from wich table.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 24, 2018 at 8:33

3 Answers 3

3

You can't go from a feature to a featureLayer, because the same feature can be part of multiple featureLayers (ex: add the same layer multiple times with different symbology, filtering etc).

What you can do is to go from the feature to the featureClass to the Dataset. If you want, you can then compare the dataset name with the layers dataset and at last extract the layer name.

To get the dataset name from an IFeature:

CType(myFeature.Class, IDataset).Name
answered Sep 24, 2018 at 12:43
0
2

You can get the table name by: ((IDataset) table).Name, but if you are using this to rifle through the layers and match based on dataset, then you also need to make sure the IWorkspace match (otherwise you may actually delete a feature from a featureclass of the same name that if it has that same OBJECTID).

answered Sep 24, 2018 at 12:43
2

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.

answered Sep 26, 2018 at 3:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.