Select to view content in your preferred language

Bridge between ArcPy and ArcObjects

3207
5
11-17-2009 10:08 AM
MarkCederholm
Regular Contributor II
I don't think Python can truly replace VBA for scripting within ArcMap until there is a way to get to actual ArcObjects in the current session (e.g. through IApplication or IMxDocument).  I don't see anything like that at this time in ArcPy.  Perhaps I'm missing something?  As far as I can tell, MapDocument("CURRENT") only leads to other ArcPy objects, and that's not good enough.
0 Kudos
5 Replies
MarkCederholm
Regular Contributor II
Looks like (after importing the comtypes module) I can pull up the AppRef object w/o any problem in the Python window, so I guess that answers that question.  It's pretty clunky, though, compared to VBA.

Next question:  is there a way to get an IDE like PythonWin to work with an ArcMap session?

Also a suggestion:  if you really want to get serious about using Python for map automation, you should expand ArcPy to allow manipulation of legend items and renderers.
0 Kudos
KimOllivier
Regular Contributor II
Can you expand on this interesting idea? I remember someone attempting to add Python interfaces to all ArcObjects at 8.0 using Swig.

Would this enable a geek to write a Python script that can do anything that VBA can? A bit like we used to call a block of binary code in GWBasic. What would be an example?
0 Kudos
MarkCederholm
Regular Contributor II
I totally forgot about the AppROT object.  I can grab any open ArcMap or ArcCatalog session through that, and go from there.  I don't need the script to be running inside ArcMap's Python window at all.

kimo, to answer your question, with the comtypes package in Python you can use COM objects much as you would in VBA, with some differences.  I'm still learning something new every day.

COM interop performance in Python is about the same as VB .NET, so if you really need the performance of VBA you need to still use that or migrate your code to C++, which is what I use to create coarse-grained COM objects that can be used by .NET or Python.

I submitted an abstract for a user presentation at the 2010 Developer Summit on using ArcObjects in Python.  Let's see if it gets enough votes to be accepted.
0 Kudos
TedCronin
MVP Honored Contributor

I submitted an abstract for a user presentation at the 2010 Developer Summit on using ArcObjects in Python. Let's see if it gets enough votes to be accepted.




I like the sound of that.
0 Kudos
MikeHunter
Occasional Contributor
I just want to let you know that I downloaded the samples from your website and have been experimenting over the last few weeks with ArcObjects/comtypes in Python.  By following your examples, everything has worked like a charm.  I have converted maybe half of my VBA modules to Python and have encountered no show-stoppers as of yet.  And the performance is pretty good.  In most, but not all, cases Python and comtypes is not noticeably slower than VBA.  If you use the GP for grinding, and stay away from heavy use of fine-grained stuff like ITopologicalOperators and such, Python does just fine.  I re-wrote my VBA forms into wxPython, which is a much better toolset anyway. 

Anyway, I just wanted to say thanks for the help, it's much appreciated.  With the demise of VBA, I think we'll see more and more ArcObjects-in-Python solutions. 

Mike   


I totally forgot about the AppROT object.  I can grab any open ArcMap or ArcCatalog session through that, and go from there.  I don't need the script to be running inside ArcMap's Python window at all.

kimo, to answer your question, with the comtypes package in Python you can use COM objects much as you would in VBA, with some differences.  I'm still learning something new every day.

COM interop performance in Python is about the same as VB .NET, so if you really need the performance of VBA you need to still use that or migrate your code to C++, which is what I use to create coarse-grained COM objects that can be used by .NET or Python.

I submitted an abstract for a user presentation at the 2010 Developer Summit on using ArcObjects in Python.  Let's see if it gets enough votes to be accepted.
0 Kudos