Tutorial - Rendering your first parametric DXF file

Let’s learn by example.

In this tutorial you will render a parametric DXF file using the qsketchmetric.renderer.Renderer module.

We’ll assume you have QSketchMetric installed already.

First download the tutorial.dxf file from the QSketchMetric repository. It is an example of a parametric DXF file that we will use in a tutorial.

To do so, open it and click Ctrl+S to save it to your computer. As a convention, we’ll assume you saved it in a file called tutorial.dxf.

Now, create a new file called render.py and place it in the same directory as tutorial.dxf.

Open render.py in your favorite text editor and import the qsketchmetric.renderer.Renderer module as well as the ezdxf.new() function:

from qsketchmetric.renderer import Renderer
from ezdxf import new
from ezdxf import units

The first one will be used to render the parametric DXF file, the second one to create output ezdxf.document.Drawing and the third one to set the units of the output drawing.

Create an output ezdxf.document.Drawing object using ezdxf.filemanagement.new() module. Remember to set the units of the output drawing to millimeters as the parametric DXF file defaults to meters.:

output_dxf = new()
output_dxf.units = units.MM

Before we will render tutorial.dxf let’s check it out in the QCAD Professional CAD software to see briefly what it looks like (File -> Open). This is what you should see:

tutorial.dxf opened in QCAD Professional

tutorial.dxf opened in QCAD Professional

Note

To see how to parametrize a drawing, see Manual parametrization.

We can see it is a parametric drawing of a chalice. To render, it needs variable h that stands for height of the chalice. Let’s set it to 50:

input_variables = {'h': 50}

Now we are ready to roll. Let’s render the parametric DXF file:

renderer = Renderer('tutorial.dxf', output_dxf, input_variables)
renderer.render()

Finally, save the output drawing:

output_dxf.saveas('rendered_tutorial.dxf')

The whole code should look like this:

from qsketchmetric.renderer import Renderer
from ezdxf import new

output_dxf = new()
input_variables = {'h': 50}
renderer = Renderer('tutorial.dxf', output_dxf, input_variables)
renderer.render()
output_dxf.saveas('rendered_tutorial.dxf')

Now run the code:

python render.py

Finally open rendered_tutorial.dxf in QCAD Professional. This is what you should see:

rendered_tutorial.dxf opened in QCAD Professional

rendered_tutorial.dxf opened in QCAD Professional

As you can see, the parametric DXF file was rendered successfully and the chalice height is 50.

Congratulation you renderer your first parametric DXF file!