使用Python保存AutoCAD文件(.dwg) [英] Saving AutoCAD files (.dwg) using Python

查看:230
本文介绍了使用Python保存AutoCAD文件(.dwg)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 win32com 来自动化AutoCAD中的一些简单任务.除了能够保存文件之外,它的运行情况一直很好.我的目标是打开一个(模板)文件,根据需要进行调整,然后将文件另存为 .dwg 在另一个文件夹中,同时将模板保留为空并准备下次使用./p>

以下是我的代码示例:

  import win32com.clientacad = win32com.client.dynamic.Dispatch("AutoCAD.Application")acad.Visible =真doc = acad.Documents.Open("C:\\ Template_folder \\ Template.dwg")doc.SaveAs("C:\\ Output_folder \\ Document1.dwg")###调整dwg ###doc.Save() 

加载模板文件效果很好,但是在尝试保存文件时(使用

任何提示或资源将不胜感激!

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

  import win32com.clientacad = win32com.client.dynamic.Dispatch("AutoCAD.Application")acad.Visible =真#打开一个新文档并将其设置为活动文档acad.Documents.Open("C:\\ Template_folder \\ Template.dwg")#在使用前设置活动文档doc = acad.ActiveDocument#保存文件doc.SaveAs("C:\\ Output_folder \\ Document1.dwg")###调整dwg ###doc.Save() 

您可以在此处找到文档

AutoCAD.申请

应用程序.文件

文档.打开()

应用程序.ActiveDocument

I am using win32com to automate some simple tasks in AutoCAD. It's mostly been working quite well except for being able to save files. My goal is to open a (template) file, adjust it depending on what is needed then save the file as a .dwg in another folder while leaving the template empty and ready to be used next time.

The following in an example of my code:

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()

Loading the template file works well, but when trying to save the file (using the SaveAs method I get the following error:

    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)

Any tips or resources will be much appreciated!

解决方案

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()

You can find the documentation here

AutoCAD.Application

Application.Documents

Documents.Open()

Application.ActiveDocument

这篇关于使用Python保存AutoCAD文件(.dwg)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