为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

largeQ 2021-12-21 16:46:20
有什么方法可以使用python将dwg文件AutoCAD更改为png格式或pdf格式。我尝试查看许多文档,但没有一个回答我的问题```from __future__ import print_functionfrom os.path import join, dirname, abspathfrom xlutils.copy import copyimport xlrdimport xlwtfrom pyautocad import Autocad, APointimport osimport win32com.clientfrom pyautocad import Autocad, APointfrom pyautocad.contrib.tables import Tablefrom comtypes import COMErrordef props(cls):  return [i for i in cls.__dict__.keys() if i[:1] != '_']# Create workbookbook = xlwt.Workbook()ws = book.add_sheet("ExportedData")book.save("Exported.xls")# Open the workbookxl_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 dirprint(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 Exportpywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)**
查看完整描述

2 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

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


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

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


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


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

凡layoutName在我的情况下,变量layoutName被读取BE迭代每个布局。在这种情况下,所有选项都将是默认的 - 您需要在绘图窗口中初始化配置并使用按钮[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

)


查看完整回答
反对 回复 2021-12-21
?
holdtom

TA贡献1805条经验 获得超10个赞

如果您想导出为 PDF 或 PNG,这是不同的。

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

但是对于.PNG您可以使用 command _Export。默认它会显示窗口保存对话框,但如果您将系统变量设置FILEDIA0,对话框将被禁用,您可以仅使用命令行导出。请记住最后将 FILEDIA 设置为旧值。在另一种情况下,它会让你非常不高兴;)


查看完整回答
反对 回复 2021-12-21
  • 2 回答
  • 0 关注
  • 722 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号