Get layer of selected object

1589
5
12-01-2011 06:25 PM
BrianWestfall
New Contributor
I need to get the layer of a single selcted object.  Using the selection tool, if a user selects a polygon, I want to determine programatically (vb.net) what layer that object is on.

Thanks

Brian
0 Kudos
5 Replies
NeilClemmons
Regular Contributor III
You can get the feature from the map's feature selection.  The feature has a Class property that will tell you the feature class it comes from.  You can then loop through all of the layers in the map until you find one that uses that feature class as a datasource.  This is not foolproof as there can be any number of layers in the map that use the same feature class as a datasource.  If this is the case then you will not be able to tell which layer the feature comes from.
0 Kudos
AlexanderGray
Occasional Contributor III
You can also loop through all the layers in the map.  You can cast the each layer to IFeatureSelection if the layer implements that interface.  You can then check each layer's selectionset for selected features.  To my knowledge this is the only way to make sure you have the right layer since, as Neil mentioned, the same featureclass can be the source for more than one layer in the map and the map's featureselection doesn't link back to the layers, only the featureclasses.
0 Kudos
BrianWestfall
New Contributor
Alexander and Neil,

Thanks you both for the suggestions.  The following code list the layers in a list box of any selected features.  It seems to work but do you see any problems with this approach.

Brian

Public Sub GetLayer()

    Dim pMxDoc As IMxDocument
    pMxDoc = CType(My.ArcMap.Application.Document, IMxDocument)
    Dim pMap As IMap
    pMap = pMxDoc.FocusMap
    Dim pEnumLayer As IEnumLayer
    pEnumLayer = pMap.Layers
    pEnumLayer.Reset()
    Dim pLayer As ILayer
    pLayer = pEnumLayer.Next
    Dim pFLayer As IFeatureLayer = Nothing
    Do While Not pLayer Is Nothing

      If TypeOf pLayer Is IFeatureLayer Then
        pFLayer = TryCast(pLayer, IFeatureLayer)
        Dim sel As IFeatureSelection
        sel = TryCast(pFLayer, IFeatureSelection)
        If sel.SelectionSet.Count > 0 Then
          ListBox1.Items.Add(pLayer.Name) ' this adds the layer name to a list box
         End If
      End If
      pLayer = pEnumLayer.Next

    Loop


  End Sub
0 Kudos
NeilClemmons
Regular Contributor III
The code lists any layers that have a feature selection.  It doesn't do anything to try and determine if the feature comes from one or more of them.  You could narrow down the list by checking the feature class of each layer to see if it is the same feature class the selected feature comes from.  You could narrow it down even further by checking the selection set of each layer you have left to see if it contains a feature with the same ObjectId as the selected feature.  If you find two or more layers with the same ObjectId in their selection set then I don't think you can do anything else to determine which one that feature actually belongs to (well, short of unselecting that feature and looking to see which selection set changed).
0 Kudos
BrianWestfall
New Contributor
Good point.  Something else to figure out how to do.  I appreciate your suggestions.

Brian
0 Kudos