Win32Com连接失败到Autocad通过Python [英] Win32Com connection failure to Autocad via Python
问题描述
我需要通过Com界面连接和控制Autocad。为了这个目的,我使用Python。在我第一次试用一个非常基本的代码片段,我得到一个错误。代码是
I need to connect and control Autocad via Com interface. For this purpose, i use Python. In my first trial with a very basic snippet, i got an error. The code is
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
doc.Utility.Prompt("Hello from Python\n")
当我尝试运行它时,我得到:
and when i try to run it, i get this :
Traceback (most recent call last):
File "C:\Users\SBYSMR10\Desktop\recipe-440493-1.py", line 2, in <module>
acad = win32com.client.Dispatch("AutoCAD.Application")
File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147221005, 'Invalid class string', None, None)
我安装了Python 2.6和Autocad LT 2009。我搜索通过网络的解决方案,但couldnt找到任何。
I have Python 2.6 and Autocad LT 2009 installed. I searched through the net for a solution but couldnt find any. So could you help me fix this issue?
提前感谢
推荐答案
p> AutoCAD LT 版本不支持自动化( Autodesk组讨论)。
AutoCAD LT versions doesn't support automation (discussion in Autodesk group).
如果您能够安装完整的AutoCAD版本,建议您使用 comtypes
package。使用 win32com
,您无法向文档中添加实体,因为AutoCAD要求VARIANT类型代码 VT_ARRAY | VT_I2或VT_ARRAY | VT_R8
InsertionPoint
。
If you'll be able to install full AutoCAD version, I'd suggest you to use comtypes
package. With win32com
you can't add entities to document, because AutoCAD requires VARIANTs with the typecodes VT_ARRAY | VT_I2 or VT_ARRAY | VT_R8
for parameters, e.g. InsertionPoint
.
请参阅 comtypes
文档中的示例
See examples in comtypes
documentation
这篇关于Win32Com连接失败到Autocad通过Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!