Select to view content in your preferred language

Set Symbology....

3507
15
04-09-2010 06:12 AM
JayKappy
Frequent Contributor
I am opening a layer with the code below.  But I have a field that I want to set the symbology to.

After the Shapefile is added to the project, how do I change the symbology?

Class = 1 set to red
Class = 2 set to blue
etc

Sub AddXYShapefileCreatedTable_Click(FileName As String)

Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pworkspaceFactory As IWorkspaceFactory
Dim pfeatureWorkspace As IFeatureWorkspace
Dim pFeatureLayer As IFeatureLayer

'Create a new ShapefileWorkspaceFactory object and open a shapefile folder
    Set pworkspaceFactory = New ShapefileWorkspaceFactory
    Set pfeatureWorkspace = pworkspaceFactory.OpenFromFile("C:\Temp", 0)
'Create a new FeatureLayer and assign a shapefile to it
    Set pFeatureLayer = New FeatureLayer
    Set pFeatureLayer.FeatureClass = pfeatureWorkspace.OpenFeatureClass(FileName)
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
    'pfeaturelayer.ScaleSymbols
    'Add the FeatureLayer to the focus map
    Set pMxDocument = Application.Document
    Set pMap = pMxDocument.FocusMap
    pMap.AddLayer pFeatureLayer

End Sub
0 Kudos
15 Replies
JayKappy
Frequent Contributor
Although I am still confused with the color ramp being used in the code.
http://edndoc.esri.com/arcobjects/9....er_Example.htm

1. It works for me but cant figure out how to change the color ramp to another color ramp. I tried to change "General" to a known color ramp value "Bathymetry_1.10"...it does not work

2. what I would like to do is specifiy my own colors as well, and not use a color ramp. I am just unsure where to put the If Then statement to test for the pRender.Value = ""

I assume a bunch of the code woudl be commented out if I was to do # 2 above. I would have to create the color ramp, get the unique value count etc.

Any thoughts?

Thanks a million guys/ladies

Maybe Simply say: I tried this and can change the style on each "jsy.Style = esriSFSVertical", but cant seem to control the color

Notice how I could change the Style but I cannot manage to control the Color.....uggg
I tried to set the RColors As IRgbColor...no luck....

My problem has to be in the RColor as IEnumCOlors.....I have to be able to set thsi a different way.....Any thoughts?

     Dim RColors As IEnumColors, ny As Long
     Set RColors = rx.Colors
     RColors.Reset
     For ny = 0 To (pRender.ValueCount - 1)
         Dim xv As String
         xv = pRender.Value(ny)
         
         If xv = "4" Then
             Dim jsy As ISimpleFillSymbol
             Set jsy = pRender.Symbol(xv)
             jsy.Color = RColors.Next
                        jsy.Style = esriSFSVertical
             pRender.Symbol(xv) = jsy
         ElseIf xv = "3" Then
             Dim jsy2 As ISimpleFillSymbol
             Set jsy2 = pRender.Symbol(xv)
             jsy2.Color = RColors.Next
                        jsy2.Style = esriSFSHorizontal
             pRender.Symbol(xv) = jsy2
        ElseIf xv = "2" Then
             Dim jsy3 As ISimpleFillSymbol
             Set jsy3 = pRender.Symbol(xv)
             jsy3.Color = RColors.Next
                          jsy3.Style = esriSFSForwardDiagonal
             pRender.Symbol(xv) = jsy3
        ElseIf xv = "1" Then
             Dim jsy4 As ISimpleFillSymbol
             Set jsy4 = pRender.Symbol(xv)
             jsy4.Color = RColors.Next
                          jsy4.Style = esriSFSBackwardDiagonal
             pRender.Symbol(xv) = jsy4
        Else
        End If

     Next ny 



