Python converting DXF files to PDF or PNG or JPEG [closed]
Asked Answered
R

4

5

Does anyone know any way of converting DXF files to either PNG or PDF?

I have a huge list of DXF Files and I want to convert them to Images to view them quicker.

If its possible, how could you extract the DXF file values, like the thickness or measurements of that drawing that's in the DXF File.

Reynold answered 17/11, 2019 at 23:20 Comment(0)
R
2

EDIT!!!

import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
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 shared a solution that works marvellously!

Full credit goes to: https://github.com/Hamza442004/DXF2img

Above is the version with out the GUI in case anyone wants the real deal!

Reynold answered 18/11, 2019 at 4:4 Comment(0)
A
3

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)
Aardwolf answered 23/10, 2020 at 0:30 Comment(1)
You are the BEST!!!!! THIS WORKS FANTASTIC!!!!!!!!!! THANK YOU SO MUCH! I'll edit my solution with a working example!Reynold
M
3

The above code did not work for me. I had to replace the following part:

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)

with the following:

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)

Note: make sure to add this at the start:

from ezdxf.addons.drawing.properties import Properties, LayoutProperties
Maas answered 5/10, 2021 at 22:41 Comment(1)
Thanks for the update! It could be that the original answer was outdated. There could have been lots of updates from matplotlib and ezdxf up until now.Reynold
R
2

EDIT!!!

import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
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 shared a solution that works marvellously!

Full credit goes to: https://github.com/Hamza442004/DXF2img

Above is the version with out the GUI in case anyone wants the real deal!

Reynold answered 18/11, 2019 at 4:4 Comment(0)
I
1

Dia from the GNOME project can convert dxf to png, it also has a Python interface allowing for scripting.

Isoniazid answered 17/11, 2019 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.