1

I am currently converting a dxf drawing that I made, to a pdf drawing using the function described here: Python converting DXF files to PDF or PNG or JPEG. (I am also putting the code below)

The problem is, that when I convert to the pdf, the code automatically scales the drawing to make it fit to a certain size. Now I need to either turn this off, or have a way of knowing what the scaling factor that it used was.

The complete code is as follows:

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(['test.DXF'],img_format='.pdf')
martineau
  • 119,623
  • 25
  • 170
  • 301
Ian
  • 151
  • 1
  • 3
  • 9

1 Answers1

1

From the github discussion thread: https://github.com/mozman/ezdxf/discussions/357

This can be solved in a way that isn't specific to ezdxf by carefully setting the figure size before saving. Matplotlib is quite complex when it comes to measurements. I have a solution which seems to work well but there may be slight inaccuracies since the calculations are done using floating point numbers but at the end of the day pixels are a discrete measurement so it's probably possible to be at least 1 pixel off. There are probably lots of other things like line widths which have an effect. ... you can calculate the desired figure size by specifying a desired units_to_pixels conversion factor and scaling the figure size so that the data spans the correct number of pixels. This assumes that the figure aspect ratio is already correct as my solution uses the same scale factor for width and height.

There's an extended workaround at the page I linked. Rather than copy-paste it here, I think the whole response is worth reading.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134