How to select by location in 3D


This example demonstrates the use of ArcObjects to select all features in the current ArcScene 3D scene which are contained by a designated geometry. In this case, the input geometry is the first feature found in the first feature layer in the scene.
This code approximates what can be achieved through the 'Select by Location' dialog in ArcScene. However, this method will include the designated input feature in the selection.

How to use

  1. Paste the code into an ArcScene VBA session and call the macro 'SampleSelectByLocation3D'.
[VBA]
Public Sub SampleSelectByLocation3D()
    
    Dim pFC As IFeatureCursor ' feature cursor to read input geometry
    Dim pFLayer As IFeatureLayer ' input feature layer
    Dim pSXDoc As ISxDocument ' the current document
    Dim pScene As IScene ' the current scene
    
    ' Get the active scene:
    Set pSXDoc = Application.Document
    Set pScene = pSXDoc.Scene
    
    ' ensure there is an input feature layer:
    If TypeOf pScene.Layer(0) Is IFeatureLayer Then
        Set pFLayer = pScene.Layer(0)
    Else
        MsgBox "Please add or move a feature layer to the top of the table of contents."
        Exit Sub
    End If
    
    ' get a feature cursor from the input layer:
    Set pFC = pFLayer.Search(Nothing, True)
    
    ' get the input feature; just the first one:
    Dim pFeat As IFeature
    Set pFeat = pFC.NextFeature
    
    ' exit if no feature was found:
    If pFeat Is Nothing Then Exit Sub
    
    ' get the actual input geometry to select with:
    Dim pGeom As IGeometry
    Set pGeom = pFeat.Shape
    
    ' the SelectByShape method takes a boolean to report if only one feature
    ' was selected:
    Dim bJustOne As Boolean
    pScene.SelectByShape pGeom, Nothing, bJustOne
    
    ' report the current selection in the scene to the immediate window:
    Debug.Print pScene.SelectionCount
    
End Sub