I AM TRYING THIS...but errors out.....
         If xv = "4" Then
             Dim jsy As ISimpleFillSymbol
             Set jsy = pRender.Symbol(xv)

            Dim myColor As IColor
             Set myColor = New RgbColor
             myColor.RGB = RGB(200, 159, 220)
            ' jsy.Color = RColors.Next

             jsy.Color = myColor
             jsy.Style = esriSFSVertical
             pRender.Symbol(xv) = jsy
      Else
     End If
0 Kudos
JayKappy
Frequent Contributor
Any thoughts  on changin the colors and not using the color ramp?

THanks
0 Kudos
maxsteinbrenner
Emerging Contributor
here is some code to change line colors manually:

If xv <> "" Then

Dim pColor As IRgbColor
Set pColor = New RgbColor

  Select Case xv
        Case "01"
            pColor.RGB = RGB(0, 0, 0)
        Case "02"
            pColor.RGB = RGB(76, 230, 0)
        Case "03"
            pColor.RGB = 255
        Case "04"
            pColor.RGB = RGB(197, 0, 255)
        Case "05"
            pColor.RGB = RGB(0, 77, 168)
        Case "06"
            pColor.RGB = RGB(255, 115, 223)
        Case "07"
            pColor.RGB = RGB(209, 255, 115)
        Case "08"
            pColor.RGB = 115
        Case "09"
            pColor.RGB = RGB(230, 230, 0)
        Case "10"
            pColor.RGB = RGB(115, 115, 0)
        Case "11"
            pColor.RGB = RGB(115, 76, 0)
        Case "12"
            pColor.RGB = RGB(112, 68, 137)
        Case "13"
            pColor.RGB = RGB(230, 152, 0)
        Case "14"
            pColor.RGB = RGB(200, 150, 100)
    End Select

Dim pLine As ICartographicLineSymbol
Set pLine = pRender.Symbol(pRender.value(ny))
pLine.Color = pColor
pRender.Symbol(pRender.value(ny)) = pLine
End If
0 Kudos
JayKappy
Frequent Contributor
thanks for the reply....yea this will do what I think I am after...

But I have polygons...I think the iCartographLine Symbol is incorrect....sort of unsure where to go from there.

THanks
0 Kudos
JayKappy
Frequent Contributor
Think I got it....THANKS msteinbrenner.....Although I was knocking my head there for a few minutes....

Polygon so had to change to iSimpleFillSymbol

AND

In the case stamtent.....my values were coming out 1,2,3 not 01,02,03.....So had to change that as well...

BUT WORKING GREAT.....and right before th weekend.....THANKS


            Dim pLine As ISimpleFillSymbol
            Set pLine = pRender.Symbol(pRender.Value(ny))
            pLine.Color = pColor
            pRender.Symbol(pRender.Value(ny)) = pLine
            End If
0 Kudos
JayKappy
Frequent Contributor
MY TOTAL CODE....This takes a selected set and changes the colors to user specs....

I am doing some selecting in another sub and then call this to render the polygons




