gpt4 book ai didi

Python、comtypes 和 ArcObjects : Error creating AppROT object

转载 作者:太空宇宙 更新时间:2023-11-03 19:33:28 24 4
gpt4 key购买 nike

我正在 Python 2.6.5 和 ArcGIS 10 SP1 下试验 comtypes 和 ArcObjects。我使用纯 Python 方法来包装 this answer 中描述的 ArcObjects OLB。 ,但在 comtypes.CoCreateInstance 方法中出现错误。

这是我正在运行的代码:

def WrapModules():
#force wrapping of all ArcObjects libraries (OLBs)
import os
import comtypes.client
# change com_dir to whatever it is for you
com_dir = r'C:\Program Files\ArcGIS\Desktop10.0\com'
coms = [os.path.join(com_dir, x) for x in os.listdir(com_dir) if os.path.splitext(x)[1].upper() == '.OLB']
map(comtypes.client.GetModule, coms)

def GetApp():
"""Get a hook into the current session of ArcMap"""
from comtypes.gen import esriFramework
pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)
if pAppROT is not None:
iCount = pAppROT.Count
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 == 'ArcMap':
print "ArcMap found"
return pApp
print 'No ArcMap session is running at this time.'
print "No AppROT found"
return None

def NewObj(MyClass, MyInterface):
"""Creates a new comtypes POINTER object where\n\
MyClass is the class to be instantiated,\n\
MyInterface is the interface to be assigned"""
from comtypes.client import CreateObject
import traceback
try:
ptr = CreateObject(MyClass, interface=MyInterface)
return ptr
except:
print traceback.format_exc()
return None

if __name__ == "__main__":
WrapModules()
pApp = GetApp()
if pApp is not None:
print "HWND: %d" % pApp.hWnd
else:
print "No ArcGIS application found!"

这是脚本的输出:

Traceback (most recent call last):  File "C:\temp\ComHelpers.py", line 35, in NewObj    ptr = CreateObject(MyClass, interface=MyInterface)  File "C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\client\__init__.py", line 235, in CreateObject    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)  File "C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\__init__.py", line 1145, in CoCreateInstance    _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))  File "_ctypes/callproc.c", line 925, in GetResultWindowsError: [Error -2147221231] ClassFactory cannot supply requested classNo AppROT foundNo ArcGIS application found!

感谢您提供的任何见解!

最佳答案

为了完整起见,this solution由 Jason Scheirer 在 GIS Stack Exchange 上发布:

Import arcpy first, you aren't doing any license checkout or setting up the ArcObjects 10.0 runtime as-is so it won't find the CoClass.

关于Python、comtypes 和 ArcObjects : Error creating AppROT object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4652911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com