Chapter 7
Combining solids
Many parts are put together from simple solids: a plate, two bosses on it, holes through them, a slot cut out. For that there are three functions, the Boolean operations:
| Function | Result |
|---|---|
union(a, b) |
one solid of both: what lies in a or in b |
difference(a, b) |
a without b: a hole, a recess |
intersection(a, b) |
what a and b have in common |
let a = box(30, 30, 30)
let b = sphere(20)
export union(a, b) color "terracotta"
export difference(a, b) |> move(x: 50) color "sage"
export intersection(a, b) |> move(x: 100) color "blue"

a and b may be ready-made solids (box,
cylinder, …), closed surfaces (revolve,
extrude, loft of closed sections) or results
of union and the others themselves. The faces are cut
exactly where they meet; a circle stays a circle until
the model is split into triangles and saved. A finer tolerance when
saving therefore makes the cut edges finer too (chapter 13), down to how
exact the cut is computed: 1e-4 mm, its points up to 4e-4 mm apart
counting as one. Below that, knotflow warns (chapter 14).
A part: a plate with bosses and holes
The example bracket:
param width = 80 in 40..140 "width [mm]"
param depth = 50 in 30..100 "depth [mm]"
param plate = 6 in 3..12 "plate thickness [mm]"
param boss = 9 in 5..15 "radius of the bosses [mm]"
param tall = 14 in 4..30 "height of the bosses [mm]"
param hole = 4 in 1.5..7 "radius of the holes [mm]"
let base = box(width, depth, plate) |> move(z: plate / 2)
let at = [(-width / 2 + boss + 4, 0), (width / 2 - boss - 4, 0)]
let bosses = [cylinder((x, y, plate), (x, y, plate + tall), boss) | (x, y) <- at]
let drills = [cylinder((x, y, -5), (x, y, plate + tall + 5), hole) | (x, y) <- at]
let slot = box(width / 3, 8, plate + 10)
let solid = fold(bosses, base, union)
let holed = fold(drills, solid, difference)
export difference(holed, slot) color "teal"

Line by line:
baseis the plate, lifted by half its thickness so that it lies on z = 0.atare the two places for the bosses;bossesanddrillsbuild one cylinder per place from them.- The drill holes begin 5 mm below the plate and end 5 mm above the bosses: they surely go all the way through.
fold(bosses, base, union)adds one boss after another to the plate. In the same way,fold(drills, solid, difference)drills one hole after another. This works for any number.- The slot
slotis a box, higher than the plate, in the middle:differencecuts it out.
union, difference and
intersection always take exactly two solids. For many, use
fold, as above.
Hollowing out: the mug
A mug with a handle can be built from the outside in: first join
everything on the outside, then take away the inside.
(shell from chapter 6 also hollows it out in one step; with
difference you choose the floor and the shape of the inside
yourself.)
param height = 90 in 50..140 "height [mm]"
param radius = 40 in 25..60 "outer radius [mm]"
param wall = 4 in 2..8 "wall and floor [mm]"
param grip = 22 in 14..35 "size of the handle [mm]"
param thick = 6 in 4..10 "thickness of the handle [mm]"
let body = cylinder((0, 0, 0), (0, 0, height), radius)
let inside = cylinder((0, 0, wall), (0, 0, height + 10), radius - wall)
let handle = torus(grip, thick) |> rotate(90, axis: (1, 0, 0)) |> move(x: radius + 2, z: height / 2)
export difference(union(body, handle), inside) color "terracotta"

