1

comtypesPython2.6.5およびArcGIS10SP1でArcObjectsを試しています。この回答で説明されているArcObjectsOLBをラップするために純粋なPythonメソッドを使用していますが、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!"

そして、これがスクリプトからの出力です。

トレースバック(最後の最後の呼び出し):
  NewObjのファイル「C:\ temp \ ComHelpers.py」、35行目
    ptr = CreateObject(MyClass、interface = MyInterface)
  CreateObjectのファイル「C:\ Python26 \ ArcGIS10.0 \ lib \ site-packages \ comtypes \ client \ __ init__.py」、235行目
    obj = comtypes.CoCreateInstance(clsid、clsctx = clsctx、interface = interface)
  CoCreateInstanceのファイル"C:\ Python26 \ ArcGIS10.0 \ lib \ site-packages \ comtypes \ __ init __。py"、1145行目
    _ole32.CoCreateInstance(byref(clsid)、punkouter、clsctx、byref(iid)、byref(p))
  GetResultのファイル"_ctypes/ callproc.c"、行925
WindowsError:[エラー-2147221231]ClassFactoryは要求されたクラスを提供できません

AppROTが見つかりません
ArcGISアプリケーションが見つかりません!

あなたが持っているかもしれない洞察に感謝します!

4

1 に答える 1

1

完全を期すために、このソリューションは Jason Scheiler によって GIS Stack Exchange に投稿されました。

最初に arcpy をインポートします。ライセンスのチェックアウトや ArcObjects 10.0 ランタイムのセットアップをそのまま行っていないため、CoClass が見つかりません。

于 2011-01-11T17:38:38.920 に答える