Blocks and control flow
Loops (FOR, FOREACH, WHILE), choices (IF) and functions of your own, to make geometry that a single command can't: a row of holes as many as fit, a pattern round a circle, a part made of repeated features.
They are part of the drawing like every other command: they are saved in the drawing file as you write them, and
they stay parametric. A block runs again, from the start, every time the drawing changes: change L, move a
point, or change a variable it uses, and what it made is made again to suit.
BEGIN … END
The statements a loop repeats, or a choice picks, go between BEGIN and END:
FOR I = 1 TO 5 BEGIN
P10 = [I * 50, 100]
P11 = P10 + [0, 40]
LINE P10, P11
END
- One statement to a line, as everywhere else; or several on a line, separated by
;:FOR I = 1 TO 5 BEGIN P10 = [I * 50, 100] ; P11 = P10 + [0, 40] ; LINE P10, P11 END. This is a quick way to type a block in the Interactive window (or press Shift+Enter there to start a new line). - A statement can go on over the end of a line inside brackets:
( ),[ ],{ }. //starts a comment, to the end of the line: on a line of its own, or after a statement.- Blocks go inside blocks to any depth.
How a block runs
Inside a block, statements run one after another, as a program does:
- An assignment sets the variable or point to its value now:
N = N + 1adds one to N, andP10 = [I * 50, 100]puts P10 where it is for this time round the loop. - An element (LINE, ARC, CIRCLE, TEXT, FILL …) is made there and then, from the points as they are: the next time round the loop can move P10 without moving the line already made.
- A name with a position can be used where an element wants a point:
LINE Q, Rdraws from the position Q to the position R.
A block sees the drawing's variables and points, but what it sets is its own: P10 set in a block is not the
drawing's P10, and L = 100 inside a block changes L for the rest of the block only. Setting #CL (the current
layer) chooses the layer of the elements the block makes from then on.
It sees the drawing's elements too, so it can work along them: ONELEMENT(@12,I/N), LENGTHOF(@12) and the
other element functions inside a block follow the element as it changes (the
Stitch Line Tool puts its holes along a line this way).
What a block makes
What a block makes is drawn, exported and printed with the drawing, each curve on its own layer, as a part's are, with its element's line width and its BRIDGES, TOOL and RULE settings (see Element settings). It belongs to the block, though: its curves aren't elements of the drawing, so they can't be picked, snapped to or dimensioned one by one. Selecting the block selects all of it. Its text is drawn as outlines.
The statements
| Statement | Does |
|---|---|
| FOR | A block for each of a run of numbers. |
| FOREACH | A block for each of some points or values. |
| WHILE | A block again and again, while something is true. |
| IF | A block, or another, or another: the first whose condition is true. |
| FUNCTION | A function of your own: a value worked out, or geometry made, or both. |
| FUNCTION … JAVASCRIPT | A function of your own written in JavaScript. |
| CALL | A function's geometry drawn. |
BREAK |
Leaves the loop it is in. |
CONTINUE |
Goes on to the next time round the loop it is in. |
RETURN |
Leaves a function (with its value: RETURN expression), or a block. |
When something goes wrong
A block typed in the Interactive window runs at once, and if something goes wrong (a name it doesn't know, a point that isn't a position), the reason is shown with the line it went wrong on, and the block isn't added.
A loop that never ends stops after a million statements; a function that calls itself without end stops 100 calls deep. A block that goes wrong later (when the drawing has changed) draws what it made before it went wrong.