Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to convert an .mxd file from an older version to ArcMap 10. The old version had two simple VBA scripts attached to toolbar buttons. I am trying to convert this .mxd to ArcMap 10. For deployment reasons I would like to maintain everything in a single file. I would also like to continue using toolbar buttons to run the scripts.

I thought all I would have to do is convert the VBA code to Python. It's apparently not that simple. Can someone please explain whether the following is possible in ArcMap 10 (seems like it should be since it was simple in previous versions):

  • single .mxd (no extra .tbx or .py files)
  • two custom toolbar buttons
  • click button1 and it runs some_python_function_1
  • click button2 and it runs some_python_function_2
share|improve this question
    
Can't wait to see what people have to say. I have the same problem. –  Not to be named Sep 28 '10 at 20:29
    
Unfortunately I had to give up on my approach and use .NET to create an add-in. I liked the portability of having everything except the data contained in a single file. I'm going to leave this question open in the hope that someone out there knows how to do this. It was simple before, but it's impossible now? I'm starting to think developers are idiots...and I are one. –  mwolfe02 Oct 29 '10 at 15:15
add comment

3 Answers

Several things to do to access ArcObjects via Python

  1. Download comtypes module from http://sourceforge.net/projects/comtypes/files/
  2. Save the following code to a python file (gis.py). Be sure that your pythonpath reads the directory in which you save this file.

.

 def GetCurrentApp():
    """Gets an IApplication handle to the current app.
    Must be run inside the app's Python window.
    Execute GetDesktopModules() first"""
    import comtypes.gen.esriFramework as esriFramework
    return NewObj(esriFramework.AppRef, esriFramework.IApplication)


def CType(obj, interface):
    """Casts obj to interface and returns comtypes POINTER or None"""
    try:
        newobj = obj.QueryInterface(interface)
        return newobj
    except:
        return None

now start programming with python:

import gis as gis
import comtypes.gen.esriArcMapUI as esriArcMapUI
import comtypes.gen.esriCartoas as esriCarto

pApp = gis.GetCurrentApp()

pDoc = pApp.Document
pMxDoc = gis.CType(pDoc, esriArcMapUI.IMxDocument)
pMap = pMxDoc.FocusMap
print str(pMap.Name)
pLayout = pMxDoc.PageLayout
pActiveView = gis.CType(pLayout, esriCarto.IActiveView)
pActiveView.Refresh()

It took me a bit of time to figure this out. It should be noted that I found the following websites very informative:

I hope this helps.

share|improve this answer
    
I ask this on another thread, but I'll ask you @twilliams, do you use this in production systems? –  Chad Cooper Oct 28 '10 at 20:27
    
I have not, but I don't see why one couldn't. –  twilliams Oct 28 '10 at 21:59
    
This is excellent information (I upvoted it), but I still don't think it quite meets the goal of having everything contained in a single file. I'm starting to think it's just not possible in Arcmap 10 (outside of getting a VBA license, but that's not future-proof). –  mwolfe02 Oct 29 '10 at 15:10
add comment

I don't do Python, just ArcObjects and VB6/VBA (and trying to migrate into .NET) but I though I would mention that in version 10 you cannot create a UI button control in the "Normal" mxt template. This was possible in all version 8 - 9.x.x. Strangely, VBA code can be written and saved but a button or any other UI control cannot be created in the "Normal" template to call this code. Instead, ESRI only allows "document-level" UI controls to be created. This way you can still create UI controls but maintenance and support becomes a nightmare. Clearly, ESRI is pushing Add-ons.

share|improve this answer
1  
That's good to know (kind of sucks, but good to know). However, in this case I'd actually prefer for everything to be saved at the document level. –  mwolfe02 Sep 29 '10 at 18:59
add comment

This is not a future-proof option but, although VBA is on the way out, you can still install the v10 VBA SDK.

Further to this, You will need to request & register (free) the VBA licence file. (I assume you would also need to do this for all the users who would be running your tools).

Then you could migrate your toolbars/UITools that hook into your python scripts, across into a v10 MXD project (not normal.mxt) and still use it?

This would only buy you some more time till your company rolls to v10.1, but the only other option would be to start moving your tools into an Add-In.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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