English

How To: Calculate the area for a polygon graphic in ArcMap

Summary

Instructions provided describe how to create a custom button in ArcMap that returns the area of a selected polygon graphic.
 

Note:
 This code is not designed to find the area of polygon features in shapefiles, feature classes, or coverages. This code only determines the area of selected polygon graphic elements in a map document, such as those created with the graphic tools on the Drawing toolbar.

Procedure

 

  1. Start ArcMap.
  2. Create a new UIButtonControl. Select Tools > Customize to open the Customize dialog box.
  3. Click the Commands tab.
  4. Select UIControls from the Categories list box.
  5. Select the format that the button will be saved to from the Save In drop-down menu.
    • Select Untitled to save to the current map document.
    • Select Normal to save to all ArcMap documents on the machine.
  6. Click New UIControl.
  7. Click UIButtonControl > Create.
  8. Drag the new UIButtonControl to the desired toolbar.
  9. Close the Customize dialog box.
     
    Note:
     For more information on creating a UIControl, see the ArcGIS Desktop Help topic: Creating custom commands with VBA and UI Controls
  10. Right-click the UIButtonControl and select View Source.
  11. Copy the following code into the UIButtonControl's click event.
     
    Dim pApp As IApplication
    Set pApp = Application
    
    '-- Get the map document
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    
    '-- Get the graphics container for the active view
    Dim pAv As IActiveView
    Set pAv = pDoc.ActiveView
    Dim pGc As IGraphicsContainerSelect
    Set pGc = pAv.GraphicsContainer
    
    '--Make sure a graphic element is selected
    If pGc.ElementSelectionCount = 0 Then
    MsgBox "Please select a graphic element"
    Exit Sub
    End If
    
    '-- Make sure only one element is selected
    Dim pElem As IElement
    If pGc.ElementSelectionCount > 1 Then
    MsgBox "Only one element can be selected"
    Exit Sub
    End If
    
    '-- Get the area for the selected element
    Dim pPoly As IPolygon
    Dim pArea As IArea
    Set pElem = pGc.SelectedElement(0)
    
    If TypeOf pElem.Geometry Is IPolygon Then
    Set pArea = pElem.Geometry
    '-- Send the area to the status bar
    pApp.StatusBar.Message(0) = "Element Area : " & pArea.Area
    '-- or a message box
    MsgBox "Element Area: " & pArea.Area
    End If
  12. Select a polygon graphic and press the UIButtonControl. The area is reported in the lower-left status bar as Element Area, as well as in a pop-up message box.

Related Information