2

Currently I am using ArcObjects (10.2.1) in vb.net (vs 2012). What I intent to develop is a spatial query tool. There are 2 steps in the function:

(1) Use "Select Features" tool in ArcMap to select features in a certain area.

(2) List the several fields of features selected of a layer.

My question is how to get the field values only for a desired feature layer. My code is listed below. It only works when only the desired layer is enabled on ArcMap.

    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pEnumFeature As IEnumFeature
    Dim pEnumFeatureSetup As IEnumFeatureSetup
    Dim pFeature As IFeature

    pMxDoc = My.ArcMap.Document
    pMap = pMxDoc.FocusMap
    pEnumFeature = pMap.FeatureSelection
    pEnumFeatureSetup = pEnumFeature
    pEnumFeatureSetup.AllFields = True
    pFeature = pEnumFeature.Next

    Dim layerNum = GetIndexNumberFromLayerName(pActiveView, "testdb.DBO.testTable")
    pFeatSel = pMap.Layer(layerNum)

    Dim tempArr As New List(Of String)
    Do While (Not pFeature Is Nothing)
        tempArr.Add(pFeature.Value(pFeature.Fields.FindField("Item_ID")).ToString)
        pFeature = pEnumFeature.Next
    Loop

    Dim message = String.Join(Environment.NewLine, tempArr.ToArray())
    Dim lstOfString As List(Of String) = New List(Of String)(tempArr)
    Dim selectedCount = lstOfString.Count

    End If


Public Function GetIndexNumberFromLayerName(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal layerName As System.String) As System.Int32

    If activeView Is Nothing OrElse layerName Is Nothing Then
        Return -1
    End If

    Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
    Dim numberOfLayers As System.Int32 = map.LayerCount
    Dim i As System.Int32 = 0
    Do While i < numberOfLayers
        If layerName = map.Layer(i).Name Then
            Return i
        End If
        i += 1
    Loop
    Return -1
    LayerNumber = i
End Function
4

I think you are making this more difficult than it has to be. You are obtaining your selected features from IMap.FeatureSelection. So multiple layers could return a selection.

A much better way is to get a handle on your ILayer, cast that into an IFeatureLayer then point an IFeatureSelection interface to IFeatureLayer. From this you can get an ISelectionSet. This has a Search method which returns a cursor over the selection in that specific layer.

Here is a bit of sample code:

Dim pFeatureSelection As IFeatureSelection
pFeatureSelection = pFeatureLayer

Dim pSelectionSet As ISelectionSet
pSelectionSet = pFeatureSelection.SelectionSet

Dim pFeatureCursor As IFeatureCursor = Nothing
pSelectionSet.Search(Nothing, True, pFeatureCursor)
  • Hi Hornbydd, thanks a lot for the quick advice. Will try to follow the workflow you suggested. – ppLily Aug 5 '14 at 9:30
  • Hi Hornbydd, your sample code helps me to select features in the desired layer successfully, but I failed to get the field value of the selected feature, I put my updated code below. To make the code clear, I put it as an answer. Appreciate if you can have a look, many tanks. – ppLily Aug 8 '14 at 2:39
1

Here is the fully working code:

    pMxDoc = My.ArcMap.Application.Document
    pMap = pMxDoc.FocusMap
    pActiveView = pMxDoc.FocusMap

    Dim layerNum = GetIndexNumberFromLayerName(pActiveView, "testdb.DBO.testTable")
    Dim pFeatureLayer As IFeatureLayer
    pFeatureLayer = pMap.Layer(layerNum)

    Dim pFeatureSelection As IFeatureSelection
    pFeatureSelection = pFeatureLayer

    Dim pSelectionSet As ISelectionSet
    pSelectionSet = pFeatureSelection.SelectionSet

    Dim pFeatureCursor As IFeatureCursor = Nothing
    pSelectionSet.Search(Nothing, True, pFeatureCursor)

    Dim tempArr As New List(Of String)

    Dim pFeature As IFeature
    pFeature = pFeatureCursor.NextFeature
    Do While (Not pFeature Is Nothing)
        tempArr.Add(pFeature.Value(pFeature.Fields.FindField("Item_ID")).ToString)
        pFeature = pFeatureCursor.NextFeature
    Loop
  • 1
    I have edited your reply to show you how the code should be. – Hornbydd Aug 8 '14 at 14:01
  • Hi Hornbydd, thanks you very much for your help. But the map still crash when executing the line pFeature = pFeatureCursor.Next before looping. – ppLily Aug 11 '14 at 1:52
  • Hi Hornbydd, it works after I modified the line pFeature = pFeatureCursor.Next to pFeature = pFeatureCursor.NextFeature, thank you very much for the help. – ppLily Aug 11 '14 at 2:53
  • Hi Andre Silva, I just use the code in above except change pFeatureCursor.Next to pFeature = pFeatureCursor.NextFeature – ppLily Jan 6 '15 at 2:01
  • Yes I change both lines. – ppLily Jan 7 '15 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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