[Visual Basic 6.0]Below are three different methods for looping through a map's selection. The first script uses IMap::FeatureSelection. The problem with this approach is that there is no guarantee all of the fields for a particular feature class are returned in the query. By default only the Shapefield is asked for as this property is primarily used to draw the map's selected features only, not access their attributes. However, this approach may be used when editing; in this case, all the fields are requested.
The second sample is the same as the first except it additionally uses IEnumFeatureSetup to explicitly tell the map to retrieve all fields.
The last sample is the best approach. It is rarely the case that one would want to loop through the entire selection in such a manner as the first two samples. The exception is of course when drawing the selected features or determining the total selection count. Instead, it is much more practical to loop through the selections belonging only to the layers of interest. For example, in a case where only selected polygons are of interest, it is much more efficient to only check the geometry type of each feature class rather than each feature returned by IMap::FeatureSelection.
Public Sub LoopThruSelection() Dim pDoc As IMxDocument Dim pMap As IMap Dim pEnumFeat As IEnumFeature Dim pFeat As IFeature Set pDoc = ThisDocument Set pMap = pDoc.FocusMap Set pEnumFeat = pMap.FeatureSelection Set pFeat = pEnumFeat.Next Do While (Not pFeat Is Nothing) Debug.Print pFeat.Value(pFeat.Fields.FindField("Name")) Set pFeat = pEnumFeat.Next Loop End Sub Public Sub LoopThruSelection2() Dim pMxDoc As IMxDocument Dim pMap As IMap Dim pEnumFeature As IEnumFeature Dim pEnumFeatureSetup As IEnumFeatureSetup Dim pFeature As IFeature Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap Set pEnumFeature = pMap.FeatureSelection Set pEnumFeatureSetup = pEnumFeature 'QI pEnumFeatureSetup.AllFields = True Set pFeature = pEnumFeature.Next Do While (Not pFeature Is Nothing) Debug.Print pFeature.Value(pFeature.Fields.FindField("Name")) Set pFeature = pEnumFeature.Next Loop End Sub Public Sub QuerySelectedFeatures() Dim pMxDoc As IMxDocument Dim pEnumLayer As IEnumLayer Dim pFeature As IFeature Dim pFeatureCursor As IFeatureCursor Dim pFeatureLayer As IFeatureLayer Dim pFeatureSelection As IFeatureSelection Dim pMap As IMap Dim pSelectionSet As ISelectionSet Dim pUID As IUID'the UID specifies the interface identifier (GUID)
'that represents the type of layer you want returned.
'in this case we want an EnumLayer containing all the FeatureLayer objects Set pUID = New UID pUID = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" ' Identifies FeatureLayer objects Set pMxDoc = Application.Document Set pMap = pMxDoc.FocusMap 'Loop through all feature layers in the map Set pEnumLayer = pMap.Layers(pUID, True) pEnumLayer.Reset Set pFeatureLayer = pEnumLayer.Next Do While Not pFeatureLayer Is Nothing 'Loop through the selected features per layer Set pFeatureSelection = pFeatureLayer 'QI Set pSelectionSet = pFeatureSelection.SelectionSet 'Can use Nothing keyword if you don't want to draw them, 'otherwise, the spatial reference might not match the Map's pSelectionSet.Search Nothing, False, pFeatureCursor Set pFeature = pFeatureCursor.NextFeature Do While Not pFeature Is Nothing 'Do something with the feature Debug.Print pFeature.Value(pFeature.Fields.FindField("Name")) Set pFeature = pFeatureCursor.NextFeature Loop Set pFeatureLayer = pEnumLayer.Next Loop End Sub
[Visual Basic .NET, C#, C++]
No example is available for Visual Basic .NET, C#, or C++. To view a Visual Basic 6.0 example, click the Language Filter button
in the upper-left corner of the page.