The arch module is tricky. It exploits the fact that OpenSCAD models in 2D are often faster to create
than 3D models. First, the code checks the z value (size[2]) to see if you are trying to produce a 2D or
3D shape. If you provide a non-zero value, it uses linear_extrude() to extrude the arch shape to the
specified thickness. When you do this, the module gets the arch shape by calling itself, a process known
as recursion (discussed in Chapter 2). It passes along all of the values that were passed to it, except that
this time it sets the thickness to zero.
When the z value is zero (or if you only provided x and y values, and there is no size[2]), the code follows
the other branch, which generates the 2D arch shape. This code will generate either a Gothic or circular
arch, with a base width equal to the x value (size[0]), with a rectangular section below it equal to the y
value (size[1]). The rectangular section overlaps the circle used to make the circular arch, or the two
intersected circles for the Gothic one, and an intersection() is used to trim off anything that extends
below the rectangle. Here is the module:
module arch(size, gothic = true) {
if(size[2]) {
linear_extrude(size[2]) arch([size[0], size[1], 0], gothic);
} else intersection() {
union() {
if(gothic) intersection_for(i = [-1, 1])
translate([i * size[0] / 2 , 0, 0]) circle(size[0]);
else circle(size[0] / 2);
if(height > 0) translate([-size[0] / 2, -size[1], 0])
square([size[0], size[1]]);
}
translate([-size[0] / 2, -size[1], 0]) square([size[0], size[0] + size[1]]);
}
}
To add doors and windows to the Chapter 3 castle, we will need to add a row of windows to the side
and back walls, and one door and flanking windows to the front. We have to rotate and translate these
appropriately, too, so they appear in the various walls of the castle. (Remember that they started out in
the x-y plane, but the castle walls are in the y-z or x-z planes.)
Finally, we will need to subtract these from the original castle using dierence() to make it negative
space. Here is the entire model, including those additions (available as castle_advanced.scad). When
all is said and done, you will have the castle shown in Figure 13-9.
Make: Geometry 263
262 Chapter 13: The Geometry Museum
Geometry_Chapter10_v15.indd 262Geometry_Chapter10_v15.indd 262 6/23/2021 9:11:36 AM6/23/2021 9:11:36 AM