簡體   English   中英

Python 將 DXF 文件轉換為 PDF 或 PNG 或 JPEG

[英]Python converting DXF files to PDF or PNG or JPEG

有誰知道將 DXF 文件轉換為 PNG 或 PDF 的任何方法?

我有一個龐大的 DXF 文件列表,我想將它們轉換為圖像以更快地查看它們。

如果可能,您如何提取 DXF 文件值,例如 DXF 文件中該圖形的厚度或測量值。

https://github.com/Hamza442004/DXF2img

import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
#import wx
import glob
import re

class DXF2IMG(object):

default_img_format = '.png'
default_img_res = 300
def convert_dxf2img(self, names, img_format=default_img_format, img_res=default_img_res):
    for name in names:
        doc = ezdxf.readfile(name)
        msp = doc.modelspace()
        # Recommended: audit & repair DXF document before rendering
        auditor = doc.audit()
        # The auditor.errors attribute stores severe errors,
        # which *may* raise exceptions when rendering.
        if len(auditor.errors) != 0:
            raise exception("The DXF document is damaged and can't be converted!")
        else :
            fig = plt.figure()
            ax = fig.add_axes([0, 0, 1, 1])
            ctx = RenderContext(doc)
            ctx.set_current_layout(msp)
            ctx.current_layout.set_colors(bg='#FFFFFF')
            out = MatplotlibBackend(ax)
            Frontend(ctx, out).draw_layout(msp, finalize=True)

            img_name = re.findall("(\S+)\.",name)  # select the image name that is the same as the dxf file name
            first_param = ''.join(img_name) + img_format  #concatenate list and string
            fig.savefig(first_param, dpi=img_res)

上面的代碼對我不起作用。 我不得不更換以下部分:

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
ctx.set_current_layout(msp)
ctx.current_layout.set_colors(bg='#FFFFFF')
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(msp, finalize=True)

具有以下內容:

fig = plt.figure()
ctx = RenderContext(doc)

# Better control over the LayoutProperties used by the drawing frontend
layout_properties = LayoutProperties.from_layout(msp)
layout_properties.set_colors(bg='#FFFFFF')

ax = fig.add_axes([0, 0, 1, 1])

out = MatplotlibBackend(ax, params={"lineweight_scaling": 0.1})

Frontend(ctx, out).draw_layout(msp,layout_properties=layout_properties, finalize=True)

注意:確保在開始時添加:

from ezdxf.addons.drawing.properties import Properties, LayoutProperties

來自GNOME項目的Dia可以將dxf轉換為png ,它還有一個Python接口允許編寫腳本

編輯!!!

import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
# import wx
import glob
import re


class DXF2IMG(object):

    default_img_format = '.png'
    default_img_res = 300
    def convert_dxf2img(self, names, img_format=default_img_format, img_res=default_img_res):
        for name in names:
            doc = ezdxf.readfile(name)
            msp = doc.modelspace()
            # Recommended: audit & repair DXF document before rendering
            auditor = doc.audit()
            # The auditor.errors attribute stores severe errors,
            # which *may* raise exceptions when rendering.
            if len(auditor.errors) != 0:
                raise exception("The DXF document is damaged and can't be converted!")
            else :
                fig = plt.figure()
                ax = fig.add_axes([0, 0, 1, 1])
                ctx = RenderContext(doc)
                ctx.set_current_layout(msp)
                ctx.current_layout.set_colors(bg='#FFFFFF')
                out = MatplotlibBackend(ax)
                Frontend(ctx, out).draw_layout(msp, finalize=True)

                img_name = re.findall("(\S+)\.",name)  # select the image name that is the same as the dxf file name
                first_param = ''.join(img_name) + img_format  #concatenate list and string
                fig.savefig(first_param, dpi=img_res)


if __name__ == '__main__':
    first =  DXF2IMG()
    first.convert_dxf2img(['GT-010.DXF'],img_format='.png')

@hamza-mohammed 分享了一個非常有效的解決方案!

完全歸功於: https://github.com/Hamza442004/DXF2img

以上是沒有 GUI 的版本,以防有人想要真正的交易!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM