views:

21

answers:

0

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