VB.Net - Selecting only Polyline layers from the TOC

569
3
Jump to solution
01-23-2018 01:58 PM
JeffreyFrye
New Contributor II

I can successfully get all layers in the TOC into a combo box, but I only want to get layers that are of a polyline geometry in the TOC. My apologies, I do not know the proper method for inserting code here.

Protected Overrides Sub OnClick()
        Dim settingsForm As New Settings
        Dim myEnumLayers As ESRI.ArcGIS.Carto.IEnumLayer
        Dim fLayers As ESRI.ArcGIS.Carto.ILayer
        Try
            myEnumLayers = My.ArcMap.Document.FocusMap.Layers
            fLayers = myEnumLayers.Next
            Do Until fLayers Is Nothing
                If fLayers.Valid Then
                    If TypeOf fLayers Is ESRI.ArcGIS.Carto.IFeatureLayer Then
                        settingsForm.ComboBox1.Items.Add(fLayers.Name)
                    End If
                    fLayers = myEnumLayers.Next
                End If
            Loop
        Catch ex As Exception
        End Try
        settingsForm.Show()
  End Sub
0 Kudos
1 Solution

Accepted Solutions
JeffreyFrye
New Contributor II

Thank you Mody - this got me going in the right direction. Instead of using ILayer, I used IFeatureLayer. I was then able to check each layer to identify IFeatureClass(es), which allowed me to access the geometry and return only polyline types. This class is designed to iterate through the current mxd layers, and populate a combobox with polyline layers only, via an add-in button.

Public Class SelectRoadLayer
  Inherits ESRI.ArcGIS.Desktop.AddIns.Button
  Public Sub New()
  End Sub
  Protected Overrides Sub OnClick()
        Dim settingsForm As New Settings
        Dim myEnumLayers As ESRI.ArcGIS.Carto.IEnumLayer
        Dim fLayers2 As ESRI.ArcGIS.Carto.IFeatureLayer
        Dim fClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
        Try
            myEnumLayers = My.ArcMap.Document.FocusMap.Layers
            fLayers2 = myEnumLayers.Next
            Do Until fLayers2 Is Nothing
                If fLayers2.Valid Then
                    fClass = fLayers2.FeatureClass
                    If fClass.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline Then
                        settingsForm.ComboBox1.Items.Add(fLayers2.Name)
                    End If
                    fLayers2 = myEnumLayers.Next
                End If
            Loop
        Catch ex As Exception
        End Try
        settingsForm.Show()
  End Sub
  Protected Overrides Sub OnUpdate()
    Enabled = My.ArcMap.Application IsNot Nothing
  End Sub
End Class

View solution in original post

0 Kudos
3 Replies
ModyBuchbinder
Esri Regular Contributor

Try if ( fLayers.FeatureClass.ShapeType = esriGeometryType.esriGeometryPolyline)

I am not sure about VB syntex but this is the way.

You can also give parameter to the My.ArcMap.Document.FocusMap.Layers to get only featurelayers.

Have fun

Mody

0 Kudos
JeffreyFrye
New Contributor II

Thank you Mody - this got me going in the right direction. Instead of using ILayer, I used IFeatureLayer. I was then able to check each layer to identify IFeatureClass(es), which allowed me to access the geometry and return only polyline types. This class is designed to iterate through the current mxd layers, and populate a combobox with polyline layers only, via an add-in button.

Public Class SelectRoadLayer
  Inherits ESRI.ArcGIS.Desktop.AddIns.Button
  Public Sub New()
  End Sub
  Protected Overrides Sub OnClick()
        Dim settingsForm As New Settings
        Dim myEnumLayers As ESRI.ArcGIS.Carto.IEnumLayer
        Dim fLayers2 As ESRI.ArcGIS.Carto.IFeatureLayer
        Dim fClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
        Try
            myEnumLayers = My.ArcMap.Document.FocusMap.Layers
            fLayers2 = myEnumLayers.Next
            Do Until fLayers2 Is Nothing
                If fLayers2.Valid Then
                    fClass = fLayers2.FeatureClass
                    If fClass.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline Then
                        settingsForm.ComboBox1.Items.Add(fLayers2.Name)
                    End If
                    fLayers2 = myEnumLayers.Next
                End If
            Loop
        Catch ex As Exception
        End Try
        settingsForm.Show()
  End Sub
  Protected Overrides Sub OnUpdate()
    Enabled = My.ArcMap.Application IsNot Nothing
  End Sub
End Class
0 Kudos
KenBuja
MVP Esteemed Contributor

One thing to be aware of. When you're cycling through the layers of your map, you may have other layers in there that are not feature layers. That would crash your code. You should use this code to make sure it is.

Dim pLayer As ESRI.ArcGIS.Carto.ILayer2
myEnumLayers = My.ArcMap.Document.FocusMap.Layers
pLayer = pEnumLayers.Next
Do Until pLayer Is Nothing
  If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then
    pFLayer = New ESRI.ArcGIS.Carto.FeatureLayer
    pFLayer = pLayer
   ' your additional code
  End If
  pLayer = pEnumLayers.Next
Loop
0 Kudos