Select to view content in your preferred language

Set Symbology....

2785
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
maxsteinbrenner
Emerging Contributor
your need to look into setting up a renderer

i beleive for that symbology a simple uniquevaluerenderer is what you'd need.

if you can manage to search the old forums there are plenty of examples out there.
0 Kudos
JayKappy
Frequent Contributor
All due respect... I have tried....I searched for Renderer, render symbology, unique render etc etc.....I cant find any examples....
0 Kudos
JayKappy
Frequent Contributor
I am tryign this but the code simple changes the color based on a point, line, polygon....
I need my code to look at a field and change the symbology baced on the field value...

how would I go about modifying this to look at the field value...do I have to build a query?

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

    
  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 = 4
            .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
0 Kudos
AlexanderGray
Honored Contributor
If you look at the help for SimpleRenderClass, you will see it says it applies on symbol for the entire featurelayer.  if you look at the Ifeaturerenderer interface, you will see a bunch of renderer classes that do different things.  You may want to focus your attention to the unique value renderer.  There is an example for it too.
0 Kudos
JayKappy
Frequent Contributor
I appreciate your help....where do I go....ESRI website is getting confusing with all the different areas and their ongoin changes.....
Forums, DEV, online, etc etc etc
I will look it up but have no idea where to go
0 Kudos
JayKappy
Frequent Contributor
0 Kudos
JayKappy
Frequent Contributor
FIRST OFF THANK YOU....for stearing me int he correct direction....

Got it....just left everything the same and changes the few occurances of the Field name....

Last 2 questions....

1.  how do I find out the names of the color ramps.....to change the custom to the Green to Red

2.  How can I set a transparency to the layer as well
0 Kudos
JayKappy
Frequent Contributor
This is why these examples, web sites etc are so confusing...i was reading on how to set the transparency of a layer....there was hundreds of lines of code....

End up this is all you need....why cant there just be a simple general example.....ugggggg

Thanks for all your help guys/ladies...it is very apprecaited.....

   If TypeOf pLyr Is ILayerEffects Then
    Dim pLayerEffects As ILayerEffects
    Set pLayerEffects = pLyr
    pLayerEffects.Transparency = 50
  End If
0 Kudos
JayKappy
Frequent Contributor
See next entry
0 Kudos