Win32Com 通过 Python 连接到 Autocad 失败 [英] Win32Com connection failure to Autocad via Python

查看:101
本文介绍了Win32Com 通过 Python 连接到 Autocad 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 COM 接口连接和控制 AutoCAD.为此,我使用 Python.在我第一次使用非常基本的代码段(1) 时,我遇到了一个错误.代码是:

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
")

当我尝试运行它时,我得到了这个:

and when I try to run it, I get this:

Traceback (most recent call last):
  File "C:UsersSBYSMR10Desktop
ecipe-440493-1.py", line 2, in <module>
    acad = win32com.client.Dispatch("AutoCAD.Application")
  File "C:Python26libsite-packageswin32comclient\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:Python26libsite-packageswin32comclientdynamic.py", line 108, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:Python26libsite-packageswin32comclientdynamic.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.我在网上搜索了解决方案,但找不到任何解决方案.那么你能帮我解决这个问题吗?

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) 代码取自 @Tom Haws 的 博客.

(1) Code taken from @Tom Haws' blog.

推荐答案

AutoCAD LT 版本不支持自动化 (Autodesk组讨论).

AutoCAD LT versions doesn't support automation (discussion in Autodesk group).

如果您能够安装完整的 AutoCAD 版本,我建议您使用 comtypes 包.使用 win32com,您无法向文档添加实体,因为 AutoCAD 需要类型代码为 VT_ARRAY | 的 VARIANT.VT_I2 或 VT_ARRAY |VT_R8 用于参数,例如插入点.

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 通过 Python 连接到 Autocad 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