如何使用python将autocad的dwg文件转换为png格式,有什么办法吗?

问题描述 投票:-1回答:2

有没有办法使用python将dwg文件AutoCAD更改为png格式或pdf格式。

我试过看了很多文档,没有人回答我的问题

```
from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt
from pyautocad import Autocad, APoint
import os
import win32com.client
from pyautocad import Autocad, APoint
from pyautocad.contrib.tables import Table
from comtypes import COMError
def props(cls):
  return [i for i in cls.__dict__.keys() if i[:1] != '_']
# Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")

# Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()

xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])

wb = copy(xl_workbook)
sheet = wb.get_sheet(0)

dwgfiles = filter(os.path.isfile, os.listdir(os.curdir))



cwd = os.path.abspath(os.path.curdir)  # current working dir
print(cwd)


for f in dwgfiles:
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")

    print(f)
    if f.endswith(".dwg"):
        print("sdaasdas")
        """ open Document"""
        acad = Autocad()
        print(cwd)
        acad.app.Documents.open(cwd + "/" + f)
        exportFile="new2.bmp"





        num_cols = xl_sheet.ncols  # Number of columns
        idx = 1

        acad = win32com.client.Dispatch("AutoCAD.Application")

        doc = acad.ActiveDocument  # Document object
        print(dir(doc))
        doc.Export('exportFile','bmp')
        print("MODEL SPACE")
        count=0
```

请帮帮我解决这个问题?我得到的错误如下

**e_to_string_', '_username_', '_wrap_dispatch_']
Traceback (most recent call last):
  File "auto1.py", line 63, in <module>
    doc.Export(exportFile,"bmp")
  File "<COMObject <unknown>>", line 3, in Export
pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)**

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python autocad autocad-plugin
2个回答
1
投票

我认为最好的方法是运行命令_-PLOT,并使用虚拟PDF绘图仪。 here 您可以找到更多信息。运行命令您可以使用:

  acad.doc.SendCommand("_-PLOT" paramerets )

其中paramerets定义纸张大小,绘图区域等。

我在python中没有任何样本,只是LISP。我希望你能处理翻译。所以最简单的样本可能是:

(setq parameters (list "_n" layoutName "" "PDFCreator" "_n" "_n" "_y")

其中layoutName在我的情况下是可变的,layoutName被读取每个布局迭代。在这种情况下,所有选项都是默认的 - 您需要在绘图窗口中初始化配置并使用按钮[Apply to Layout]

更复杂的例子可能是:

(setq parameters (list 
"_y"            ; detailed configuration
"Layout1"       ; Layout name
"PDFCreator"    ; ploter name 
"A4"            ; page size
""              ; drawing units
""              ; drawing orientation
""              ; plot upside-down
"_w"            ; plot area - window
(strcat (rtos(car P1)) "," (rtos(cadr P1)))     ; P1 and P2 are points ( three elemenets list of coordinates as real value ))
(strcat (rtos(car P2)) "," (rtos(cadr P2)))     ; we need to convert real values to strings
""              ; drawing scale
""              ; plot offset
""              ; use plot style table
""              ; plot styme name
""              ; Lineweight
""              ; Lineweight scale
""              ; plot paperspace first
""              ; plot paperspace objects
""              ; save to file ( *.plt)
"_n"            ; save changes to page configuration
"_y"            ; confirm
)

0
投票

如果您要导出为PDF或PNG,则有所不同。

对于PDF,在我看来,最好的方法是使用虚拟绘图仪,例如PDFCreator。因为你需要设置纸张尺寸等等。

但是对于.PNG你可以使用命令_Export。默认情况下它将显示窗口保存对话框,但如果您将系统变量FILEDIA设置为0,则对话框将被禁用,您可以使用命令行导出。记得最后将FILEDIA设置为旧值。在另一种情况下,它让你很不开心;)

© www.soinside.com 2019 - 2024. All rights reserved.