使用python2.7中的comtypes获取正在运行的AutoCAD应用程序

2024-09-25 06:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取一个实例,以指向已在运行的AutoCAD 2016应用程序,或者在未运行的情况下创建一个新实例。在

下面是一个简单的代码示例:

#Import needed modules
import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetModule, GetActiveObject

try:
        acad = GetActiveObject("AutoCAD.Application.20")
        print "AutoCAD is Active"
        print "########"
except(OSError, COMError): #If AutoCAD isn't running, run it
        acad = CreateObject("AutoCAD.Application.20",dynamic=True)
        print "AutoCAD is successfuly Opened"
        print "########"

当AutoCAD未运行时,函数CreateObject只能在dynamic设置为true时才能成功工作。。但是GetActiveObject抛出回溯错误

^{pr2}$

我见过this但没有任何线索。在

另外,我又回到了comtypes的文档中

The GetActiveObject function succeeds when the COM object is already running, and has registered itself in the COM running object table. Not all COM objects do this.

但这也没有给我任何线索。在

p.SGetActiveObject与其他应用程序(如ETABS 2016和SAP2000 v18

如有任何帮助和有见地的回应,我们将不胜感激,提前感谢


Tags: 实例fromimportcomclient应用程序isrunning
1条回答
网友
1楼 · 发布于 2024-09-25 06:26:44

这很奇怪,但确实有用(至少对我来说是这样)

我去了gen文件夹,它的完整路径是C:\Python27\Lib\site-packages\comtypes\gen,然后查找文件AutoCAD.py,在空闲时打开它,知道它的等效生成符号(ID),如_4E3F492A_FB57_4439_9BF0_1567ED84A3A9_0_1_0,然后删除这4个文件:

  1. 在AutoCAD.py在
  2. 在AutoCAD.pyc在
  3. _4E3F492A_FB57_4439_9BF0_1567; d84a3a9_0_1_0.py
  4. _4E3F492A_FB57_4439_9BF0_1567; d84a3a9_0_1_0.pyc

您可以注意到,最后2个文件是由文件中提到的相同的生成符号命名的AutoCAD.py. 在

相关问题 更多 >