How to convert features to graphic elements


This sample converts selected features to graphic elements in the focus map's basic graphics layer. The sample works with features having a geometry type of point, polyline, or polygon. For simplicity sake, the sample does not specify a symbol for the new elements forcing the system to automatically create one.

How to use

  1. Paste the code into VBA.
  2. Select one or more features.
  3. From the Macros dialog, run the ConvertFeaturesToGraphics routine.
[VBA]
Public Sub ConvertFeaturesToGraphics()
    Dim pMxDoc As IMxDocument
    Dim pEnumFeature As IEnumFeature
    Dim pFeature As IFeature
    Dim pElement As IElement
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pActiveView As IActiveView
    
    'Create a graphic element based on each selected feature
    Set pMxDoc = Application.Document
    Set pGraphicsContainer = pMxDoc.FocusMap
    Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
    pEnumFeature.Reset
    Set pFeature = pEnumFeature.Next
    
    Do While Not pFeature Is Nothing
        'Determine the geometry type
        Select Case pFeature.Shape.GeometryType
            Case esriGeometryPoint
                Set pElement = New MarkerElement
            Case esriGeometryPolyline
                Set pElement = New LineElement
            Case esriGeometryPolygon
                Set pElement = New PolygonElement
        End Select
        'Give the feature shape to the element
        If Not pElement Is Nothing Then
            pElement.Geometry = pFeature.Shape
            pGraphicsContainer.AddElement pElement, 0
        End If
        'Get the next feature in the selection
        Set pFeature = pEnumFeature.Next
    Loop
    
    Set pActiveView = pMxDoc.FocusMap
    pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
    
End Sub