How to find a feature programmatically while editing


This sample code searches for a feature by looping through all feature layers in the focus map. The sample uses a UITool control to provide the MouseDown event and search point location. The search point is buffered using IEditor::CreateSearchShape to increase the search tolerance and the likelihood of finding a feature.
Because this sample relies on the CreateSearchShape method, an active edit session must be present. To see an implementation that does not require an edit session, see the sample "Finding a Feature Programmatically" under ArcMap.

How to use

  1. Add a custom UIToolControl onto the Editor toolbar and make sure the names of the control match the code. This sample assumes the control is called UIToolControl1.
  2. Paste in this routine for the MouseDown event.
  3. Select the tool and then click on a feature.
[VBA]
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, _
                                     ByVal x As Long, ByVal y As Long)
    Dim pEditLayers As IEditLayers
    Dim pEditor As IEditor
    Dim pEnumLayer As IEnumLayer
    Dim pEnv As IEnvelope
    Dim pFeature As IFeature
    Dim pFeatureClass As IFeatureClass
    Dim pFeatureLayer As IFeatureLayer
    Dim pFCursor As IFeatureCursor
    Dim pGeometry As IGeometry
    Dim pMap As IMap
    Dim pPoint As IPoint
    Dim pSpatialFilter As ISpatialFilter
    Dim pUID As New UID
    Dim strShapeFieldName As String
    
    'Get a handle to the Editor extension
    pUID = "esriEditor.Editor"
    Set pEditor = Application.FindExtensionByCLSID(pUID)
    If pEditor Is Nothing Then Exit Sub
    'Make sure an edit session is in progress
    If Not pEditor.EditState = esriStateEditing Then Exit Sub
    Set pEditLayers = pEditor 'QI
    Set pMap = pEditor.Map
    
    'Convert the incoming x,y coordinates to map units
    Set pPoint = pEditor.Display.DisplayTransformation.ToMapPoint(x, y)
    
    'Pass point to CreateSearchShape which creates a geometry around the point
    'The larger geometry is an envelope and will give us better search results
    'The click therefore doesn't have to be exactly on the feature
    Set pGeometry = pEditor.CreateSearchShape(pPoint)
    Set pEnv = pGeometry ''QI
    
    'Create a new spatial filter and use the new envelope as the geometry
    Set pSpatialFilter = New SpatialFilter
    Set pSpatialFilter.Geometry = pGeometry
    strShapeFieldName = pEditLayers.CurrentLayer.FeatureClass.ShapeFieldName
    Set pSpatialFilter.OutputSpatialReference(strShapeFieldName) = pMap.SpatialReference
    pSpatialFilter.GeometryField = pEditLayers.CurrentLayer.FeatureClass.ShapeFieldName
    pSpatialFilter.SpatialRel = esriSpatialRelIntersects
    
    'Search for intersecting features in each feature layer in the focus map
    pUID = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" ''GUID for IGeoFeatureLayer
    Set pEnumLayer = pMap.Layers(pUID, True)
    pEnumLayer.Reset
    Set pFeatureLayer = pEnumLayer.Next
    Do While Not pFeatureLayer Is Nothing
        Set pFeatureClass = pFeatureLayer.FeatureClass
        Set pFCursor = pFeatureClass.Search(pSpatialFilter, False)
        Set pFeature = pFCursor.NextFeature
        If Not pFeature Is Nothing Then
            'Do something with the feature
            MsgBox pFeature.OID
        End If
        Set pFeatureLayer = pEnumLayer.Next
    Loop
    
End Sub






Additional Requirements
  • An Edit Session.