How to add a feature class with representations programmatically


This sample code adds a feature class with representations to the focus map and renders the features with the representation renderer.

How to use

  1. Paste the code into VBA.
  2. Modify the code to change the geodatabase path and representation class name.
  3. Run the routine from the Macros dialog.
[VBA]
Public Sub AddRepresentationClass()
    On Error GoTo errorhandler
    
    Dim pWorkspaceFactory As IWorkspaceFactory
    Dim pFeatureWorkspace As IFeatureWorkspace
    Dim pFeatureLayer As IFeatureLayer
    Dim pMxDocument As IMxDocument
    Dim pMap As IMap
    Dim pRC As IRepresentationClass
    Dim pGeoFL As IGeoFeatureLayer
    Dim pRepRend As IRepresentationRenderer
    Dim pRendPropPage As IRendererPropertyPage
    
    'Create a new AccessWorkspaceFactory object and open a personal geodatabase
    Set pWorkspaceFactory = New AccessWorkspaceFactory
    Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("C:\Samples\Test.mdb", 0)
    
    'Get the representation workspace extension from workspace
    Dim pExtManager As IWorkspaceExtensionManager
    Dim pRepWE As IRepresentationWorkspaceExtension
    Dim pUID As IUID
    Set pExtManager = pFeatureWorkspace
    Set pUID = New UID
    pUID.Value = "{FD05270A-8E0B-4823-9DEE-F149347C32B6}"
    Set pRepWE = pExtManager.FindExtension(pUID)
    Set pRC = pRepWE.OpenRepresentationClass("Trees_Rep")
    
    'Create a new FeatureLayer and assign the feature class to it
    Set pFeatureLayer = New FeatureLayer
    Set pFeatureLayer.FeatureClass = pRC.FeatureClass
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
    'Add the FeatureLayer to the focus map
    Set pMxDocument = Application.Document
    Set pMap = pMxDocument.FocusMap
    pMap.AddLayer pFeatureLayer
    
    'Use representation renderer to draw the features
    Set pRepRend = New RepresentationRenderer
    Set pRepRend.RepresentationClass = pRC
    Set pGeoFL = pFeatureLayer
    Set pGeoFL.Renderer = pRepRend
    Set pRendPropPage = New RepresentationRulesPropertyPage
    pGeoFL.RendererPropertyPageClassID = pRendPropPage.ClassID
    'Refresh map drawing and update table of contents (TOC)
    pMxDocument.ActiveView.Refresh
    pMxDocument.UpdateContents
    Exit Sub
errorhandler:
    MsgBox Err.Description, vbInformation, "AddRepresentationClass"
End Sub