Home arrow GIS Data & Resources arrow Scripts and Code arrow Visual Basic / VBA arrow VBA: Set Layer Renderer Colors From Table
VBA: Set Layer Renderer Colors From Table PDF Print E-mail

Written by Bert Granberg,


This script allows for the colors in a UniqueValueRender object to be loaded from a table containing the RGB values for each attribute used in an ArcMap legend. You'll need to specify the layer and table positions and the field names for the colors and the attribute that was symbolized on. Public Sub updateUniqueColorsFromTable()

On Error Resume Next

  Dim pDoc As IMxDocument
  Dim pTC As ITableCollection
  Dim pTable As ITable
  Dim pMap As IMap
  Dim pCursor As ICursor
  Dim pRow As IRow
 
  Dim pLayer As ILayer
  Dim pFLayer As IFeatureLayer
  Dim pGFLayer As IGeoFeatureLayer
  Dim pUVRenderer As IUniqueValueRenderer
  Dim pFillSymbol As IFillSymbol
  Dim pRGBColor As IRgbColor
  Dim pColor As IColor
  Dim redVal, greenVal, blueVal As Integer
  Dim code As String
 
  Set pDoc = ThisDocument
  Set pMap = pDoc.FocusMap
  Set pTC = pMap
  Set pTable = pTC.Table(0)
  Set pLayer = pMap.Layer(0)
  Set pFLayer = pLayer
  Set pGFLayer = pFLayer
  Set pUVRenderer = pGFLayer.Renderer
      
 
  Set pCursor = pTable.Search(Nothing, True)
  Set pRow = pCursor.NextRow
 
  Do Until pRow Is Nothing
    code = pRow.Value(pTable.FindField("DESCRIPTIO")) 'the key field, values must match the values used for the symbolize by unique values
    redVal = pRow.Value(pTable.FindField("RED"))
    greenVal = pRow.Value(pTable.FindField("GREEN"))
    blueVal = pRow.Value(pTable.FindField("BLUE"))
    Set pRGBColor = New RgbColor
    SetpfillSymbol = Nothing
    Set pFillSymbol = pUVRenderer.Symbol(code)
    If Not pFillSymbol Is Nothing Then
        pRGBColor.red = redVal
        pRGBColor.Green = greenVal
        pRGBColor.Blue = blueVal
        Set pColor = pRGBColor
        pFillSymbol.Color = pColor
        pUVRenderer.Symbol(code) = pFillSymbol
        Debug.Print "FOUND "; code
    Else
        Debug.Print "CAN'T FIND "; code
    End If
    Set pRow = pCursor.NextRow
  Loop
 
  Set pGFLayer.Renderer = pUVRenderer
End Sub

Users' Comments  
 

No comment posted

Add your comment

01, Jan. 2008
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for