Win32Com 通过 Python 与 Autocad 连接失败
我需要通过COM接口连接和控制AutoCAD。为此,我使用 Python。在我第一次尝试使用非常基本的代码片段(1)时,我遇到了错误。代码是:
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
doc.Utility.Prompt("Hello from Python\n")
当我尝试运行它时,我得到:
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。我在网上搜索解决方案但找不到任何解决方案。那么你能帮我解决这个问题吗?
(1) 代码取自 @Tom Haws' 博客。
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(1), 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)
I have Python 2.6 and AutoCAD LT 2009 installed. I searched through the net for a solution but couldn't find any. So could you help me fix this issue?
(1) Code taken from @Tom Haws' blog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AutoCAD LT 版本不支持自动化 (Autodesk 小组中的讨论)。
如果您能够安装完整的 AutoCAD 版本,我建议您使用
comtypes
包。使用
win32com
,您无法将实体添加到文档中,因为 AutoCAD 需要具有类型代码VT_ARRAY | 的 VARIANT。 VT_I2 或 VT_ARRAY | VT_R8
用于参数,例如InsertionPoint
。请参阅
comtypes
文档中的示例AutoCAD LT versions doesn't support automation (discussion in Autodesk group).
If you'll be able to install full AutoCAD version, I'd suggest you to use
comtypes
package. Withwin32com
you can't add entities to document, because AutoCAD requires VARIANTs with the typecodesVT_ARRAY | VT_I2 or VT_ARRAY | VT_R8
for parameters, e.g.InsertionPoint
.See examples in
comtypes
documentation