knotflow · Lessons
How a text-based parametric modeller is built, from NURBS curves to a language, a browser and a language server – one lesson for every step. Every lesson ends with something to try yourself.
I · Geometry
- 1.1 What is parametric design?
- What parametric design is, why a model is a recipe rather than a result, and the path knotflow takes from text to a printable file.
- 1.2 Curves
- What a curve in knotflow is, where it comes from, and how to describe smooth curves, including exact circles, with a few numbers: Bézier curves, B-splines, and weights.
- 1.3 Surfaces, solids, triangles
- Curves become surfaces, surfaces become closed solids, solids become triangles, and triangles become a file that a slicer opens without errors.
- 1.4 Computing with floating-point numbers
- How precisely a computer computes, what this means for a model, and which rules knotflow follows so that rounding errors never lead to broken files.
II · The language
- 2.1 Building a language
- How text becomes a model: the lexer, parser and evaluator of a language of its own. The twisted vase from Lesson 1.3 is a text file:
- 2.2 Language design
- A language is a collection of decisions. This lesson first shows the whole language on one page and the places where it differs from what you know from other languages.
- 2.3 Rules you can rely on
- A language for models must above all do one thing: do what is written, or say why not. This lesson collects the rules knotflow keeps for that, and for each one explains which trap it prevents.
- 2.4 Modules
- You should be able to use a library without copying it. In knotflow,
a module is simply a value: a record of its
pubdefinitions. The example is the standard libraryknots, a knot framework written in the language itself (Lesson 3.2). - 2.5 Data from files
- a model should be able to use data someone else described: game pieces in a JSON file, sizes in a table. There are two steps for that:
III · Modelling
- 3.1 Distributing and instances
- The most common step in node editors: create points and put something on each one. Spikes on a body, links along a chain, rivets on a metal sheet. This takes three things:
- 3.2 Knots and building blocks
- A knot framework, written in the language itself. Along the way it becomes clear what a language needs so that you can write libraries in it:
- 3.3 Walls: moving curves sideways
- A cup, a bowl, a pot: they all have a wall of a certain thickness. It is measured at right angles to the wall, not horizontally.
- 3.4 What a solid is
- A single surface can be printed if it is closed all round. Most parts, though, are made of several surfaces: a wall of outer surface, inner surface and rim, a box of six sides, a Boolean solid of pieces of many surfaces.
- 3.5 Holes through curved walls
- The air-pruning pot from Lesson 1.1, this time with its 874 hexagonal cells in the wall. As a mesh model in OpenSCAD, such a pot needs facet phases, eps gaps and detours to avoid sliver triangles.
- 3.6 Where surfaces meet
- Two surfaces that cross meet along a curve. Finding this curve is the most important building block of a CAD kernel. The Booleans (Lesson 3.7) cut faces along it; general CAD kernels need it for fillets and walls as well.
- 3.7 Booleans: union, difference, intersection
- Most parts are put together from simple solids: a plate, bosses on
it, holes through both.
union(a, b)joins two solids,difference(a, b)takes b away from a,intersection(a, b)keeps what both have in common. - 3.8 Walls: shell
- A cup, a bowl, a lampshade have a wall of even thickness, often a
floor and a flat rim.
shell(shape, t)builds it from a shape: - 3.9 Rounded edges
- Sharp edges are unwelcome in 3D printing. At the bottom they bleed into the print bed, at the top they are unpleasant in the hand, and where a boss stands on a plate the part likes to break along the edge.
- 3.10 Rounding the edges of solids
- Lesson 3.9 rounds curves before they become surfaces. But many edges only appear when solids are put together: where a hole leaves a plate, where a boss stands on a plate, where two boxes form an L.
- 3.11 Constructing instead of computing
- whoever writes a model as text often works out by hand where a point has to lie for everything to fit: trigonometry, intersections, distances.
- 3.12 Implicit shapes and simplifying meshes
- For flowing transitions that look grown, knotflow describes shapes a second way: not by control points, but by a function that gives the distance to the surface for every point.
- 3.13 Growing: fields and a bone
- A shape that is not drawn but described: the outline of a thigh bone, the forces while walking, where it is held and how much bone there should be.
- 3.14 Gallery: hard shapes
- Four models of the kind people usually build with node editors – each one a real test for the language and the geometry core.
- 3.15 What research knows about intersections
- Lesson 3.6 shows how knotflow finds the curve where two surfaces intersect, and Lesson 3.7 shows how Booleans are made from it.
IV · Tools
- 4.1 In the browser: WebAssembly, a worker and three.js
- The modeller also runs in the browser, with nothing to install. It is
the same Rust code as in
kf, just compiled to WebAssembly. - 4.2 Recomputing only what changed
- With the cache, exactly the same comes out as without, only faster.
Lesson 1.1 said that a sequence of
letlines is a data-flow graph as in a node editor, and that it follows for free that after a change only the dependent part needs to be recomputed. - 4.3 Preview and exact computation
- The view has to be quick enough, the print has to be exact. If the exact model takes too long, the browser shows a coarser preview that is ready five to ten times sooner, and computes exactly afterwards.
- 4.4 A language server
- In the browser, the modeller marks errors and helps you write (Lesson
4.1). Every editor gets the same help through
kf lsp, a language server. - 4.5 Tools for an AI
- No language model has ever seen knotflow. Still, an AI can write models in it if it gets the same tools as a human: evaluate, read errors, look at the result, look things up.
- 4.6 3MF and pictures
- STL knows only triangles. A slicer gets neither colours nor separate parts from it, and every copy of a shape is stored in full in the file.
- 4.7 knotflow and OpenSCAD: a comparison on large models
- OpenSCAD is the best-known tool that describes shapes as text, and knotflow has taken a lot from it (Lesson 2.3). This lesson puts the two side by side: