This is my fifth post in a series about the open source split-flap display I’ve been designing in my free time. Check out a video of the prototype.
Posts in the series:
Scripting KiCad Pcbnew exports
Automated KiCad, OpenSCAD rendering using Travis CI
Using UI automation to export KiCad schematics
In addition to creating a nice animated rendering, I wanted to make sure I could consistently export the final vector design to be laser cut. There were three main challenges to this:
Because each piece is a separate module, they can be moved and rotated (using the
The splitflap design uses this technique to reuse the same components in the 3d model and 2d flattened layout. The only thing you have to remember is to include all the pieces from the 3d model into the flattened module as well!
To illustrate, here’s an exaggerated example: you can see the desired design on the left, and in the middle I’ve superimposed a particularly wide “laser beam” path in blue as if the center of the laser followed the contours of the design to cut it out.
Notice how much less of the teal part is exposed in the middle image? On the right, you can see the material that would be left if a wood panel was cut using the blue “laser beam” path — the shape that we wanted came out way too small and thin!
To correct for this kerf, we need to adjust the design so that all edges are shifted outward by half the laser beam width. This can be done by applying the
Note that before the
Below you can see the design after applying the kerf-adjustment
On a real design, the impact of kerf won’t be quite so visually obvious as in this example (it’s something small like 0.2mm for the wood I used), but that small difference can be pretty important if you want a clean, tight fit.
One common technique to save money when laser cutting is to make multiple pieces share a common cut line since you’re generally charged for the total length of all cuts.
This presents a problem though if you use a simple export of a single SVG image — sometimes OpenSCAD will merge shapes if their edges perfectly overlap:
Another issue with exporting the entire design as a single SVG is that you can’t render overlapping components with 2d shapes. In the splitflap design, the text to be engraved is aligned directly on top of the bottom panel:
But when flattened into a 2d shape, the overlapping text is merged into the bottom panel shape, and since the bottom panel is larger than the engraved text, the text is lost completely in the exported design.
With a bit of scripting it’s not too difficult to export each component to its own SVG before merging them to avoid both of these problems. To start with, we can create a wrapper module that lets us render a single child element at a time (and we can also use this to apply the kerf correction discussed above):
To use it, we just wrap the list of laid out elements with it:
Then from a python script, we can first run OpenSCAD to identify the number of individual components to render (determined by looking for the output of the
Once all the components have been exported as separate SVGs, it’s easy to combine the
There are a few other tricks I used so that the python script can distinguish between components that should be cut out vs. engraved and apply the appropriate stroke and fill styles in the final SVG.
You can find those tricks and more details in the source code:
/3d/generate_2d.py
/3d/projection_renderer.scad
/3d/projection_renderer.py
/3d/svg_processor.py
/3d/openscad.py
In a past blog post, I discussed how I run this script using Travis CI to automatically render the flattened 2d design (shown at the top of this post) and more every time the source code changes. You should check it out if you haven’t already: Automated KiCad, OpenSCAD rendering using Travis CI.
Posts in the series:
Scripting KiCad Pcbnew exports
Automated KiCad, OpenSCAD rendering using Travis CI
Using UI automation to export KiCad schematics
In addition to creating a nice animated rendering, I wanted to make sure I could consistently export the final vector design to be laser cut. There were three main challenges to this:
- Layout - All of the pieces that make up the 3D design need to be laid out flat so they can be cut out of a single sheet of wood.
- Kerf - When laser cutting, the beam burns away material, leaving a gap where cuts were made (referred to as kerf). This means that shapes will all be slightly smaller than desired if cut exactly to dimension, so the dimensions need to be adjusted to compensate.
- Generating output - Laser cutters typically operate using a vector image such as SVG, and expect a strict set of encoded properties, e.g. cut lines in blue, vector engraving in black, etc, so we need to transform OpenSCAD’s SVG output to conform.
Layout
For a little background, in the 3d model I designed each distinct piece (e.g. gear, front enclosure face, etc) as a planar shape (to be cut out of thin MDF wood board) laying flat on the XY plane. Here’s a simple example:
thickness = 4;
module a() {
color("red") {
linear_extrude(thickness, center=true) {
difference() {
square([40,80]);
translate([10, 10]) {
square([20, 60]);
}
}
}
}
}
module b() {
linear_extrude(thickness, center=true) {
difference() {
square([40, 40]);
translate([20, 20]) {
circle(r=15);
}
}
}
}
Because each piece is a separate module, they can be moved and rotated (using the
translate
and rotate
operators) to be assembled into a 3d model, or laid out flat next to each other in the plane for laser cutting:
module 3d() {
translate([-2,0,0])
rotate([0,-90,0])
a();
translate([0, 82, 0])
rotate([90, 0, 0])
b();
}
module flat() {
projection() {
a();
translate([0, 90, 0]) {
b();
}
}
}
The splitflap design uses this technique to reuse the same components in the 3d model and 2d flattened layout. The only thing you have to remember is to include all the pieces from the 3d model into the flattened module as well!
Kerf
While laser cutters enable small, intricate designs, it’s important to remember that just like a table saw blade, the laser beam doing the cutting is not infinitesimally small. This means that if the center of the laser follows the edges/lines of your design exactly, you will actually lose a small amount of material on either side of that line. This is referred to as “kerf,” which has a width that varies depending on the laser cutter, power/speed settings, and material being cut.To illustrate, here’s an exaggerated example: you can see the desired design on the left, and in the middle I’ve superimposed a particularly wide “laser beam” path in blue as if the center of the laser followed the contours of the design to cut it out.
Notice how much less of the teal part is exposed in the middle image? On the right, you can see the material that would be left if a wood panel was cut using the blue “laser beam” path — the shape that we wanted came out way too small and thin!
To correct for this kerf, we need to adjust the design so that all edges are shifted outward by half the laser beam width. This can be done by applying the
offset
operator:
offset(delta=kerf/2) {
projection() {
a();
}
}
Note that before the
offset
is applied a projection()
is used, which flattens a 3d shape by removing the Z-axis. This is necessary because the offset
operator only works on 2d geometry.Below you can see the design after applying the kerf-adjustment
offset
on the left (it’s fatter and the hole is smaller than the original), along with an updated “laser beam” overlay in the middle image that follows those adjusted edges. If you look at what material would remain after cutting, in the rightmost image, you can see that the remaining shape is actually the size that we wanted from our original design (compare it to the original in the left image above)!On a real design, the impact of kerf won’t be quite so visually obvious as in this example (it’s something small like 0.2mm for the wood I used), but that small difference can be pretty important if you want a clean, tight fit.
Generating output
The last piece of the puzzle is taking the flattened 3d design that’s been kerf-corrected and shipping it off to be laser cut. I ordered my laser cut parts from Ponoko, which provides a template SVG file and expects certain image properties for different types of laser cuts:One common technique to save money when laser cutting is to make multiple pieces share a common cut line since you’re generally charged for the total length of all cuts.
This presents a problem though if you use a simple export of a single SVG image — sometimes OpenSCAD will merge shapes if their edges perfectly overlap:
The bottom piece is actually two separate components that accidentally got merged together! |
Another issue with exporting the entire design as a single SVG is that you can’t render overlapping components with 2d shapes. In the splitflap design, the text to be engraved is aligned directly on top of the bottom panel:
But when flattened into a 2d shape, the overlapping text is merged into the bottom panel shape, and since the bottom panel is larger than the engraved text, the text is lost completely in the exported design.
With a bit of scripting it’s not too difficult to export each component to its own SVG before merging them to avoid both of these problems. To start with, we can create a wrapper module that lets us render a single child element at a time (and we can also use this to apply the kerf correction discussed above):
module projection_renderer(render_index = 0, kerf_width = 0) {
echo(num_components=$children);
offset(delta=kerf_width/2) {
projection() {
// Only include a single child, the one at index "render_index"
children(render_index);
}
}
}
To use it, we just wrap the list of laid out elements with it:
render_index = 0;
projection_renderer(render_index=render_index, kerf_width=0.1) {
a();
translate([0, 90, 0]) {
b();
}
}
Then from a python script, we can first run OpenSCAD to identify the number of individual components to render (determined by looking for the output of the
echo(num_components=$children)
statement from the projection_renderer
), and then invoke OpenSCAD that many times, using the -D render_index=<value>
command line argument to increment the render_index
variable each time.Once all the components have been exported as separate SVGs, it’s easy to combine the
<path>
elements from each SVG into a single file.There are a few other tricks I used so that the python script can distinguish between components that should be cut out vs. engraved and apply the appropriate stroke and fill styles in the final SVG.
You can find those tricks and more details in the source code:
/3d/generate_2d.py
/3d/projection_renderer.scad
/3d/projection_renderer.py
/3d/svg_processor.py
/3d/openscad.py
In a past blog post, I discussed how I run this script using Travis CI to automatically render the flattened 2d design (shown at the top of this post) and more every time the source code changes. You should check it out if you haven’t already: Automated KiCad, OpenSCAD rendering using Travis CI.
Hello Scott, I just found this treasure-post from a while ago; thanks a lot for sharing these wonderful lines of code ! :D
ReplyDelete