How to convert hatches to graphics


This sample code demonstrates how to convert hatches into graphics. The reason you might want to convert hatches to graphics is so that the individual hatches can be moved, resized, and edited on the map for cartographic reasons.
The following code takes the selected features in the layer and converts the hatches to graphics. The graphics are output to a new annotation target group and added to ArcMap as a graphics layer.

How to use

  1. Add a polyline (with measures) layer to your map.
  2. Set up hatches on the layer.
  3. Select one or more features.
  4. Paste the code into VBA.
  5. Modify the code to match your data, etc .
  6. Run the code.
  7. Turn the layer off to see that the new graphics more clearly.
[VBA]
Public Sub ConvertHatchesToGraphics()
    On Error GoTo eh
    
    '+++ This code takes selected features in the route layer which contain hatches and
    '+++ converts the hatches to graphics. The graphics are output to a new annotation target
    '+++ group and added to ArcMap as a graphics layer.
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Dim pActiveView As IActiveView
    Dim i As Long
    
    '+++ Get the layer whose hatches you want to convert.
    Dim pRtLayer As ILayer
    Dim pFeatRtLayer As IFeatureLayer
    For i = 0 To pMap.LayerCount - 1
        Set pRtLayer = pMap.Layer(i)
        If LCase(pRtLayer.Name) = "hatches" Then 'Change the name to reflect your layer name.
            If TypeOf pRtLayer Is IFeatureLayer Then
                Set pFeatRtLayer = pRtLayer
                Exit For
            End If
        End If
    Next i
    If pFeatRtLayer Is Nothing Then
        MsgBox "Could not find the route layer", vbExclamation, "ConvertHatchesToGraphics"
        Exit Sub
    End If
    
    '+++ Get the hatch layer extension attached to the layer. It was added automatically when you added the
    '+++ to your map document.
    Dim pHatchLayerExt As IHatchLayerExtension
    Dim pLayerExt As ILayerExtensions
    Set pLayerExt = pFeatRtLayer
    For i = 0 To pLayerExt.ExtensionCount - 1
        If TypeOf pLayerExt.Extension(i) Is IHatchLayerExtension Then
            Set pHatchLayerExt = pLayerExt.Extension(i)
        End If
    Next i
    If pHatchLayerExt Is Nothing Then
        MsgBox "Could not get hatch layer extension from the route layer", vbExclamation, "ConvertHatchesToGraphics"
        Exit Sub
    End If
    
    '+++ Check to make sure the hatch layer extension has hatch classes that can be converted.
    If pHatchLayerExt.ShowHatches = False Then
        MsgBox "There are no hatches displayed", vbInformation, "ConvertHatchesToGraphics"
        Exit Sub
    End If
    Dim lHatchClassCount As Long
    lHatchClassCount = pHatchLayerExt.HatchClassCount
    If Not lHatchClassCount > 0 Then
        MsgBox "There are no hatches to convert", vbInformation, "ConvertHatchesToGraphics"
        Exit Sub
    End If
    
    '+++ We are only going to convert hatches for the selected set of features, so we need to
    '+++ check if there are any features selected.
    Dim pFeatureSelection As IFeatureSelection
    Dim pSelectionSet As ISelectionSet
    Set pFeatureSelection = pFeatRtLayer
    Set pSelectionSet = pFeatureSelection.SelectionSet
    If pSelectionSet.Count = 0 Then
        MsgBox "No route features selected.", vbInformation, "ConvertHatchesToGraphics"
        Exit Sub
    End If
    
    '+++ Set up a Graphics Layer that the converted graphics will output to
    '+++ We will create a new composite graphics layer.
    Dim pCompGraphicsLayer As ICompositeGraphicsLayer
    Dim pGraphicsLayer As IGraphicsLayer
    Set pCompGraphicsLayer = pMap.BasicGraphicsLayer
    Set pGraphicsLayer = pCompGraphicsLayer.AddLayer("CovertedHatches", Nothing) 'This should not exist
    
    '+++ Set as active anno target.
    Set pMap.ActiveGraphicsLayer = pGraphicsLayer
    
    '+++ Set up the Display object to be passed into the the ConvertToGraphics method.
    Dim pDisplay As IScreenDisplay
    Set pActiveView = pMap
    Set pDisplay = pActiveView.ScreenDisplay
    
    '+++ Convert all visible hatch classes to graphics for the selected route features.
    Dim sHatchClassNames As Variant
    Dim sHatchClassName As Variant
    Dim pHatchClass As IHatchClass
    Dim pHatchTemplate As IHatchTemplate
    sHatchClassNames = pHatchLayerExt.HatchClassNames
    For i = 0 To pHatchLayerExt.HatchClassCount - 1
        sHatchClassName = sHatchClassNames(i)
        Set pHatchClass = pHatchLayerExt.HatchClass(sHatchClassName)
        If pHatchClass.ShowHatches Then
            pHatchClass.ConvertToGraphics pRtLayer, pGraphicsLayer, pDisplay, esriConvertHatchesSelected
        End If
    Next i
    
    pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
    Exit Sub
    
eh:
    Dim lNum As Long, sSrc As String, sDesc As String
    lNum = Err.Number
    sSrc = Err.Source
    sDesc = Err.Description
    Err.Raise lNum, sSrc, sDesc
    
End Sub






Additional Requirements
  • ArcEditor at 9.0; ArcView at 9.1 forward