I have been experimenting with Python's comtypes module to access ArcObjects. We are still using ArcGIS 10.0 as we are in the process of upgrading our ArcGIS for Server to version 10.2.2. I am currently running ArcGIS desktop version 10.0 and have a VM with 10.2 installed for testing.
Anyways, the following from the Snippets module works to access an open mxd in version 10.0, but fails in 10.2:
def GetApp(app="ArcMap"): import arcpy """app must be 'ArcMap' (default) or 'ArcCatalog'\n\ Execute GetDesktopModules() first""" if not (app == "ArcMap" or app == "ArcCatalog"): print "app must be 'ArcMap' or 'ArcCatalog'" return None import comtypes.gen.esriFramework as esriFramework import comtypes.gen.esriArcMapUI as esriArcMapUI import comtypes.gen.esriCatalogUI as esriCatalogUI pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT) iCount = pAppROT.Count if iCount == 0: return None for i in range(iCount): pApp = pAppROT.Item(i) print pApp if app == "ArcCatalog": if CType(pApp, esriCatalogUI.IGxApplication): return pApp continue if CType(pApp, esriArcMapUI.IMxApplication): return pApp return None
In 10.2, I get the following error message:
Traceback (most recent call last): File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 264, in <module> GetApp() File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 86, in GetApp pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT) File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 128, in NewObj ptr = CreateObject(COMClass, interface=COMInterface) File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\comtypes\client\__init__.py", line 235, in CreateObject obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface) File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\comtypes\__init__.py", line 1145, in CoCreateInstance _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p)) File "_ctypes/callproc.c", line 936, in GetResult WindowsError: [Error -2147221164] Class not registered
I thought this may be an issue with versions since in the gen folder all wrappers ended with "_10_0". Everyone's sys.path has access to the same comtypes which is on our server, so I blew away all the modules in the gen folder and called the following function to load all esri modules to ensure I was getting the most up to date wrappers:
def load_all(): '''loads all object libraries''' from comtypes.client import GetModule mods = glob.glob(os.path.join(GetLibPath(), '*.olb')) for mod in mods: GetModule(mod) return
This loaded everything, and I still get that same error when running at 10.2. I saw this post with a similar problem but the solution did not work for me: Python, comtypes and ArcObjects: Error creating AppROT object
Has anyone else had this problem? If so, how did you alter the GetApp() function. Thanks.