@mylasertools/kerfscript 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/LANGUAGE.md +201 -84
  3. package/README.md +2 -2
  4. package/dist/assembly.d.cts +2 -1
  5. package/dist/assembly.d.ts +2 -1
  6. package/dist/ast.d.cts +6 -0
  7. package/dist/ast.d.ts +6 -0
  8. package/dist/bores.d.cts +5 -0
  9. package/dist/bores.d.ts +5 -0
  10. package/dist/cli.js +2098 -285
  11. package/dist/customizer.d.cts +1 -0
  12. package/dist/customizer.d.ts +1 -0
  13. package/dist/evaluator.d.cts +9 -0
  14. package/dist/evaluator.d.ts +9 -0
  15. package/dist/functions.d.cts +2 -0
  16. package/dist/functions.d.ts +2 -0
  17. package/dist/geometry.d.cts +11 -0
  18. package/dist/geometry.d.ts +11 -0
  19. package/dist/halving.d.cts +8 -0
  20. package/dist/halving.d.ts +8 -0
  21. package/dist/highlight.cjs +2 -1
  22. package/dist/highlight.js +2 -1
  23. package/dist/index.cjs +474 -58
  24. package/dist/index.d.cts +1 -1
  25. package/dist/index.d.ts +1 -1
  26. package/dist/index.js +474 -58
  27. package/dist/language.cjs +462 -46
  28. package/dist/language.js +462 -46
  29. package/dist/libraries.cjs +435 -19
  30. package/dist/libraries.js +435 -19
  31. package/dist/plot.d.cts +16 -0
  32. package/dist/plot.d.ts +16 -0
  33. package/dist/signatures.cjs +189 -5
  34. package/dist/signatures.js +189 -5
  35. package/dist/verify/solids.d.cts +2 -1
  36. package/dist/verify/solids.d.ts +2 -1
  37. package/dist/verify/types.d.cts +2 -0
  38. package/dist/verify/types.d.ts +2 -0
  39. package/docs/cli.md +1 -0
  40. package/docs/compatibility.md +0 -8
  41. package/docs/customizer.md +18 -0
  42. package/docs/library.md +248 -5
  43. package/docs/parts-and-sheets.md +86 -18
  44. package/docs/versions.md +2 -2
  45. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,108 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0 (2026-11-09)
4
+
5
+ Language version 0.3, as in 0.8.0: programs run as before.
6
+
7
+ Language:
8
+
9
+ - `assert(c, msg) expr` and `echo(...) expr` check or trace before an
10
+ expression, so a library function can refuse bad sizes itself; `let` (and
11
+ a function literal) can follow an operator.
12
+ - `sort(values, key)`, `fold(values, initial, fn)` and `scan(values,
13
+ initial, fn)`; `bounds(points)` and `crossings(points, x | y)` for
14
+ outlines as point lists.
15
+ - `intersection_for` intersects its iterations, as in OpenSCAD.
16
+ - `part(qty = 0)` makes nothing; `largest(n)` keeps the biggest pieces of the
17
+ material; `join_islands(width)` joins every piece to the rest.
18
+ - `text(weight = "bold")` and `text(bridge = w)` for cut-out lettering;
19
+ `symbol(solid = true, bridge)` cuts a pictogram in one piece with its
20
+ detail engraved.
21
+
22
+ Plotting (line art as pen plotters draw it, for score and engrave lines):
23
+
24
+ - `noise(x, y, z, seed, octaves)`: smooth gradient noise, or of a point
25
+ `noise(p / 30)`.
26
+ - `flow(field, spacing, step, min_length, seed)` fills an area with evenly
27
+ spaced lines that follow a direction field: an angle, or a function of
28
+ the point giving degrees, a vector, or `undef` where lines stop.
29
+ - `contours(field, levels, interval, cell)` draws where a height field
30
+ crosses each level across an area.
31
+ - `occlude()` draws its children back to front, each opaque: lines and
32
+ outlines hide behind later areas, with each `for` iteration a child of
33
+ its own; `hide()` is an area that covers without being drawn. Stacked
34
+ ridgelines, landscapes and overlapping shapes score without double lines.
35
+ - `$join_lines = true` joins lines that meet end to end and orders them
36
+ nearest first, so the laser travels less between them.
37
+
38
+ Joints, mechanisms and layout:
39
+
40
+ - `part(bores = ["Crank axle"], fit = "turn" | "press" | mm)` cuts holes
41
+ where hardware rods pass through, at any angle.
42
+ - `part(halving = ["Rib*"])`: crossing plates each slotted halfway, from
43
+ opposite sides.
44
+ - `part(slot_extra = [dx, dy])` sweeps slots for parts that drop in.
45
+ - `kerf check` names two parts whose tabs meet in one slot.
46
+ - A setting marked `// [0:5:360] drive` is the one that moves the model:
47
+ `kerf check --drive` tries every value, and the playground plays it.
48
+ - Copies of curved parts pack in rows, every other one turned, when that
49
+ takes fewer sheets (24 S-curved ribs: four sheets to one).
50
+ - `kerf/fit`: `fit(size, mode, anchor)` and `align(anchor)` for drawings of
51
+ unknown size.
52
+
53
+ Fixes:
54
+
55
+ - Slots: an edge lying on a face (a gable wall's top under its roof) is
56
+ touching, not a slot the length of the contact. The slot spans the part
57
+ only across from what goes deeper than the touching tolerance, so a tab's
58
+ flared shoulder is still cut. This removes stray slots from roofs whose
59
+ walls also tab through them.
60
+ - Sheet items of mirrored copies (`mirror = [false, true]`) keep their copy
61
+ numbers from the assembly; both used to say copy 1.
62
+ - A module that uses `children(i)` past the last child it was given warns,
63
+ with the call's line: `m() a(); b();` gives m() one child.
64
+ - Two libraries defining a module or function of the same name warn once,
65
+ naming both files; the later one is still used.
66
+ - A collision is reported at a point inside the overlap, not the middle of
67
+ the two parts' boxes.
68
+ - A line far longer than the area it is clipped to (a part's material, a
69
+ `difference()`) is clipped where it crosses the area, instead of running
70
+ on without reaching the step limit.
71
+
72
+ ## 0.8.0 (2026-11-08)
73
+
74
+ Language version 0.3, as in 0.7.0: programs run as before.
75
+
76
+ - `hardware(shape = "ball")` (or `sides = 0`) is a ball of diameter `size`
77
+ midway between `from` and `to`, which it may leave out: a bulb, a marble
78
+ or a bead. The 3D view draws it, and verification gives it an envelope.
79
+ New hardware kinds: `bulb`, `ball`, `tube`, `lens`, `mirror` and
80
+ `movement`.
81
+ - `measure()` also sets `$area` and `$centroid`, the area of the material
82
+ its first child cuts and its balance point.
83
+ - `offset_path(points, d, closed)` moves a path sideways with mitred
84
+ corners: the two edges of a strip along a curve.
85
+ - `train_for(ratio, stages, pinion, wheel)` gives tooth counts for a gear
86
+ train of 1 to 3 stages nearest a ratio.
87
+ - `kerf/linkage`: `circle_meet()`, `slider_crank()`, `slider_stroke()`,
88
+ `four_bar()`, `four_bar_turns()` and `link_angle()` give where a
89
+ linkage's joints are at a crank angle.
90
+ - `kerf/cones`: `cone_flat()` cuts a cone or cut-off cone flat, with a
91
+ scored glue tab, and `cone_pattern()` gives its sector.
92
+ - `kerf/sizes`: `part_size()` gives the sizes of bought parts: a music box
93
+ movement, a quartz clock movement and marbles.
94
+ - `kerf/gears`: `cam()` draws a cam from a lift curve for a knife or
95
+ roller follower, with `cam_rise()` for dwells, rises and falls,
96
+ `cam_follower()` to place the follower and `cam_pressure()` to check it
97
+ won't jam. `geneva_wheel()`, `geneva_lock()`, `geneva_crank()` and
98
+ `geneva_turn()` make a Geneva drive; `ratchet()` and `pawl()` a wheel
99
+ that turns one way only.
100
+ - `kerf/sizes`: the instant film photo sizes are now `"instant mini"`,
101
+ `"instant square"`, `"instant wide"` and `"instant classic"`. The old
102
+ names still work.
103
+ - `part(material = "red acrylic")` names a part's material without a
104
+ `material()` round it.
105
+
3
106
  ## 0.7.0 (2026-11-03)
4
107
 
5
108
  Language version 0.3. Programs for 0.2 run as before, except that a
package/LANGUAGE.md CHANGED
@@ -90,7 +90,20 @@ produce an error at the source line.
90
90
  In conditions, `false`, `0`, `""`, `[]` and `undef` count as false.
91
91
 
92
92
  Use `let (a = 1, b = a + 1) expr` to define names for one expression.
93
- Each binding can use the ones before it.
93
+ Each binding can use the ones before it. A `let` can follow an operator,
94
+ as in `ok && let (d = b - a) d > 0`; its expression reaches as far right as
95
+ it can.
96
+
97
+ `assert(condition, message) expr` checks before it works out `expr`, which
98
+ a failed assertion never reaches, and `echo(...) expr` prints on the way.
99
+ Checks chain, so a function can refuse bad arguments itself:
100
+
101
+ ```kerf
102
+ function slot_width(t, clearance) =
103
+ assert(t > 0, "Thickness must be positive")
104
+ assert(clearance >= 0, "Clearance must not be negative")
105
+ t + clearance;
106
+ ```
94
107
 
95
108
  ### List comprehensions
96
109
 
@@ -117,6 +130,7 @@ module name(a, b = 2) { ... } // module (makes shapes)
117
130
  name(args) child; // module call with one child
118
131
  name(args) { child; child; } // module call with a block of children
119
132
  for (i = range_or_list, j = ...) ... // repeats, combined as a union
133
+ intersection_for (i = ...) ... // repeats, keeping what every one covers
120
134
  if (cond) ... else ...
121
135
  let (a = 1) ...
122
136
  use <path> // import modules and functions
@@ -128,6 +142,8 @@ Arguments can be positional or named: `rect([20, 10], r = 2)`.
128
142
  A library's own `use <path>` and `include <path>` look next to that
129
143
  library first, then where the main program's would: `lib/props.kerf`
130
144
  saying `use <finish.kerf>` finds `lib/finish.kerf`.
145
+ When two libraries define a module or function of the same name, the one
146
+ used later replaces the earlier, with a warning naming both.
131
147
 
132
148
  ### Lengths in real units
133
149
 
@@ -165,6 +181,9 @@ the render options. `!` is accepted for OpenSCAD files and has no effect.
165
181
  Inside a module, `children()` renders all child statements passed by the
166
182
  caller. `children(index)` selects one child by its position from 0, or several with a list of positions.
167
183
  `$children` is the number of children.
184
+ A call without braces takes exactly one statement as its child: in
185
+ `m() a(); b();` only `a()` is a child, and `b()` stands on its own. A module
186
+ that asks for a child past the last one it was given warns.
168
187
 
169
188
  ```kerf
170
189
  module hang_hole(d = 5) {
@@ -174,23 +193,26 @@ module hang_hole(d = 5) {
174
193
 
175
194
  ## Special variables
176
195
 
177
- | Name | Default | Meaning |
178
- | ----------------- | ------------- | --------------------------------------------------------------------------- |
179
- | `$thickness` | `3` | material thickness, used by joinery and stored on parts |
180
- | `$kerf` | `0` | beam width; cut outlines are offset by half of it |
181
- | `$material` | `"material"` | material name; parts are packed per material and thickness |
182
- | `$sheet` | `[600, 400]` | sheet size for packing |
183
- | `$gap` | `3` | space between packed parts and around the sheet edge |
184
- | `$layout` | `"pack"` | `"pack"` packs parts onto sheets, `"none"` keeps your placement |
185
- | `$fn` `$fa` `$fs` | `0` `2` `0.2` | curve resolution: fixed segment count, or max angle and max segment length |
186
- | `$font` | | default font name for `text()` |
187
- | `$mirrored` | `false` | set inside a mirrored part and on the back face: `text()` reads right |
188
- | `$scale` | `1` | the model's scale: `140cm` is 1400 mm divided by `$scale` |
189
- | `$nest` | `false` | pack small parts into the holes of bigger ones |
190
- | `$registration` | `false` | print-and-cut marks in three corners of every sheet |
191
- | `$label_parts` | `false` | engrave each part's number on its back face, as in the parts list and steps |
192
- | `$min_width` | `1.5` | warn where a part's material is narrower than this; `0` turns it off |
193
- | `$bounds` | | set by `measure()` for the children after the one it measures |
196
+ | Name | Default | Meaning |
197
+ | ----------------- | ------------- | ----------------------------------------------------------------------------- |
198
+ | `$thickness` | `3` | material thickness, used by joinery and stored on parts |
199
+ | `$kerf` | `0` | beam width; cut outlines are offset by half of it |
200
+ | `$material` | `"material"` | material name; parts are packed per material and thickness |
201
+ | `$sheet` | `[600, 400]` | sheet size for packing |
202
+ | `$gap` | `3` | space between packed parts and around the sheet edge |
203
+ | `$layout` | `"pack"` | `"pack"` packs parts onto sheets, `"none"` keeps your placement |
204
+ | `$fn` `$fa` `$fs` | `0` `2` `0.2` | curve resolution: fixed segment count, or max angle and max segment length |
205
+ | `$font` | | default font name for `text()` |
206
+ | `$mirrored` | `false` | set inside a mirrored part and on the back face: `text()` reads right |
207
+ | `$scale` | `1` | the model's scale: `140cm` is 1400 mm divided by `$scale` |
208
+ | `$nest` | `false` | pack small parts into the holes of bigger ones |
209
+ | `$registration` | `false` | print-and-cut marks in three corners of every sheet |
210
+ | `$label_parts` | `false` | engrave each part's number on its back face, as in the parts list and steps |
211
+ | `$join_lines` | `false` | join lines that meet end to end and order them nearest first, for less travel |
212
+ | `$min_width` | `1.5` | warn where a part's material is narrower than this; `0` turns it off |
213
+ | `$bounds` | | set by `measure()` for the children after the one it measures |
214
+ | `$area` | | set by `measure()`: the area of material the measured child cuts |
215
+ | `$centroid` | | set by `measure()`: the measured child's balance point `[x, y]` |
194
216
 
195
217
  ## Layers
196
218
 
@@ -257,11 +279,15 @@ material(thickness = 4, kerf = 0.12, name = "birch") {
257
279
  `$thickness`, `$kerf` and `$material` where it is called. `at = [x, y, z]`
258
280
  and `rotate = [x, y, z]` (degrees) place the part in the assembled view:
259
281
  rotated first, then moved, with its thickness along +Z. A part with copies
260
- takes one placement or a list with one per copy. `render()` returns the
261
- placed parts as `assembly` and warns when two of them collide.
282
+ takes one placement or a list with one per copy. `qty = 0` makes nothing,
283
+ so `part(qty = len(list), at = list)` needs no guard for an empty list.
284
+ `render()` returns the placed parts as `assembly` and warns when two of
285
+ them collide.
262
286
  `rotate = orient(x, z)` turns the part's own x and z axes onto two
263
287
  directions; `euler(rotation(a) * rotation(b))` turns by `b`, then by `a`.
264
288
  - More about a part, all named:
289
+ - `material = "red acrylic"`: the part's material, as `material(name =
290
+ ...)` would set `$material` round it.
265
291
  - `stack = n`: n layers glued at each placement, one thickness apart
266
292
  along the part's z. The parts list says how many make a stack.
267
293
  - `mirror = true`: the part is cut mirrored left to right (x becomes
@@ -288,6 +314,21 @@ material(thickness = 4, kerf = 0.12, name = "birch") {
288
314
  names before it took: `slots = ["*", "!back"]` cuts a slot for every
289
315
  part that passes through except the back. A slot clears every layer,
290
316
  marks included, and a part that only touches the edge leaves no notch.
317
+ - `slot_extra = [dx, dy]` sweeps each of its slots that far in this
318
+ part's own coordinates: a pegboard hook goes in through the slot and
319
+ drops, so its slot is taller than its tab.
320
+ - `halving = ["name", ...]`: where this part and the named parts cross,
321
+ each takes a slot from opposite sides, meeting halfway along the line
322
+ they cross on, `clearance` wider, so they slide together: an egg-crate
323
+ grid, or ribs notched from their outer edge onto rings slotted from
324
+ their inner rim. The way round that leaves both parts soonest is used,
325
+ and a slot stops at the part's first edge. Names take `*` and `!`.
326
+ - `bores = ["label", ...]`: where the hardware rods with those labels (or
327
+ kinds) pass through this part, holes are cut the shape of their section,
328
+ at any angle. `fit = "turn"` (the default) makes them 0.3 mm wider than
329
+ the rod for a part that turns on it, `"press"` 0.1 mm narrower for one
330
+ held fast, or give a number of millimetres. Names take `*` and `!` as in
331
+ `slots`.
291
332
  - `place(at, rotate) { ... }` places the parts and hardware inside in the
292
333
  assembled view, as part()'s `at` and `rotate` do; their own placements
293
334
  are inside it. A library module wrapped in `place()` can be set anywhere.
@@ -296,12 +337,15 @@ material(thickness = 4, kerf = 0.12, name = "birch") {
296
337
  text as `steps` and each placed part's `step`. The 3D view shows the
297
338
  model built up a step at a time. A step inside a step is a sub-step,
298
339
  `"outer: inner"`; `step(text, substeps = false)` takes inner steps in.
299
- - `hardware(kind, from, to, size = 6, up, sides = 24, label)` lists
300
- hardware the assembly needs, from one point to another: a dowel, a screw,
301
- a magnet, paper. `size` is a diameter, or `[w, h]` for a flat section
302
- whose `h` points along `up` (by default up, or along y for something
303
- standing upright). A round one is drawn with `sides` sides, 3 to 128.
304
- `render()` returns it as `hardware`, and `kerf check` lists it.
340
+ - `hardware(kind, from, to, size = 6, up, sides = 24, shape = "rod", label)`
341
+ lists hardware the assembly needs, from one point to another: a dowel, a
342
+ screw, a magnet, paper. `size` is a diameter, or `[w, h]` for a flat
343
+ section whose `h` points along `up` (by default up, or along y for
344
+ something standing upright). A round one is drawn with `sides` sides, 3
345
+ to 128. `shape = "ball"` (or `sides = 0`) is a ball of diameter `size`
346
+ midway between `from` and `to`; `to` may be left out. It comes back with
347
+ `sides = 0` and `from` and `to` both at its centre. `render()` returns it
348
+ as `hardware`, and `kerf check` lists it.
305
349
  - Shapes outside any part form one part named `part`.
306
350
  - `material(thickness, kerf, name) { ... }` sets those special variables for
307
351
  its children.
@@ -320,22 +364,24 @@ export examples.
320
364
 
321
365
  ### Shapes
322
366
 
323
- | Module | Notes |
324
- | ---------------------------------------------------------- | ---------------------------------------------------------------------------- |
325
- | `rect(size, center = false, r = 0)` | `size` is a number or `[w, h]`; `r` rounds the corners. `square` is an alias |
326
- | `circle(r)` or `circle(d = ...)` | |
327
- | `ellipse([rx, ry])` | |
328
- | `regular_polygon(n, r)` | corner radius `r` |
329
- | `star(n, r, inner)` | |
330
- | `polygon(points, paths)` | even-odd fill, `paths` lists point indexes per outline |
331
- | `path(d, fill = "evenodd", close = false)` | SVG path data; closed subpaths become shapes, open ones lines unless `close` |
332
- | `text(text, size = 10, font, halign, valign, spacing = 1)` | `halign`: left, center, right; `valign`: baseline, top, center, bottom |
333
- | `text(text, size, fit = width)` | no wider than `fit`: the size shrinks to fit, never above `size` if given |
334
- | `line(from, to)` | a line, not an area |
335
- | `line(from, to, width)` | a strip `width` wide between the points, an area |
336
- | `spline(points, width, steps = 8, closed = false)` | a smooth curve through the points; with `width`, an area along it |
337
- | `polyline(points, closed = false)` | |
338
- | `arc(r, start = 0, end = 90)` | arc line, angles in degrees |
367
+ | Module | Notes |
368
+ | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
369
+ | `rect(size, center = false, r = 0)` | `size` is a number or `[w, h]`; `r` rounds the corners. `square` is an alias |
370
+ | `circle(r)` or `circle(d = ...)` | |
371
+ | `ellipse([rx, ry])` | |
372
+ | `regular_polygon(n, r)` | corner radius `r` |
373
+ | `star(n, r, inner)` | |
374
+ | `polygon(points, paths)` | even-odd fill, `paths` lists point indexes per outline |
375
+ | `path(d, fill = "evenodd", close = false)` | SVG path data; closed subpaths become shapes, open ones lines unless `close` |
376
+ | `text(text, size = 10, font, halign, valign, spacing = 1)` | `halign`: left, center, right; `valign`: baseline, top, center, bottom |
377
+ | `text(text, size, fit = width)` | no wider than `fit`: the size shrinks to fit, never above `size` if given |
378
+ | `text(text, size, weight = "bold")` | strokes thickened by 3.5% of the size, or by a number of mm (less than 0 thins) |
379
+ | `text(text, size, bridge = w)` | every island (an i's dot, an accent, a stencil's counter) joined to the rest by a strip `w` wide, for cut-out and stencil lettering |
380
+ | `line(from, to)` | a line, not an area |
381
+ | `line(from, to, width)` | a strip `width` wide between the points, an area |
382
+ | `spline(points, width, steps = 8, closed = false)` | a smooth curve through the points; with `width`, an area along it |
383
+ | `polyline(points, closed = false)` | |
384
+ | `arc(r, start = 0, end = 90)` | arc line, angles in degrees |
339
385
 
340
386
  Supply geometry as children of a shape-processing module, for example
341
387
  `translate([10, 0]) circle(d = 5);`. Fonts used by `text()` must be supplied
@@ -356,21 +402,27 @@ playground, the command line and `kerfscript.wasm` have `Atkinson`
356
402
 
357
403
  ### Combining
358
404
 
359
- | Module | Notes |
360
- | ---------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
361
- | `union()` / `group()` | joins children |
362
- | `difference()` | removes every later child's area from every layer of the first child |
363
- | `intersection()` | keeps the area common to all children; lines of any child are clipped to the others' area, and a child of lines alone leaves only lines |
364
- | `fill_holes(min_area)` | fills holes smaller than `min_area` mm², or all of them |
365
- | `close(r = 0.5)` | closes gaps and notches narrower than `2 r`, keeping the outline |
366
- | `pattern(spacing, stagger = 0)` | repeats its first child over the area of the others, clipped at the edge |
367
- | `hatch(angle = 45, pitch = 5, width)` | parallel lines across the children's area, or stripes `width` wide |
368
- | `paint(layers)` | each child loses the area of every later one; child i goes on `layers[i]`. A child wholly on an area of its own layer warns |
369
- | `measure(draw = false)` | `$bounds` = `[x0, y0, x1, y1]` of the first child, for the children after |
370
- | `hull()` | convex hull; each named part inside gets its own hull |
371
- | `offset(r)` / `offset(delta, chamfer = false)` | grow (positive) or shrink areas; `r` rounds corners |
372
- | `stroke(width)` | turns lines into areas of that width, for cutting single line art |
373
- | `outline()` | turns areas into their outline lines |
405
+ | Module | Notes |
406
+ | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
407
+ | `union()` / `group()` | joins children |
408
+ | `difference()` | removes every later child's area from every layer of the first child |
409
+ | `intersection()` | keeps the area common to all children; lines of any child are clipped to the others' area, and a child of lines alone leaves only lines |
410
+ | `fill_holes(min_area)` | fills holes smaller than `min_area` mm², or all of them |
411
+ | `join_islands(width = 1)` | joins every separate piece of the material to the nearest other piece with a strip `width` wide, until it is one piece |
412
+ | `largest(n = 1)` | keeps the `n` biggest connected pieces of the material, dropping crumbs; marks stay as drawn |
413
+ | `close(r = 0.5)` | closes gaps and notches narrower than `2 r`, keeping the outline |
414
+ | `pattern(spacing, stagger = 0)` | repeats its first child over the area of the others, clipped at the edge |
415
+ | `hatch(angle = 45, pitch = 5, width)` | parallel lines across the children's area, or stripes `width` wide |
416
+ | `flow(field, spacing = 2, step, min_length, seed = 1)` | evenly spaced lines following `field(p)` (degrees, a vector, or `undef` to stop) across the children's area; see [Plotting](#plotting) |
417
+ | `contours(field, levels = 10, interval, cell = 1)` | lines where `field(p)` crosses each level across the children's area: `levels` evenly spaced ones or a list, or every `interval` |
418
+ | `occlude()` | children back to front: lines and outlines hide behind later children's areas, fills lose them. Each iteration of a `for` is a child |
419
+ | `hide()` | inside `occlude()`, an area that hides what is behind it and is not drawn |
420
+ | `paint(layers)` | each child loses the area of every later one; child i goes on `layers[i]`. A child wholly on an area of its own layer warns |
421
+ | `measure(draw = false)` | `$bounds` = `[x0, y0, x1, y1]`, `$area` and `$centroid` = `[x, y]` of the first child, for the children after |
422
+ | `hull()` | convex hull; each named part inside gets its own hull |
423
+ | `offset(r)` / `offset(delta, chamfer = false)` | grow (positive) or shrink areas; `r` rounds corners |
424
+ | `stroke(width)` | turns lines into areas of that width, for cutting single line art |
425
+ | `outline()` | turns areas into their outline lines |
374
426
 
375
427
  ### Joinery
376
428
 
@@ -390,6 +442,57 @@ playground, the command line and `kerfscript.wasm` have `Atkinson`
390
442
  | `children(index)` | Render a child passed to the current module; omit `index` to render all children |
391
443
  | `call(name, ...)` | Call the module named by the string `name` with the other arguments and the children: `call(entry.draw, 40, 30)` |
392
444
 
445
+ ### Plotting
446
+
447
+ Line art drawn the way pen plotters draw, for score and engrave lines.
448
+
449
+ `flow(field)` grows lines both ways from seeds until they leave the area,
450
+ the field stops, or they come within half of `spacing` of another line or
451
+ of themselves; new lines start `spacing` to either side of each line, so
452
+ they fill the area evenly. `field` is one angle, or a function of the point
453
+ `p` giving degrees, a vector `[x, y]`, or `undef` where lines stop. Another
454
+ `seed` gives another arrangement.
455
+
456
+ ```kerf
457
+ part("Panel") {
458
+ rect([120, 80], r = 5);
459
+ score() flow(function(p) 180 * noise(p / 40, seed = 3, octaves = 2), spacing = 2)
460
+ offset(-5) rect([120, 80], r = 5);
461
+ }
462
+ ```
463
+
464
+ `contours(field)` samples `field(p)` every `cell` mm across the area and
465
+ draws where it crosses each level, as a map's height lines.
466
+
467
+ ```kerf
468
+ part("Coaster") {
469
+ circle(d = 90);
470
+ score() contours(function(p) noise(p / 30, seed = 7, octaves = 3), interval = 0.08)
471
+ circle(d = 82);
472
+ }
473
+ ```
474
+
475
+ `occlude()` draws its children back to front as if each were opaque. Lines
476
+ and the outlines of line layers are cut where a later child's area covers
477
+ them; filled layers lose that area, as in `paint()`; cut and guide layers
478
+ are unchanged. `hide()` gives a child an area that covers without being
479
+ drawn. Stacked ridgelines, nearest last:
480
+
481
+ ```kerf
482
+ ridge = function(k) [for (x = [0:2:100]) [x, k * 3 + 12 * max(0, noise(x / 15, k / 5))]];
483
+ part("Ridges") {
484
+ translate([-5, -5]) rect([110, 75]);
485
+ occlude() for (k = [19:-1:0]) {
486
+ score() polyline(ridge(k));
487
+ hide() polygon(concat(ridge(k), [[100, -1], [0, -1]]));
488
+ }
489
+ }
490
+ ```
491
+
492
+ Set `$join_lines = true` to join lines that meet end to end into one and
493
+ put them in nearest-first order, each started from its end nearer the
494
+ last, so the laser head travels less between them.
495
+
393
496
  ## Built-in functions
394
497
 
395
498
  Functions return values. Arguments can be positional or named with the
@@ -411,6 +514,7 @@ arguments.
411
514
  | `min(values)`, `max(values)` | Minimum or maximum of a list; also accept separate values |
412
515
  | `clamp(x, low, high)` | Limit x to the given interval |
413
516
  | `lerp(a, b, t)` | Interpolate between numbers or vectors; t = 0 gives a, t = 1 gives b |
517
+ | `train_for(ratio, stages = 2, pinion, wheel)` | Tooth counts for a gear train nearest `ratio`; see kerf/gears |
414
518
  | `norm(v)` | Length of a vector |
415
519
  | `cross(a, b)` | Cross product; a scalar for 2D vectors or a vector for 3D vectors |
416
520
 
@@ -418,36 +522,42 @@ arguments.
418
522
 
419
523
  A path is a list of `[x, y]` points.
420
524
 
421
- | Function | Result |
422
- | -------------------------------------------------- | -------------------------------------------------------------------- |
423
- | `path_length(points)` | Length along the path |
424
- | `point_at(points, s)` | The point `s` along the path; past either end it carries on straight |
425
- | `heading_at(points, s)` | The path's direction `s` along it, in degrees from +x |
426
- | `path_points(d, tolerance)` | The points of SVG path data, a list of points for each subpath |
427
- | `rotation(angles)` | The 3 × 3 matrix of `[x, y, z]` degrees as `part()` turns; use `*` |
428
- | `euler(matrix)` | The `[x, y, z]` angles of a rotation matrix, for `rotate` |
429
- | `orient(x, z = [0, 0, 1])` | The angles that turn a part's own x and z axes onto two directions |
430
- | `spline_points(points, steps = 8, closed = false)` | The points of the smooth curve `spline()` draws |
525
+ | Function | Result |
526
+ | ---------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
527
+ | `path_length(points)` | Length along the path |
528
+ | `point_at(points, s)` | The point `s` along the path; past either end it carries on straight |
529
+ | `heading_at(points, s)` | The path's direction `s` along it, in degrees from +x |
530
+ | `offset_path(points, d, closed = false)` | The path moved `d` to its left (right for `d < 0`), corners mitred |
531
+ | `path_points(d, tolerance)` | The points of SVG path data, a list of points for each subpath |
532
+ | `rotation(angles)` | The 3 × 3 matrix of `[x, y, z]` degrees as `part()` turns; use `*` |
533
+ | `euler(matrix)` | The `[x, y, z]` angles of a rotation matrix, for `rotate` |
534
+ | `orient(x, z = [0, 0, 1])` | The angles that turn a part's own x and z axes onto two directions |
535
+ | `spline_points(points, steps = 8, closed = false)` | The points of the smooth curve `spline()` draws |
536
+ | `bounds(points)` | `[x0, y0, x1, y1]` round a list of points, or of outlines |
537
+ | `crossings(points, x)` or `crossings(points, y = v)` | Where a vertical (or horizontal) line crosses a closed outline, or a list of them: the y (or x) values, smallest first |
431
538
 
432
539
  ### Lists and strings
433
540
 
434
- | Function | Result |
435
- | ------------------------------ | -------------------------------------------------------------------------------------------- |
436
- | `len(value)` | Number of items in a list, characters in a string or fields in a record |
437
- | `keys(record)` | A record's field names, in the order they were written |
438
- | `merge(a, b)` | A record with the fields of both, `b`'s winning |
439
- | `concat(...)` | Join lists and individual values into one list |
440
- | `reverse(value)` | Reverse a list or string |
441
- | `sum(values)` | Sum a list of numbers |
442
- | `slice(value, start = 0, end)` | Extract part of a list or string; `end` is exclusive and defaults to its length |
443
- | `str(...)` | Convert values to text and join them |
444
- | `chr(...)`, `ord(text)` | Convert Unicode code points to text, or read the first character's code point |
445
- | `upper(text)`, `lower(text)` | Change letter case |
446
- | `split(text, separator = " ")` | Split text into a list of strings |
447
- | `join(values, separator = "")` | Join values into a string |
448
- | `search(value, list)` | Index of the first equal item, or `undef` if absent; also searches characters in strings |
449
- | `contains(value, item)` | Whether a list contains an item or a string contains a substring |
450
- | `lookup(x, table)` | Interpolate in a table of `[x, y]` pairs; use the nearest endpoint outside the table's range |
541
+ | Function | Result |
542
+ | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
543
+ | `len(value)` | Number of items in a list, characters in a string or fields in a record |
544
+ | `keys(record)` | A record's field names, in the order they were written |
545
+ | `merge(a, b)` | A record with the fields of both, `b`'s winning |
546
+ | `concat(...)` | Join lists and individual values into one list |
547
+ | `reverse(value)` | Reverse a list or string |
548
+ | `sort(values, key)` | The list smallest first: numbers, strings, or lists item by item; ordered by `key(item)` if given, equal keys keeping their order |
549
+ | `fold(values, initial, fn)` | The value after `fn(acc, item)` for each item in turn, from `initial` |
550
+ | `scan(values, initial, fn)` | The value after each item as `fold()` goes: running totals, `[]` for an empty list |
551
+ | `sum(values)` | Sum a list of numbers |
552
+ | `slice(value, start = 0, end)` | Extract part of a list or string; `end` is exclusive and defaults to its length |
553
+ | `str(...)` | Convert values to text and join them |
554
+ | `chr(...)`, `ord(text)` | Convert Unicode code points to text, or read the first character's code point |
555
+ | `upper(text)`, `lower(text)` | Change letter case |
556
+ | `split(text, separator = " ")` | Split text into a list of strings |
557
+ | `join(values, separator = "")` | Join values into a string |
558
+ | `search(value, list)` | Index of the first equal item, or `undef` if absent; also searches characters in strings |
559
+ | `contains(value, item)` | Whether a list contains an item or a string contains a substring |
560
+ | `lookup(x, table)` | Interpolate in a table of `[x, y]` pairs; use the nearest endpoint outside the table's range |
451
561
 
452
562
  ### Types
453
563
 
@@ -460,6 +570,13 @@ A path is a list of `[x, y]` points.
460
570
  `rands(min, max, count, seed = 1)` returns a list of numbers between the
461
571
  bounds. The same seed gives the same sequence.
462
572
 
573
+ `noise(x, y = 0, z = 0, seed = 1, octaves = 1)` is smooth gradient noise,
574
+ about -1 to 1, that changes gradually from point to point; `x` may be a
575
+ point `[x, y]` or `[x, y, z]`, as in `noise(p / 30, seed = 4)`. It is 0 at
576
+ whole numbers, so scale the point down for broad shapes. `octaves` adds
577
+ finer layers, each half as strong at twice the detail. Step `z` to get a
578
+ related but different pattern.
579
+
463
580
  ### Text measurements
464
581
 
465
582
  `text_width(text, size, font)` returns the advance width, including glyph
package/README.md CHANGED
@@ -69,8 +69,8 @@ npx @mylasertools/kerfscript render box.kerf -o box.svg -D width=200
69
69
 
70
70
  That writes all six panels packed on a sheet, kerf compensated, as a
71
71
  millimetre SVG with one layer per operation. Output is checked automatically
72
- in Inkscape; see [docs/compatibility.md](docs/compatibility.md) for what has
73
- and has not been tested in other laser software.
72
+ in Inkscape; see [docs/compatibility.md](docs/compatibility.md) for import
73
+ settings in laser software.
74
74
 
75
75
  `kerf/box` is one of the libraries in the
76
76
  [standard library](docs/library.md), built into every renderer:
@@ -31,9 +31,10 @@ export declare function placementMatrix(p: Placement): number[];
31
31
  export declare function worldBox(p: AssemblyPart): [Vec3, Vec3];
32
32
  export declare function besideAssembly(placed: AssemblyPart[], loose: AssemblyPart[], gap?: number): AssemblyPart[];
33
33
  export declare function collisions(parts: AssemblyPart[], maxPairs?: number, eps?: number, pitch?: number, volume?: number): {
34
- found: [AssemblyPart, AssemblyPart][];
34
+ found: [AssemblyPart, AssemblyPart, Vec3][];
35
35
  complete: boolean;
36
36
  };
37
+ export declare function overlapPoint(a: AssemblyPart, b: AssemblyPart, eps?: number, pitch?: number, volume?: number): Vec3 | undefined;
37
38
  export declare function translation(x: number, y: number, z: number): number[];
38
39
  export declare const IDENTITY4: number[];
39
40
  export declare function invert(m: number[]): number[];
@@ -31,9 +31,10 @@ export declare function placementMatrix(p: Placement): number[];
31
31
  export declare function worldBox(p: AssemblyPart): [Vec3, Vec3];
32
32
  export declare function besideAssembly(placed: AssemblyPart[], loose: AssemblyPart[], gap?: number): AssemblyPart[];
33
33
  export declare function collisions(parts: AssemblyPart[], maxPairs?: number, eps?: number, pitch?: number, volume?: number): {
34
- found: [AssemblyPart, AssemblyPart][];
34
+ found: [AssemblyPart, AssemblyPart, Vec3][];
35
35
  complete: boolean;
36
36
  };
37
+ export declare function overlapPoint(a: AssemblyPart, b: AssemblyPart, eps?: number, pitch?: number, volume?: number): Vec3 | undefined;
37
38
  export declare function translation(x: number, y: number, z: number): number[];
38
39
  export declare const IDENTITY4: number[];
39
40
  export declare function invert(m: number[]): number[];
package/dist/ast.d.cts CHANGED
@@ -81,6 +81,11 @@ export type Expr = {
81
81
  body: Expr;
82
82
  pos: Pos;
83
83
  end?: number;
84
+ } | {
85
+ kind: 'chain';
86
+ check: Expr;
87
+ body: Expr;
88
+ pos: Pos;
84
89
  };
85
90
  export type ListItem = {
86
91
  kind: 'item';
@@ -153,6 +158,7 @@ export type Stmt = ({
153
158
  assigns: Assign[];
154
159
  body: Stmt[];
155
160
  pos: Pos;
161
+ intersect?: boolean;
156
162
  } | {
157
163
  kind: 'if';
158
164
  cond: Expr;
package/dist/ast.d.ts CHANGED
@@ -81,6 +81,11 @@ export type Expr = {
81
81
  body: Expr;
82
82
  pos: Pos;
83
83
  end?: number;
84
+ } | {
85
+ kind: 'chain';
86
+ check: Expr;
87
+ body: Expr;
88
+ pos: Pos;
84
89
  };
85
90
  export type ListItem = {
86
91
  kind: 'item';
@@ -153,6 +158,7 @@ export type Stmt = ({
153
158
  assigns: Assign[];
154
159
  body: Stmt[];
155
160
  pos: Pos;
161
+ intersect?: boolean;
156
162
  } | {
157
163
  kind: 'if';
158
164
  cond: Expr;
@@ -0,0 +1,5 @@
1
+ import type { HardwareItem } from './evaluator.cjs';
2
+ import { type Poly } from './geometry.cjs';
3
+ import type { Solid } from './slots.cjs';
4
+ export declare const FITS: Record<string, number>;
5
+ export declare function boreFor(target: Solid, h: HardwareItem, room: number): Poly | undefined;
@@ -0,0 +1,5 @@
1
+ import type { HardwareItem } from './evaluator.js';
2
+ import { type Poly } from './geometry.js';
3
+ import type { Solid } from './slots.js';
4
+ export declare const FITS: Record<string, number>;
5
+ export declare function boreFor(target: Solid, h: HardwareItem, room: number): Poly | undefined;