knotflow Handbook Deutsch Lessons Modeller ↗ Source

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"

Box and ball: joined, taken from each other, intersected

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"

The plate with two bosses, drilled, with a slot

Line by line:

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 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:

  1. 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.
  2. 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.
  3. 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:

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

How the Boolean operations work inside and which cases are still open is described in lesson 3.7.