knotflow Handbook Deutsch Lessons Modeller ↗ Source

Chapter 5

Shapes: curves and surfaces

In knotflow, shapes almost always come about in two steps: first draw a curve in the plane (a profile, an outline, a section), then make a surface of it (turn it, pull it up, stretch it through several sections). For the simplest solids there are ready-made functions.

All sizes are millimetres, all angles degrees. z points up.

Ready-made solids

export box(20, 20, 20) |> move(z: 10)
export cylinder((0, 0, 0), (0, 0, 25), 10) |> move(x: 30)
export sphere(12) |> move(x: 60, z: 12)
export cone(12, 0, 25) |> move(x: 90)
export cone(12, 6, 25) |> move(x: 120)
export torus(12, 4) |> move(x: 155, z: 4)

Box, cylinder, sphere, cone, truncated cone, torus

Function Shape where it stands
box(w, d, h) box, width × depth × height centred on the origin
cylinder(a, b, r) cylinder from point a to point b between a and b
sphere(r) sphere centred on the origin
cone(r1, r2, h) cone or truncated cone, r1 at the bottom, r2 at the top stands on the origin
torus(big, r) ring around the upright axis, tube radius r centred on the origin

Careful: box and sphere sit centred on the origin, so they also reach below z = 0. To make a box stand on the print bed, move it up by half its height: box(20, 20, 20) |> move(z: 10). cylinder, on the other hand, takes two points and lies exactly between them; that is also how to lay a drill hole crosswise: cylinder((-30, 0, 5), (30, 0, 5), 2).

Curves

Curves lie in the xy plane, most of them around the origin. They cannot be exported on their own ("a curve has no area yet"): they are the raw material for surfaces.

let points = [(0, 0), (8, 14), (20, 4), (30, 15)]

let a = line((0, 0), (20, 10))              // straight line from point to point
let b = arc(10, 0, 135)                     // circular arc: radius, start angle, angle swept
let c = circle(10)                          // circle
let d = polygon(6, 10)                      // regular hexagon, corners on radius 10
let e = rectangle(20, 12)                   // rectangle around the origin
let f = polyline(points)                    // straight pieces through the points
let g = interpolate(points)                 // smooth through the points
let h = nurbs(points)                       // smooth, attracted by the points

export [extrude(k, 2) | k <- [c, d, e]]

The curves from above: line, arc, circle, hexagon, rectangle, polyline, interpolate, nurbs

Bézier curves and weights

bezier(points) is the simplest smooth curve: a single piece whose degree is the number of points minus one. Like nurbs, it passes only through the first and the last point; the points in between set its direction and bulge. Four points give the cubic Bézier curve known from drawing programs. bezier takes no more than 16 points (degree at most 15); long curves are built with nurbs or interpolate, which consist of many pieces.

nurbs has two optional arguments: degree (default 3) and weights, one weight per point (default 1 everywhere). A larger weight pulls the curve closer to its point, a smaller one lets it go looser. With the right weights a curve of degree 2 becomes an exact circular arc; that is why circles in knotflow are not polygons (lesson 1.2).

let s = sqrt(2) / 2
let quarter = nurbs([(10, 0), (10, 10), (0, 10)], degree: 2, weights: [1, s, 1])  // quarter circle, r = 10
let pulled  = nurbs([(10, 0), (10, 10), (0, 10)], degree: 2, weights: [1, 3, 1])  // pulled to the corner
let wave    = bezier([(0, 0), (5, 15), (20, 15), (25, 0)])

fn piece(k) = extrude(chain([line((0, 0), (10, 0)), k, line((0, 10), (0, 0))]), 2)

export piece(quarter)
export piece(pulled) |> move(x: 20)
export extrude(chain([wave, line((25, 0), (0, 0))]), 2) |> move(x: 40)

Joining curves: chain

A profile often consists of pieces: a straight floor, then a curved wall. chain joins them into one curve:

let outline = chain([
  line((0, 0), (14, 0)),                              // floor, from the axis outwards
  interpolate([(14, 0), (7, 10), (6, 22), (9, 28)]),  // the wall upwards
  line((9, 28), (0, 28))])                            // top, back to the axis

export revolve(outline)

Every piece must begin exactly where the one before ends. Otherwise chain says how far off it is ("curve 1 starts 1 mm away from where curve 0 ends"). A piece that runs the wrong way is turned round by reverse(curve).

From curves to surfaces

let pawn = chain([
  line((0, 0), (14, 0)),
  interpolate([(14, 0), (7, 10), (6, 22), (9, 28)]),
  line((9, 28), (0, 28))])
let path = interpolate([(0, 0, 0), (10, 0, 15), (0, 5, 30)])

export revolve(pawn)
export extrude(polygon(5, 12), 20) |> move(x: 40)
export loft([circle(12), circle(7) |> move(x: 4, z: 15), circle(10) |> move(z: 30)]) |> move(x: 80)
export sweep(rectangle(6, 3), path) |> move(x: 115)
export tube(path, 3) |> move(x: 140)

revolve, extrude, loft, sweep, tube

