通过Python打开一个新的Autocad实例

时间:2013-08-15 17:59:02

标签: python python-2.7 autocad autocad-plugin

有没有人有使用Python与Autocad的经验?我只是尝试,作为一个测试,看看我是否可以通过Python打开一个新的Autocad实例,虽然PyAutocad工作得很好(如果你有任何建议,随时提供其他建议)

无论如何基于doc(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))

1 个答案:

答案 0 :(得分: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版本相对应。