Accessing ArcObjects through Python

6073
6
04-14-2010 06:56 AM
JonathanBaarda
New Contributor
Anyone know if there's an efficient way to use ArcObjects in a Python script?

If not, does anyone know if there's a way to convert a VBA macro to a tool/script? I need to use ArcObjects to perform some calculations, but doing this through a macro saved in an mxd (like I do now) is definitely not the most efficient method...I guess I could take the time to learn C# but if there's a simpler way, that would be ideal. Anyone got suggestions?
0 Kudos
6 Replies
BradPosthumus
Occasional Contributor II
Yes, it is possible. First of all, however, see if there's a way to convert it using just Python and the geoprocessor object. But if you absolutely need to use ArcObjects in Python, this should get you started:

  1. Install comtypes on your machine from here: http://sourceforge.net/projects/comtypes/files/ (get the most recent version, it will detect your version of Python when you install)

  2. Take a look at the "Using ArcObjects in Python" presentation from Mark Cederholm on this page: http://www.pierssen.com/arcgis/upload/misc/.  He also has sample code on the same page. This man must be a genius.

  3. Read this thread from Frank Perks (another genius) for a different example: http://forums.esri.com/Thread.asp?c=93&f=993&t=289397#927436

I'm astounded these fellows were able to figure this out. I've attempted it on-and-off for several years now but could never get a foothold on it. With there information, I'm now using Python to extract layer information and symbology from an MXD. It's very, very slow compared to ArcObjects in VBA, but it works. (Hopefully the arcpy module when alleviate this need at some point.)

If you need a hand getting started, let us know and I can attempt to throw some code your way.

Brad Posthumus
JonathanBaarda
New Contributor
Thanks, this is just what I was looking for. I can do the majority of what I need with the native geoprocessor, it's just I want to use a couple specific objects. But this is excellent, now I can use them in python.
0 Kudos
BradPosthumus
Occasional Contributor II
Yes, it opens up a whole new world of functionality to Python users! For anyone who has ever asked if MXD's can be created or altered through Python, there's a good chance the answer is "yes" if you use comtypes.
0 Kudos
StefanOffermann
Occasional Contributor II
For anyone who has ever asked if MXD's can be created or altered through Python, there's a good chance the answer is "yes" if you use comtypes.


It is a new feature of arcpy to get access to mxds and its contents with ArcGIS 10, isn't it?

Have a look at the new "mapping" module: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000032000000.htm

Best regards, Stefan
0 Kudos
BradPosthumus
Occasional Contributor II
It is a new feature of arcpy to get access to mxds and its contents with ArcGIS 10, isn't it?

Have a look at the new "mapping" module: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000032000000.htm


True enough, but for those of us who have at least another year to wait  before having to ArcGIS 10 -in our case due to a general corporate roll-out lag, or for others who won't be upgrading any time soon- it's nice to have this option now rather than later.
0 Kudos
FrankPerks
New Contributor II

  • Read this thread from Frank Perks (another genius) for a different example: http://forums.esri.com/Thread.asp?c=93&f=993&t=289397#927436

  • [


    😮 I feel special now.

    It's very, very slow compared to ArcObjects in VBA, but it works. (Hopefully the arcpy module when alleviate this need at some point.)


    Last set of benchmarks i saw, basically put comtypes 3x times faster then arcpy :confused:

    But overall its actually extremely useful IMO. Its a really really easy way to basically or throw something togther extremely quickly just to see if it works. As for comtypes pretty much everything from ESRI's API you can access there is a handful of things that comtypes does not like, simply because of how they are implemented. Hopefully after exams, i can actually finish making my more friendly arcgis comtypes library. (theres bugs, esri does not follow standards for iterable interfaces, comtypes does not handle esri hresult markup, etc.
    0 Kudos