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.

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
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).

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.