Sub CreateAndApplyUVRenderer()

     Dim pApp As Application
     Dim pDoc As IMxDocument
     Set pDoc = ThisDocument
     Dim pMap As IMap
     Set pMap = pDoc.FocusMap
 
     Dim pLayer As ILayer
     Set pLayer = pMap.Layer(0)
     Dim pFLayer As IFeatureLayer
     Set pFLayer = pLayer
     Dim pLyr As IGeoFeatureLayer
     Set pLyr = pFLayer
          
     Dim pFeatCls As IFeatureClass
     Set pFeatCls = pFLayer.FeatureClass
     Dim pQueryFilter As IQueryFilter
     Set pQueryFilter = New QueryFilter 'empty supports: SELECT *
     Dim pFeatCursor As IFeatureCursor
     Set pFeatCursor = pFeatCls.Search(pQueryFilter, False)
      
     '** Make the renderer
     Dim pRender As IUniqueValueRenderer, n As Long
     Set pRender = New UniqueValueRenderer
     
     Dim symd As ISimpleFillSymbol
     Set symd = New SimpleFillSymbol
     symd.Style = esriSFSSolid
     symd.Outline.Width = 0.4
     
     '** These properties should be set prior to adding values
     pRender.FieldCount = 1
     pRender.Field(0) = "CLASS"
     pRender.DefaultSymbol = symd
     pRender.UseDefaultSymbol = True
     
     Dim pFeat As IFeature
     n = pFeatCls.FeatureCount(pQueryFilter)
     '** Loop through the features
     Dim i As Integer
     i = 0
     Dim ValFound As Boolean
     Dim NoValFound As Boolean
     Dim uh As Integer
     Dim pFields As IFields
     Dim iField As Integer
     Set pFields = pFeatCursor.Fields
     iField = pFields.FindField("CLASS")
     Do Until i = n
         Dim symx As ISimpleFillSymbol
         Set symx = New SimpleFillSymbol
         symx.Style = esriSFSSolid
         symx.Outline.Width = 0.4
         Set pFeat = pFeatCursor.NextFeature
         Dim x As String
         x = pFeat.Value(iField) '*new Cory*
         '** Test to see if we've already added this value
         '** to the renderer, if not, then add it.
         ValFound = False
         For uh = 0 To (pRender.ValueCount - 1)
           If pRender.Value(uh) = x Then
             NoValFound = True
             Exit For
           End If
         Next uh
         If Not ValFound Then
             pRender.AddValue x, "Tornado Damage Totals", symx
             pRender.Label(x) = x
             pRender.Symbol(x) = symx
         End If
         i = i + 1
     Loop
         
     For ny = 0 To (pRender.ValueCount - 1)
         Dim xv As String
         xv = pRender.Value(ny)
         
         If xv <> "" Then
         
         Dim pColor As IRgbColor
         Set pColor = New RgbColor

            Select Case xv
            Case "1"
            pColor.RGB = RGB(255, 0, 0)
            Case "2"
            pColor.RGB = RGB(0, 92, 230)
            Case "3"
            pColor.RGB = RGB(112, 168, 0)
            Case "4"
            pColor.RGB = RGB(169, 0, 230)
            Case "5"
            pColor.RGB = RGB(0, 77, 168)
            Case "6"
            pColor.RGB = RGB(255, 115, 223)
            Case "7"
            pColor.RGB = RGB(209, 255, 115)
            Case "8"
            pColor.RGB = 115
            Case "9"
            pColor.RGB = RGB(230, 230, 0)
            Case "10"
            pColor.RGB = RGB(115, 115, 0)
            Case "11"
            pColor.RGB = RGB(115, 76, 0)
            Case "12"
            pColor.RGB = RGB(112, 68, 137)
            Case "13"
            pColor.RGB = RGB(230, 152, 0)
            Case "14"
            pColor.RGB = RGB(200, 150, 100)
            End Select

            Dim pPoly As ISimpleFillSymbol
            Set pPoly = pRender.Symbol(pRender.Value(ny))
            pPoly .Color = pColor
            pRender.Symbol(pRender.Value(ny)) = pPoly 
            End If

     Next ny

     '** If you didn't use a color ramp that was predefined
     '** in a style, you need to use "Custom" here, otherwise
     '** use the name of the color ramp you chose.
     pRender.ColorScheme = "General"
     pRender.fieldType(0) = True
     Set pLyr.Renderer = pRender
     pLyr.DisplayField = "CLASS"

     '** This makes the layer properties symbology tab show
     '** show the correct interface.
     Dim hx As IRendererPropertyPage
     Set hx = New UniqueValuePropertyPage
     pLyr.RendererPropertyPageClassID = hx.ClassID

    ' Set the transpareny of the new layer
       If TypeOf pLyr Is ILayerEffects Then
        Dim pLayerEffects As ILayerEffects
        Set pLayerEffects = pLyr
        pLayerEffects.Transparency = 50
      End If

     '** Refresh the TOC
     pDoc.ActiveView.ContentsChanged
     pDoc.UpdateContents

     '** Draw the map
     pDoc.ActiveView.Refresh

End Sub
0 Kudos