Samples

Custom Simple Renderer

Description:

This sample demonstrates how to create a custom renderer and use it in ArcMap. The renderer draws symbology for point, line and polygon feature layers, rendering all features with the same symbol. This sample expands on the First Custom Renderer sample by demonstrating how to implement ISimpleRenderer and ILegendInfo. Implementing ISimpleRenderer lets you assign a symbol to the renderer before drawing and makes the renderer compatible with the single symbol property page. Implementing ILegendInfo makes the rendered layer compatible with the TOC.
Products:

ArcView: VB6

Engine: Java

Platforms: Windows, Solaris, Linux

Requires: For VB Usage: An ArcMap session with at least one layer added.

Minimum ArcGIS Release: 9.0

How to use:
[VB6]
  1. Add a point, line, or polygon feature layer to the map.
  2. In the Visual Basic Editor, go to Tools > References. If this sample's dll (CustomSimpleRenderer) does not appear in the Available References box, Browse to the dll and add it. Check the box next to the dll's name. Dismiss the References dialog.
  3. Copy-paste the following VBA code into a code module and run it.
    Public Sub TestRenderer()
    ' this VBA macro demonstrates using CustomSimpleRenderer.CustomSimpleRend
    ' run this macro with at least one feature layer of points, lines, or polygons in the active dataframe
    ' the macro creates a symbol depending on feature type and the draws the
    ' first featurelayer in the dataframe using this symbol
    
    On Error GoTo ErrHand
       
      Dim pApp As IMxApplication
      Set pApp = Application
      Dim pDoc As IMxDocument
      Set pDoc = ThisDocument
      Dim pMap As IMap
      Set pMap = pDoc.ActiveView.FocusMap
    
      Dim pFeatLyr As IFeatureLayer
      Dim i As Long
      ' run through the layers and get the first featurelayer
      For i = 0 To pMap.LayerCount - 1
        If TypeOf pMap.Layer(i) Is IFeatureLayer Then
          Set pFeatLyr = pMap.Layer(i)
          Exit For
        End If
      Next i
      
    ' --------------------------------------------------
    ' symbol setup
      
      ' this macro will handle feature symbology according to the following rules:
      '     if we have point features, then use a marker symbol for rendering
      '     else, if we have line features, then use a line symbol
      '     else, if we have polygon features, then use a fill symbol
      '     else, (we don't have any of these feature types) so do not assign renderer to layer
        
      Dim pColor As IRgbColor
      Set pColor = New RgbColor
      ' use red. it's a good color
      pColor.RGB = vbRed
      
      Dim pSym As ISymbol
        
      ' based on feature type, make proper symbol, then assign to pSym
      Select Case pFeatLyr.FeatureClass.ShapeType
        Case esriGeometryPoint     ' set up a marker symbol
            Dim pMarkerSym As ISimpleMarkerSymbol
            Set pMarkerSym = New SimpleMarkerSymbol
            With pMarkerSym
                .Size = 12
                .Color = pColor
                .Style = esriSMSX
            End With
            Set pSym = pMarkerSym
      
        Case esriGeometryPolyline    ' set up a line symbol
            Dim pLineSymbol As ISimpleLineSymbol
            Set pLineSymbol = New SimpleLineSymbol
            With pLineSymbol
                .Width = 1
                .Color = pColor
                .Style = esriSLSDashDotDot
            End With
            Set pSym = pLineSymbol
     
        Case esriGeometryPolygon    ' setup a fill symbol
            Dim pFillSymbol As ISimpleFillSymbol
            Set pFillSymbol = New SimpleFillSymbol
            With pFillSymbol
                .Color = pColor
                .Style = esriSFSBackwardDiagonal
            End With
            Set pSym = pFillSymbol
      
        Case Else
            MsgBox "Invalid feature type"
            GoTo EndProc
      
      End Select
    ' --------------------------------------------------
        
      ' create a new CustomSimpleRend
      Dim pRend As IFeatureRenderer
      Set pRend = New CustomSimpleRenderer.CustomSimpleRend
      
      ' set symbol.  we must use ISimpleRenderer interface
      Dim pSimpleRend As ISimpleRenderer
      Set pSimpleRend = pRend
      Set pSimpleRend.Symbol = pSym
      
      Dim pGeoFL As IGeoFeatureLayer
      Set pGeoFL = pFeatLyr
      
      ' finally, set the new renderer to the layer and refresh the map
      Set pGeoFL.Renderer = pRend
      pDoc.ActiveView.Refresh
      pDoc.UpdateContents
    
      GoTo EndProc
    
    ErrHand:
        MsgBox "TestRenderer " & Err.Description
    EndProc:
        ' set all variables to nothing
        Set pApp = Nothing
        Set pDoc = Nothing
        Set pMap = Nothing
        Set pFeatLyr = Nothing
        Set pColor = Nothing
        Set pSym = Nothing
        Set pMarkerSym = Nothing
        Set pLineSymbol = Nothing
        Set pFillSymbol = Nothing
        Set pRend = Nothing
        Set pSimpleRend = Nothing
        Set pGeoFL = Nothing
        Exit Sub
    End Sub
  4. You should see that the default renderer for the first feature layer in the map gets replaced by CustomSimpleRend.
  5. Save the ArcMap document as "CustomSimpleRenderer.mxd". Exit ArcMap.
  6. Start ArcMap again and open "CustomSimpleRenderer.mxd" file. You should see that the layer is still drawng with your the symbol assigned by CustomSimpleRend.
  7. You can change the symbology of the rendered layer by double clicking on the name of the layer and browsing to the Symbology tab.
  8. The symbology can also be changed directly through the layer's entry in the Table of Contents (TOC). Right click on the symbol to change it's color. Double click to change other properties, or to select a different symbol.
  9. The TOC entry will also display on any legend that includes the layer.
[Java]
  1. Run the application

VB6 Java
File Description
CustomSimpleRend.cls Class definition for the custom renderer.
CustomSimpleRenderer.vbp Visual Basic project file.
CustomSimpleRenderer.dll The compiled project.


Key CoClasses:SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, LegendGroup, LegendClass
Key Interfaces: IFeatureRenderer, IPersistVariant, ILegendInfo, ILegendGroup, ILegendClass, ISymbol, ISimpleMarkerSymbol, ISimpleLineSymbol, ISimpleFillSymbol, IRGBColor
Key Members:ISimpleRenderer::Symbol, IRGBColor::RGB, ILegendGroup::Class,, ILegendClass::Symbol


Feedback Send feedback on this sample