1

I had asked this question previously which I think will work. But as I am new to this, I can't figure out how to get a Layer as an ILayer object to pass to the method that's in my previous questions answer.

I have been playing with the Mark Cederholm's 'Snippets.py' that uses comtypes to drive ArcObjects. That works well, but I don't know enough to venture far from the examples given. It first gets an IMxApplication object, and I can't figure out how to get a Layer file as an ILayer from this object so that I can use the script I was shown in my previous question.

In the end I want to be able to change the Display Expression of some Table Views, and be able to grab them by their name in the TOC.

Note: If you know how to do this with C# or VBA, I can figure it out in Python

Update: Here is the answer in Python which relies on Mark Cederholm's Snippets.py and comtypes:

from Snippets import *
GetDesktopModules()
import comtypes.gen.esriCarto as esriCarto
import comtypes.gen.esriArcMapUI as esriArcMapUI
InitStandalone()
pApp = GetApp()
pMxDoc = CType(pApp.Document, esriArcMapUI.IMxDocument)
pFeatureLayer = CType(pMxDoc.FocusMap.Layer(0), esriCarto.IFeatureLayer) 

2 Answers 2

2

You need to access the active document in order to get access to layers. Create a reference to the document using your IMxApplication object (pMxApp):

Dim pMxDoc as IMxDocument = pMxApp.Document

Then access layers through focusmap:

Dim pLayer as ILayer = pMxDoc.FocusMap.Layer(i)

Where "i" is the index of your layer.

1
  • 1
    Great, that did it. I've updated my answer with the equivalent Python code.
    – cndnflyr
    Jul 8, 2014 at 14:12
1

You can use IMapDocument interface. In my code snippetarcgis102.py it is Mark Cederholm's 'Snippets.py'

pMapDocument = snippetarcgis102.NewObj(esriCarto.MapDocument,esriCarto.IMapDocument)
pMapDocument.open(path) #like pDoc
pMap = pMapDocument.Map(0)
pEnumLayer = pMap.Layers(None,True)
#key = name of layer from gdb (en UPPER CASE) value =ptr to IGeoFeatureLayer layers (not group layers)
layer_ptr_dict={} 

pLayer = pEnumLayer.Next()
while pLayer: 
  pGeoFeatureLayer = snippetarcgis102.CType(pLayer,esriCarto.IGeoFeatureLayer)   
  if pGeoFeatureLayer: # list of IGeofeature layers (not group layers)
    en_name = pGeoFeatureLayer.FeatureClass.AliasName.encode('utf-8') # name in GDB, not in TOC
    layer_ptr_dict [en_name] = pGeoFeatureLayer 
  else: # else it is group layer and we collapse it 
    group_layer =  snippetarcgis102.CType(pLayer,esriCarto.IGroupLayer)
    group_layer.Expanded = False #collapse group layers  
  pLayer = pEnumLayer.Next()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.