How to buffer features and create graphic elements


This sample buffers each selected feature in the focus map and stores the results as polygon graphic elements in the map's basic graphics layer.

How to use

  1. Paste the code into VBA.
  2. Load one or more feature layers.
  3. Select some features.
  4. From the Macros dialog, run the BufferFeatures macro.
[VBA]
Public Sub BufferFeatures()
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pEnumFeature As IEnumFeature
    Dim pFeature As IFeature
    Dim pTopoOp As ITopologicalOperator
    Dim pElement As IElement
    Dim strBufferDistance As String
    
    Set pMxDoc = Application.Document
    Set pActiveView = pMxDoc.FocusMap
    Set pGraphicsContainer = pMxDoc.FocusMap
    
    'Verify there is a feature selection
    If pMxDoc.FocusMap.SelectionCount = 0 Then Exit Sub
    
    'Get a buffer distance from the user
    strBufferDistance = InputBox("Enter Distance:", "Buffer")
    If strBufferDistance = "" Or Not IsNumeric(strBufferDistance) Then Exit Sub
    
    'Buffer all the selected features by the BufferDistance
    'and create a new polygon element from each result
    Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
    pEnumFeature.Reset
    Set pFeature = pEnumFeature.Next
    Do While Not pFeature Is Nothing
        Set pTopoOp = pFeature.Shape
        Set pElement = New PolygonElement
        pElement.Geometry = pTopoOp.Buffer(CInt(strBufferDistance))
        pGraphicsContainer.AddElement pElement, 0
        Set pFeature = pEnumFeature.Next
    Loop
    
    'Redraw the graphics
    pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
End Sub