且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python 保存 AutoCAD 文件 (.dwg)

更新时间:2023-02-02 21:09:48

查看用于 AutoCAD 的 ActiveX API 的文档,当您调用 Documents.Open() 时,它看起来应该返回打开的文档并将其设置为活动文档.也就是说,这似乎不是这里实际发生的事情.您的问题的解决方案应如下所示:

Looking at the documentation for the ActiveX API for AutoCAD it looks like when you Call Documents.Open() it should return the opened document and set it as the active document. That said, it looks like that is not what is happening in practice here. The solution for your issue should look something like this:

import win32com.client

acad = win32com.client.dynamic.Dispatch("AutoCAD.Application")
acad.Visible=True

# Open a new document and set it as the active document
acad.Documents.Open("C:\Template_folder\Template.dwg")

# Set the active document before trying to use it
doc = acad.ActiveDocument

# Save the documet
doc.SaveAs("C:\Output_folder\Document1.dwg")

### Adjust dwg ###

doc.Save()

您可以在此处找到文档

AutoCAD.申请

应用程序.文件

文档.打开()

应用程序.活动文档