Python 将 DXF 文件转换为 PDF、PNG 或 JPEG

2024-04-08

有谁知道将 DXF 文件转换为 PNG 或 PDF 的方法吗?

我有一个巨大的 DXF 文件列表,我想将它们转换为图像以便更快地查看它们。

如果可能的话,如何提取 DXF 文件值,例如 DXF 文件中绘图的厚度或尺寸。


https://github.com/Hamza442004/DXF2img 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 将 DXF 文件转换为 PDF、PNG 或 JPEG 的相关文章

随机推荐