首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AutoCAD保存Python文件(.dwg

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

Stack Overflow用户
提问于 2019-10-18 20:10:17
回答 1查看 1.2K关注 0票数 3

我正在使用win32com在AutoCAD中自动执行一些简单的任务。除了能够保存文件之外,它在大多数情况下都运行得很好。我的目标是打开一个(模板)文件,根据需要调整它,然后将该文件保存为另一个文件夹中的.dwg,同时将模板保留为空,以便下次使用。

下面是我的代码示例:

代码语言:javascript
复制
import win32com.client


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

doc = acad.Documents.Open("C:\\Template_folder\\Template.dwg")
doc.SaveAs("C:\\Output_folder\\Document1.dwg")

### Adjust dwg ###

doc.Save()

加载模板文件运行良好,但在尝试保存文件时(使用SaveAs时,我得到以下错误:

代码语言:javascript
复制
    doc.SaveAs("C:\\Output_folder\\Document1.dwg")
  File "<COMObject Open>", line 3, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'AutoCAD', 'Error saving the document', 'C:\\Program Files\\Autodesk\\AutoCAD 2019\\HELP\\OLE_ERR.CHM', -2145320861, -2145320861), None)

任何技巧或资源都将非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-04 22:16:43

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

代码语言:javascript
复制
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.Application

Application.Documents

Documents.Open()

Application.ActiveDocument

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58450655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档