revolve: turning

revolve(profile) turns a profile around the upright axis, as on a potter's wheel. The profile is drawn flat: x is the radius, y the height. After turning, y points up (z).

For a closed solid, the profile must

Traps:

extrude: pulling up

extrude(outline, height) pushes an outline upwards. A closed outline gives a solid with a floor and a top. With direction: (0, 1, 1) it goes at a slant.

An outline that crosses itself seen along the extrusion (a figure eight) is reported ("the curve crosses itself near …"), since its walls would run through each other. An open curve in space whose crossing places lie at least the height apart, such as a helix with room between its turns, is fine. Explanation: chapter 14.

loft: stretching through sections

loft(sections) stretches a skin through several curves, in order. The sections are usually built with a comprehension, like the vase in chapter 1. Closed sections give a solid; the ends get flat caps.

All sections must be of the same degree: only circles, only polygons, only smooth curves. A circle (degree 2) and a rectangle (degree 1) do not go together; loft then says "section 1 has degree 1, the first one 2". The remedy: build both the same way, for example both with interpolate(…, closed: true) from points.

loft(…, closed: true) leads from the last section back to the first: a ring of sections.

The skin must not run through itself: a section lying back where the skin came from, or so far aside that the skin through it swings out and back, folds it. knotflow then warns (the surface through the sections turns back on itself); showing and exporting work, but no boolean, shell or rounding. Explanation and remedy: chapter 14.

sweep, tube, pipe: along a path

Moving, turning, scaling

All transformations take any shape, curves and lists of shapes too:

Function what it does
move(shape, x: 10, z: 5) moves; missing directions are 0
translate(shape, (10, 0, 5)) moves by a vector
rotate(shape, 30) turns around the upright axis, counter-clockwise seen from above
rotate(shape, 90, axis: (1, 0, 0)) turns around another axis through the origin
scale(shape, 2), scale(shape, (1, 1, 0.5)) enlarges, also differently per direction
mirror(shape, (1, 0, 0)) mirrors at the plane through the origin with this normal
orient(shape, direction) tilts the shape so that its upright axis points along the direction
place(shape, frame) sets the shape on a frame (see below)

With |> you write them as a chain, in the order in which they happen:

let bar = box(30, 6, 6) |> move(z: 3)

export bar |> rotate(45) |> move(x: -40) color "teal"     // first turn, then move
export bar |> move(x: 40) |> rotate(45) color "ochre"     // first move, then turn

From above: on the left turned first, on the right moved first and then turned around the origin

The order matters. rotate and scale always work around the origin. So: build shapes at the origin, turn and scale them there, and move them to their place last.

Setting on a frame: place

A point only says where something goes, not which way round. For that there are frames: a point with three axes, a small coordinate system of its own, as a record {origin, x, y, z}. place(shape, frame) sets a shape that stands at the origin and points up (+z) onto the frame, so that its origin lies on origin and its upright axis points along z; its x and y directions follow the axes x and y.

let ball = sphere(30)
let pin  = cylinder((0, 0, 0), (0, 0, 8), 2)

export ball color "glass"
export place(pin, frame_on(ball, 0.25, 0.3)) color "plum"
export place(pin, frame_at((0, 0, 30), (0, 0, 1))) color "teal"

Frames are made and changed like this:

Call gives
frame_at(point, direction) a frame at a point, z along the direction
frame_on(surface, u, v) a frame on a surface: z is the normal, x points along u
frames_along(curve, count) frames at equal distances along a curve, z along the curve
scatter(surface, count), grid_on(surface, nu, nv) frames spread at random or in a grid over a surface
spin(f, angle) the frame f, turned around its own z axis
sized(f, k) f with axes k times as long: what is placed on it becomes k times as large

Many copies at once are better set with instance than with place; that and the ways of spreading things out are in chapter 10.

Groups and colours

A list of shapes is a group: it is moved, turned and exported together.

fn leg(x, y) = cylinder((x, y, 0), (x, y, 40), 3)

let legs  = [leg(x, y) | x <- [-25, 25], y <- [-15, 15]]
let top   = box(60, 40, 4) |> move(z: 42)
let table = [top, ...legs]

export table color "ochre"
export table |> rotate(20) |> move(x: 90) color "sage"

Important: a group does not melt its pieces together. If they touch or overlap, the file holds several solids inside one another. Most slicers still print that correctly. If you really want a single solid, for example to drill a hole through it afterwards, join the pieces with union (chapter 7).

Colours are given by color after the export (see chapter 3): with names like "terracotta" or as (r, g, b), see-through as (r, g, b, a). The 3MF file keeps them, so a printer with several colours can take them over.

What comes next

Chapter 6 makes vessels with an even wall out of shapes, chapter 7 puts solids together and drills holes. Rounded corners are best drawn into the curve straight away: fillet(curve, r) rounds its corners, rounded_extrude extrudes it with rounded top and bottom edges (examples/rounded_shapes.kf, chapter 8). How curves and surfaces work inside is explained in lessons 1.2 and 1.3.