gpt4 book ai didi

python - 通过 Python 打开一个新的 AutoCAD 实例

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:04 24 4
gpt4 key购买 nike

有人有在 AutoCAD 中使用 Python 的经验吗?我只是在尝试,作为一个测试,看看我是否可以通过 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))

最佳答案

您考虑过 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 版本。

关于python - 通过 Python 打开一个新的 AutoCAD 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com