Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a problem with an editor extension in an Add-In for ArcGIS 10. The editor extension listens on different events occuring on shapefile layers via registering listeners:

mEditEvents.OnChangeFeature += OnChangeFeature;
mEditEvents.OnCreateFeature += OnCreateFeature;
mEditEvents.OnDeleteFeature += OnDeleteFeature;

All these callbacks get are a parameter of type IObject, which can be cast to IFeature. With this IFeature I can get the feature class and its alias name (IFeature.Class.AliasName) which seems to be the name of the shapefile associated with this feature, but I want the name of the layer associated with this feature which can be different from the shapefile's name.

Is there any way how to get this layer name?

Thanks in advance!

Update:

I found a solution (see my comment below) - thanks for the help!

share|improve this question
    
Can you assume all edits are occuring on the current editlayer IEditLayers.CurrentLayer? Or do you have other code somewhere that edits/updates features in other layers (these will fire events too)? –  Kirk Kuykendall Feb 12 '13 at 15:19
    
I already tried this, but IEditLayers.CurrentLayer is null if you (1) start an edit session, (2) select a feature and (3) move it somewhere. But this event should also be related to the correct layer it will operate on. –  geoSuleiman Feb 12 '13 at 15:31
    
Do you need to support the situation where a particular featureclass is referenced by multiple featurelayers? –  Kirk Kuykendall Feb 12 '13 at 16:10
    
No, I just have 5 layers having different names than the shapefiles being the source of these layers. When an edit event arrives, I only have an object of type IObject which can be cast to IFeature. Then I can obtain the FeatureClass and its AliasName, but not the name of the layer this object belongs to. –  geoSuleiman Feb 12 '13 at 16:16
1  
Not a 100% sure on this, but if edit is occuring in ArcMap you can loop through the loaded layers, get the layer name from ILayer.Name then query to IFeatureLayer>IFeatureClass.AliasName to check against. –  artwork21 Feb 12 '13 at 16:35
show 2 more comments

3 Answers 3

up vote 1 down vote accepted

I finally found a simple solution: Before starting the edit session I store the alias names for each of the layers (I can identify the layers by their names).

With this mapping I now can relate any single edit event to the right layer because I have the AliasName of the feature's FeatureClass.

share|improve this answer
    
layer name and featureclass' aliasname are not necessarily same..a user can simply rename a layer in TOC –  vinayan Feb 13 '13 at 3:07
    
I know - that's the source of the problem / my question! And that's why I keep a mapping from aliasname to name (as I know every shapefile is only loaded once this is correct). –  geoSuleiman Feb 13 '13 at 7:43
add comment

IFeature:Table

[IDataset:Name

You can use the IFeature:Table property. Table implements IDataset so you can get the name from the IDataset:Name property. Here is an example:

Private Function GetPathFromFeature(ByVal pfeature As IFeature) As System.String

If pfeature Is Nothing Then Return Nothing

Dim ptable As ITable = CType(pfeature.Table, ITable)

If ptable Is Nothing OrElse Not (TypeOf ptable Is IDataset) Then
    Return Nothing
End If

Dim dataset As IDataset = CType(ptable, IDataset) ' Explicit Cast

Return (dataset.Workspace.PathName & "\" & dataset.Name)

End Function

share|improve this answer
    
This gives the same result as the featureclass' aliasname - not the name of the layer the feature belongs to (when asking my question I hadn't thought of the fact that a shapefile can be loaded multiple times and this is likely the reason why you cannot get the layer's name from a feature(class) as there could be more than one layer associated with this featureclass). –  geoSuleiman Feb 13 '13 at 7:45
add comment

You may need to get the dataset's name from the feature first. Then you can loop all the layers in TOC and check if their dataset name matches the one obtained earlier. It would look something like below.

IFeature feature = //get the feature..;
IDataset featureDs = (IDataset)feature.Class;
IMap map = //get the map...;

for (int i = 0; i < map.LayerCount; i++)
{
    ILayer layer = map.get_Layer(i);
    if (layer is IFeatureLayer)
    {
        IFeatureClass layerFc = ((IFeatureLayer)layer).FeatureClass;
        IDataset layerDs = (IDataset)layerFc;

        if (layerDs.Name == featureDs.Name)
        {
            //print the layer name in TOC..
            System.Diagnostics.Debug.Print(layer.Name);
        }
    }
}  
share|improve this answer
1  
Need to recurse into grouplayers. Also need to check to make sure layerFc is not null, or why not just check to see if layerFc == feature.Class (reference equivalency)? –  Kirk Kuykendall Feb 13 '13 at 14:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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