Using ArcObjects in Python

Recently, I found the need for more fine-grained control over a map document than the Arcpy library alone could afford, so I turned to the comtypes library (downloaded here) and associated Snippets file (ArcGIS 10.1 version here), which allows access to VBA-based ArcObjects. The Mark Cederholm presentation I followed is from the 2010 ESRI Dev Summit, so it is possible that this method is somewhat outdated, but it does exactly what I need.

Here is a simple example showing how to add some guides to the layout rulers in a map document, which is not possible solely using Arcpy:

# Import the modules
from comtypes.client import GetModule, CreateObject
from Snippets import GetStandaloneModules, InitStandalone

# First time through, need to import the “StandaloneModules”. Can comment out later.
# GetStandaloneModules()
InitStandalone()

# Get the Carto module
esriCarto = GetModule(r”C:\Program Files (x86)\ArcGIS\Desktop10.1\com\esriCarto.olb”)

# Create a map document object
mxdObject = CreateObject(esriCarto.MapDocument, interface=esriCarto.IMapDocument)

# Create new mxd file
mxdObject.New(r”map.mxd”)

# Create some new layout guides
for i in range(1,21):
    mxdObject.PageLayout.VerticalSnapGuides.AddGuide(i)
    mxdObject.PageLayout.HorizontalSnapGuides.AddGuide(i)

# Change current view to layout
mxdObject.SetActiveView(mxdObject.PageLayout)

# Save the mxd
mxdObject.Save()

# Delete the map document object
del mxdObject

…and the result:

layout

Advertisements

3 thoughts on “Using ArcObjects in Python

  1. Pingback: Accessing ArcObjects in Python | Tereshenkov's Blog

    1. dkwiens Post author

      Glad you found it somewhat useful. The hardest part for me was navigating the ArcObjects help pages, but it’s worth the effort if you need to do something simple (like the example) that’s not available via arcpy. Much easier than, say, learning VBA.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s