2

I am writing an external Python/comtypes script (in PythonWin) that needs to get a reference to the current ArcGIS 10.0 ArcMap session (through the ArcObjects COM). Because the script is outside the application boundary, I am getting the application reference through the AppROT (running object table). The first code snippet below is the main Python driver module. In it is a function GetApp() to grab an application reference from the AppROT. This code works fine and returns IApplication on the singleton AppRef object. Makes sense, and that's what the ArcObjects reference seems to indicate. Now, my main goal is to get to an IMxDocument. In the main loop, I get to an IDocument successfully and can print the title. The next line, though, a Query Interface, throws an error - NameError: name 'esriArcMapUI' is not defined. The error occurs consistently, even after closing PythonWin and reopening (which you always want to try before you conclude that you have a problem). [BTW, the second code snippet is the CType() function for QI, defined in and imported from the SignHelpers.py module.] So, here are my questions:

(1) What COM object is the IDocument on?
(2) How do I get from my IDocument to the intended IMxDocument? Do I need to create a new MxDocument object first? [Sorry. Two questions there.]
(3) If I don't have to create a new object, then how do I do the QI?

I did a lot of ArcObjects work in VB6 quite a few years ago, so explicit QI's and namespaces are putting the screws to me at the moment. Once I can get to an IMxDocument I will be home free. I would appreciate any help anyone can give me with this.

Also, I apologize for the formatting of the code below. I could not figure out how to get Python code to format correctly. Indentation doesn't work, and some of the Python code is interpreted as formatting characters.

=== code:  main py module ===  

    import sys, os  
    sys.path.append('C:\GISdata\E_drive\AirportData\ATL\Scripts')

    import comtypes
    from SignHelpers import *

    def GetApp(app):  
        """Get a hook into the current session of ArcMap; \n\  
        Execute GetDesktopModules() and GetStandaloneModules() first"""  
        print "GetApp called" #@@@  
        import comtypes.gen.esriFramework as esriFramework    
        import comtypes.gen.esriArcMapUI as esriArcMapUI  
        import comtypes.gen.esriCarto as esriCarto  
        pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)  
        iCount = pAppROT.Count  
        print "appROT count = ", iCount  #@@@  
        if iCount == 0:  
            print 'No ArcGIS application currently running.  Terminating ...'  
            return None  
        for i in range(iCount):  
            pApp = pAppROT.Item(i)  #returns IApplication on AppRef  
            if pApp.Name == app:  
                print "Application:  ", pApp.Name  #@@@  
                return pApp  
        print 'No ArcMap session is running at this time.'  
        return None  


    if __name__ == "__main__":  
        #Wrap needed ArcObjects type libraries (.olb)...  

        ... code omitted ...  

        GetDesktopModules(dtOLB)    #These force comtypes to generate  
        GetStandaloneModules(saOLB) #the Python wrappers for .olb's  

        #Connect to ArcMap      
        pApp = GetApp("ArcMap")  

        pDoc = pApp.Document  #IDocument on current Document object  
        print pDoc.Title  
        pMxDoc = CType(pDoc, esriArcMapUI.IMxDocument)  #QI to IMxDocument on MxDocument  

    === code for CType() ===  
    def CType(obj, interface):  
        try:  
            newobj = obj.QueryInterface(interface)  
            return newobj  
        except:  
            return None
celticflute
  • 121
  • 4
  • this code is not well formatted – Xavier Combelle Oct 29 '10 at 16:33
  • No, it is not. This is my first post, and I couldn't figure out how to get Python code to be formatted correctly. For instance, the double underscores of special attributes are being read as bold tags (though the sidebar says that this should produce italics - I don't want either.) I also am mystified by the lack of indenting; my input code was indented, but I couldn't get it to work. – celticflute Oct 29 '10 at 23:56
  • By the way, I did solve the problem that I posted about. Admittedly, it was a newbie mistake. The import comtypes.gen.esriArcMapUI as esriArcMapUI statement needed to define the esriArcMapUI namespace was being run within the GetApp() function, so the namespace was local to the function. A scoping error - geez I hate it when I do that. Soooo, should I officially answer my own question, or is that contrary to protocol? – celticflute Oct 30 '10 at 00:04
  • Fixed code block formatting. Sorry for the awful mess the first time. – celticflute Oct 30 '10 at 01:11
  • 1
    answer your question, i hate reading though the comments – patrick Nov 03 '10 at 17:46

1 Answers1

0

Scoping error (per the comments):

The import comtypes.gen.esriArcMapUI as esriArcMapUI statement needed to define the esriArcMapUI namespace was being run within the GetApp() function, so the namespace was local to the function.

Macke
  • 24,812
  • 7
  • 82
  • 118