This sample loops through the selected features of the focus map. It loops using the IEnumFeature interface, which is reached through a QueryInterface from the FeatureSelection property of the map. For each feature it checks the geometry type and if Polygon, it performs a QueryInterface for the IArea interface. Using the Area property of the interface, it adds the area to a running total. At the end, it reports the total area via a message box.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.
How to use
- Add the code to the Click event of a UIButtonControl in ArcMap.
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pUID As New UID
pUID = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" 'IGeoFeatureLayer IID
Dim pEnumLayer As IEnumLayer
Set pEnumLayer = pMxDoc.FocusMap.Layers(pUID, True)
pEnumLayer.Reset
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim pArea As IArea
Dim dTotalArea As Double
Set pFeatureLayer = pEnumLayer.Next
Do Until (pFeatureLayer Is Nothing)
If (pFeatureLayer.FeatureClass.ShapeType = esriGeometryPolygon) Then
Set pFeatureSelection = pFeatureLayer
If (pFeatureSelection.SelectionSet.Count <> 0) Then
pFeatureSelection.SelectionSet.Search Nothing, True, pFeatureCursor
Set pFeature = pFeatureCursor.NextFeature
Do Until (pFeature Is Nothing)
Set pArea = pFeature.Shape
dTotalArea = dTotalArea & pArea.Area
Set pFeature = pFeatureCursor.NextFeature
Loop
End If
End If
Set pFeatureLayer = pEnumLayer.Next
Loop
MsgBox "Total Area for selected polygon features = " & CStr(dTotalArea)