Select to view content in your preferred language

Layer does not get selected when in a group

929
5
06-21-2010 10:00 AM
JoseSanchez
Frequent Contributor
Hi all

Whenever I modify a layer and the layer is in a group the following function FindLayer(ByRef pMap As ESRI.ArcGIS.Carto.IMap) does not find it.

Please advice

  'finish edit operation
            m_pEditor.StopOperation(("Merge Network Features"))

            ErrCode = 2
            'refresh features
            Dim pRefresh As ESRI.ArcGIS.Geodatabase.IInvalidArea
            pRefresh = New ESRI.ArcGIS.Carto.InvalidArea
            pRefresh.Display = m_pEditor.Display
            pRefresh.Add(pNewFeature)
            pRefresh.Invalidate(-2)

'select new feature
            Dim pMap As ESRI.ArcGIS.Carto.IMap
            pMap = m_pEditor.Map

            pMap.ClearSelection()
            pMap.SelectFeature(FindLayer(pMap), pNewFeature)   #######DOES NOT WORK
             Me.Close()

            '  Exit Sub
          

        Catch ex As Exception
            Debug.Print(ex.Message)
            MsgBox("Error Message: " & ex.Message)

        End Try
    End Sub



Public Function FindLayer(ByRef pMap As ESRI.ArcGIS.Carto.IMap) As ESRI.ArcGIS.Carto.ILayer
        'helper function to find a layer for a feature class
        Dim i As Integer
        Dim pLayer As ESRI.ArcGIS.Carto.ILayer
        Dim pFeatLayer As ESRI.ArcGIS.Carto.IFeatureLayer
        Try
            For i = 0 To pMap.LayerCount - 1
                pLayer = pMap.Layer(i)

                Debug.WriteLine(player.Name)

                'UPGRADE_WARNING: TypeOf has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
                If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then
                    pFeatLayer = pLayer
                    'Check if the layer is valid, ie: it's data source is valid....
                    'UPGRADE_WARNING: Couldn't resolve default property of object pFeatLayer.Valid. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    If pFeatLayer.Valid Then
                        'UPGRADE_WARNING: Couldn't resolve default property of object m_pFC.ObjectClassID. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        'UPGRADE_WARNING: Couldn't resolve default property of object pFeatLayer.FeatureClass.ObjectClassID. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        If pFeatLayer.FeatureClass.ObjectClassID = m_pFC.ObjectClassID Then
                            pLayer = pFeatLayer
                            FindLayer = pLayer
                        End If
                    End If
                End If
            Next i
        Catch ex As Exception
            Debug.Print(ex.Message)
            MsgBox("Error Message: " & ex.Message)
        End Try
    End Function
0 Kudos
5 Replies
JeffMatson
Frequent Contributor
Have you already tried using IEnumLayer inside your FindLayer function?


Dim enumLayer as IEnumLayer = pMap.Layers
Dim layer as ILayer = enumLayer.Next

Do Until layer Is Nothing
  If TypeOf layer is IFeatureLayer Then
    '...
  layer = enumLayer.Next
Loop
0 Kudos
RuchiraWelikala
Regular Contributor
use an elseif statement after your original if statement (the one that checks if pLayer is a ifeaturelayer) and check if pLayer is a ICompositeLayer.  if it is a composite layer then do another for loop and loop through the Composite layer and find the layer you're looking for.
0 Kudos
AlexanderGray
Honored Contributor
There is an ESRI snippet for this called Loop through layers of specific uid.
Basically uses the IMap.layers with the UID of the Ifeaturelayer (see snippet or help for the IMap.layers method.)  Also the second argument is to search recursively, setting it to true will search layers inside group layers.)
0 Kudos
AlexanderGray
Honored Contributor
One last thing I forgot to mention, if the map has no layers, calling IMap.Layers returns an exception so make sure you check that the layer count is not zero first.
0 Kudos
JeffMatson
Frequent Contributor
use an elseif statement after your original if statement (the one that checks if pLayer is a ifeaturelayer) and check if pLayer is a ICompositeLayer. if it is a composite layer then do another for loop and loop through the Composite layer and find the layer you're looking for.



Yes if you are searching by layer index as the original poster's code is, this would be necessary.

By using an IEnumLayer object set to IMap.Layers (with no additional arguments) you get a recursive search of all layers that includes all layer types, by default.
0 Kudos