- The handle is a torus, stood up (turned around the x axis) and moved to the side. Its inner half is inside the mug.
insidereaches 10 mm above the rim. Otherwise its top face would lie exactly in the plane of the rim.- Because the inside is taken away after the handle is added, the handle does not reach into the mug.
Rules that save trouble
Boolean operations are strongest where faces clearly cut each other. They are weak where faces lie exactly on each other or only touch. knotflow handles many of these cases (a drill hole exactly flush with the underside of the plate, a cylinder standing exactly on a plate), but not all. Three habits avoid the weak spots altogether:
- Let tools reach through. What is taken away reaches a little beyond the solid on every open side: drill holes from below the plate to above it, the inside of a cup above the rim. A millimetre is enough.
- Push parts slightly into each other. What is added sits a little inside the other part instead of standing exactly on it. This matters most for curved parts and for turned profiles with a flat step.
- Make nothing exactly the same size. Two cylinders of the same radius that cross touch in the two points where their intersection curves cross; two almost equal solids, moved by 0.1 mm, cut each other in wafer-thin strips. Better make one clearly larger or smaller.
What knotflow recognises, it reports:
| Message | Case | Remedy |
|---|---|---|
two of the solids share a face (they lie on each other there): move one of them a little |
Two faces lie on each other, such as a flat ring of a turned part on a plate. | push it in a little (rule 2) |
two of the solids touch where they cross (like two cylinders of equal radius at right angles) |
cylinders of equal thickness crossing | change one radius (rule 3) |
the model exports only empty shapes |
difference took everything away or
intersection found nothing |
check sizes and positions |
The first two messages also say where it is and which two solids, and
so do almost share a face and the messages of the surface
intersection (chapter 14):
near (0, 0, 5): the first argument (from (-20, -5, -5) to (20, 5, 5)) and the second argument (from …).
Where an argument is itself several solids combined, say by a
fold, it says
solid 4 of 7 in the first argument, with its extent to find
it by. In the browser, a red dot marks the place in the picture.
What knotflow does not report, kf render shows in its
summary:
parts: 2instead ofparts: 1: the pieces have not joined. Usually they only touch, at a point or along a line (a ball on a plate), instead of cutting each other. Remedy: rule 2. (For a hollow space inside, two parts are right: the outside and the inside.)NOT a closed solid ✗withmore than two triangles: two solids touch exactly at an edge, like two boxes corner to corner. Such a solid is ambiguous for the slicer too. Remedy: rule 2.NOT a closed solid ✗with the noteclosed in memory, but not in the file: two parts touch in a point, a ball at the wall of a hole, say. In memory the solid is closed, but near the point both faces lie closer together than the file's 32-bit numbers can tell apart; there their corners become one point. Most slicers print it all the same. Remedy: rule 2.
kf render checks the mesh as the file holds it, just as
kf check file.stl checks a finished file; in the browser
the status line warns ("open edges", the tooltip says why). Each file
holds the model its own way: an STL as one single mesh, a 3MF each
export as an object of its own. Two exports touching at an edge are
closed in a 3MF, then, but not in an STL; kf render checks
the file -o names, and for a 3MF it adds
edges as the 3MF file holds them.
Closed is not yet right: a mesh can be watertight and still have a
piece too much or miss one.
kf check --against-tree m.kf --tol 0.02 checks that at many
points: does the mesh wind round a point as often as exports hold it?
Whether an export holds it its own solids say (cut finer into
triangles), with union, difference,
intersection between them. Near a face both are right –
there the mesh is off by its tolerance – so only points further than
three times the tolerance from every face count. Errors found are named
with their place, the nearest face and the solid it belongs to; with
--mesh m.stl it checks a finished file made at the same
--tol. Only models whose exports are all solids can be
checked (surfaces closed around count, meshes and copies do not).
solid: a surface as a
solid
solid(surface) turns a closed surface into a solid made
of single faces, the same one union and the others build
internally. You rarely need it, because the Boolean operations take
surfaces directly. It is useful for checking early whether a surface
encloses a solid: an open piece (such as
revolve(profile, 270)) is reported by solid
right away ("the surface is open at its sides"), not only at the union.
In chapter 8 you build rounded solids of your own with
solid(around(…)).
Where surfaces cut each
other: intersect
intersect(a, b) gives not a solid but the
curves along which two surfaces cut each other, as a
list. With them you can make lines of intersection visible or use them
further, for example as the path of a tube:
let ball = sphere(20)
let rod = cylinder((-30, 0, 5), (30, 0, 5), 8)
export ball color "glass"
export rod color "glass"
export [tube(c, 0.6) | c <- intersect(ball, rod)] color "plum"
Here there are two curves: where the rod enters the ball and where it
leaves it. If the surfaces do not cut each other, the list is empty. The
example intersections shows more.
Keeping a distance:
offset_surface
offset_surface(surface, d) moves a surface by d along
its normal everywhere: outwards for a closed solid, inwards with a
negative d. The result is a surface again, to within 0.0001 mm. It gives
a part play: a shape grown by 0.3 mm and taken away
from a block makes a mould or a holder the part fits into.
param play = 0.3 in 0.1..1 "Play [mm]"
let knob = loft([circle(r) |> move(z: z) | (r, z) <- [(12, 0), (9, 10), (11, 18), (8, 24)]])
let gap = offset_surface(knob, play)
let mould = difference(box(36, 36, 20) |> move(z: 10), gap)
export mould color "sage"
export knob |> move(x: 45) color "terracotta"
offset_surface takes smooth surfaces closed all round: a
loft through closed sections, a solid of revolution. A solid of
revolution that ends in a point on the axis has no offset there ("the
surface comes to a point"): the profile must meet the axis level, or the
tip is rounded off. For an even wall inwards shell is
easier (chapter 6).
Limits
- Rounding edges works after a Boolean operation too,
with
fillet_edgesandchamfer_edges(chapter 8). perforatetakes combined solids too, and a perforated solid can be combined further. Holes the other solid does not touch stay as they are; a hole it touches is drilled as withdifference– slow with many such holes. A hole over the line where two solids meet is made withdifference.shellhollows out combined solids as well (chapter 6).- Several contacts in a single point (two intersection curves and an edge meeting exactly there) can give an open mesh. Rule 2 helps here too.
How the Boolean operations work inside and which cases are still open is described in lesson 3.7.