Three ways to write a model
A model in Xtrůd is a Sprout program. There is more than one good way to write one, and most real models mix them. Here is the same plate written three ways, and when each is the one to reach for.
1 · A cell of features
A part is a cell, and each line in it is one feature with a name. They are built from the top down, and the tree beside the view shows the same names in the same order. A later line can use an earlier one by its name: row copies first-hole.
width : 120 depth : 60 thick : 8 holes : 5 plate : part "Plate" slab : extrude [round-rect [width, depth, 8], thick] first-hole : hole [[15 - (width / 2), 0], 6, on : top + thick] row : pattern [first-hole, x, holes, width - 30 / (holes - 1)] foot : extrude [circle [6, at : [12 - (width / 2), depth / 2 - 12]], 10, reverse : true] feet : pattern [foot, x, 2, width - 24, second-direction : z, second-count : 2, second-spacing : depth - 24]
Good for: a part made once, where you think "then a hole, then a row of them, then feet". Every feature can be clicked, rolled back and changed on its own line. This is how the hinged bracket and the L-bracket are written.
Its limit: copies come from pattern, circular and mirror, which only know rows, rings and reflections. When things sit where a rule puts them, write the rule.
2 · Steps first: work the places out, then build
Here the places are worked out before the part, as lists, and the features take the lists whole. hole drills every point it is given; a field of a part can hold a list of features made in a loop.
width : 120 depth : 60 thick : 8 holes : 5 -- where things go, worked out first hole-places : range holes collect [number | [number - 1 * (width - 30 / (holes - 1)) + 15 - (width / 2), 0]] foot-places : grid [2, 2, width - 24, depth - 24] plate : part "Plate" slab : extrude [round-rect [width, depth, 8], thick] row : hole [hole-places, 6, on : top + thick] feet : foot-places collect [place | extrude [circle [6, at : place], 10, reverse : true]]
It builds exactly the same plate (the tests build both and compare them to the cubic millimetre). What changes is where the thinking is: in the cell style it is in the features, in this one it is in the numbers above them.
Good for: anything placed by a rule: holes on a bolt circle (ring), a grid with one missing, panels counted from the roof's length, steps of a stair. The fourplex's solar panels and stair treads are written this way.
Its limit: a list of features shows in the tree as feet 1, feet 2, and a list inside a list is not opened up for you in a part, so loop once over one list.
3 · Words: when a thing is made more than once
A word is a cell with inputs. It can give back a number, a point, a shape, a feature, or a whole part. When something is made twice with different numbers, give it a name and its inputs.
thick : 8 -- a plate of any width, with as many holes as it has room for plate-of : [width | part "Plate [width]" slab : extrude [round-rect [width, 60, 8], thick] row : hole [range (width / 30 floor) collect [number | [number - 0.5 * 30 - (width / 2), 0]], 6, on : top + thick] ] short : plate-of 90 long : plate-of 180
The fourplex's four front doors are one word, front-door, used four times with four colours. The vocabulary's own bolt, nut and washer are words that give back parts, written in Sprout like these.
Good for: families of parts, repeated details (an opening through a wall, a tube of a frame), and anything you would otherwise copy and edit.
Its limit: a part made by a word belongs to the line that named it, so its features all point at that one line.
Which, when
| You are thinking | Write |
|---|---|
| "then this, then that" | a cell of features |
| "one every so far", "as many as fit", "where the angle lands" | places first, as a list; then one feature that takes the list |
| "another one like that, but bigger" | a word with inputs |
| "what does it weigh, will it fit, is it rated" | a number at the top level (its value shows at the end of its line) and a check |
Names carry most of the meaning. Whole plain words that someone would guess: direction, not dir; wall-thickness, not wt; and not a long compound that has to be looked up.
Things that bite
- Sprout reads strictly left to right.
a + b * 2is(a + b) * 2. Put the right side of a+or-in brackets when it is more than one word:width / 2 - (thick * 2). - A word with several inputs takes a list,
block [from, to]. A word with one input takes it bare,nut 8,increase visits. - A list inside a list is opened up in an assembly but not in a part. In a part, loop once.
- Your names win. Calling a number
lengthorangleis fine, but then the vocabulary's word of that name is hidden from the lines below it. rightandleftbelong to Sprout, which is why the third plane is calledside.- A word Sprout does not know gives false and a note, and the model carries on. The note is marked on its line; read it before wondering where a feature went.