【问题标题】:Open a new instance of AutoCAD via Python通过 Python 打开一个新的 AutoCAD 实例
【发布时间】:2013-08-17 23:59:57
【问题描述】:

有人有使用 Python 和 AutoCAD 的经验吗?我只是在尝试,作为一个测试,看看我是否可以通过 Python 打开一个新的 AutoCAD 实例,尽管 PyAutocad 运行良好(如果您有任何建议,请随时提供其他建议)。

无论如何基于文档 (https://pypi.python.org/pypi/pyautocad/#downloads) - 它说这些代码行应该这样做,但到目前为止还没有发生任何事情。

from pyautocad import Autocad, APoint
acad = Autocad()
acad.prompt("Hello, Autocad from Python\n")

只是这些代码行应该在命令行上生成信息,但会产生大约 50 行的回溯(如果有人感兴趣,我也可以发布)- 有什么想法吗?

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
acad.prompt("Hello, Autocad")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 180, in GetActiveObject
obj = comtypes.GetActiveObject(clsid, interface=interface)
File "C:\Python27\lib\site-packages\comtypes\__init__.py", line 1165, in GetActiveObject
oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
File "_ctypes/callproc.c", line 941, in GetResult
WindowsError: [Error -2147221021] Operation unavailable

添加 VBA 模块后,回溯现在显示为:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    acad.prompt("Hello")
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
    self.doc.Utility.Prompt(u"%s\n" % text)
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
    return self.app.ActiveDocument
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
    self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
  File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 183, in GetActiveObject
    return _manage(obj, clsid, interface=interface)
  File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
    obj = GetBestInterface(obj)
  File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
    interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'

现在根据@reclosedev 删除 comtypes 的“gen”文件夹后,Autocad 现在会打开,但文本字符串仍不会出现在命令行上 - 回溯显示:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    acad.prompt("Hello")
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
    self.doc.Utility.Prompt(u"%s\n" % text)
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
    return self.app.ActiveDocument
  File "C:\Python27\lib\site-packages\pyautocad\api.py", line 59, in app
    self._app.Visible = self._visible
  File "C:\Python27\lib\site-packages\comtypes\client\dynamic.py", line 116, in __setattr__
    dispid = self._comobj.GetIDsOfNames(name)[0]
  File "C:\Python27\lib\site-packages\comtypes\automation.py", line 643, in GetIDsOfNames
    self.__com_GetIDsOfNames(riid_null, arr, len(names), lcid, ids)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))

【问题讨论】:

  • 回溯会很有帮助。我敢打赌,阅读此问题的大多数人都没有安装 AutoCAD,因此他们将无法测试您发布的代码。
  • 明白了,对此感到抱歉 - 已编辑为有回溯。谢谢@JosephDunn
  • 您使用哪个版本的 AutoCAD?确保它不是 LT。此外,如果它高于 2010,请确保您已安装 AutoCAD VBA Module
  • 我使用的是 2011 - 我会尝试安装 VBA 模块,看看是否有帮助。我认为 comtypes 包是我唯一需要使两者相互兼容的东西
  • 尝试删除comtypes缓存文件夹C:\Python27\lib\site-packages\comtypes\gen\。如果 AutoCAD 尚未运行,您需要使用 create_if_not_exists=True arg 创建 Autocad 对象。

标签: python python-2.7 autocad autocad-plugin


【解决方案1】:

你考虑过 IronPython 吗?

http://through-the-interface.typepad.com/through_the_interface/2009/03/using-ironpython-with-autocad.html

这是一个如何利用它的简单示例:

import clr
import System

from System import Type, Activator

acApp = Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"))
acApp.Visible = 1
acApp.ActiveDocument.SendCommand("(Princ \"Hello World from Python!\")(Princ)\n")

显然,ProgId 将对应于上次打开的 AutoCAD 版本。

【讨论】:

  • 我最终使用 VisualStudio 并通过 C# 编译整个东西 - 也就是说,由于我正在进入另一个领域,我可能会使用 VS2013 插件重新使用 Python。感谢@Locke 的回答!
猜你喜欢
  • 2020-04-26
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多