Templates
A template builds a new drawing from a few dimensions: a plate with holes at its corners, a sleeve with a glue flap, your own standard boxes and parts. File › Extensions › New from Template… lists the templates, asks for the one chosen's parameters, and opens the drawing it makes.
To do the same with a drawing file instead of JavaScript, see Run Parametric.
The drawing is parametric: a template sets its parameters as variables of the drawing and defines its points on them, so it can be resized afterwards by changing the variables, like any other drawing.
Declaring one
template({
name: "Plate",
category: "General",
description: "A rectangle with a hole in the middle.",
parameters: {
length: { label: "Length", value: 300, min: 1 },
width: { label: "Width", value: 200, min: 1 },
hole: { label: "Hole diameter", value: 20, min: 0 }
},
build(p) {
setVariable("L", p.length);
setVariable("W", p.width);
setVariable("HOLE", p.hole);
const a = newPoint("[0,0]"), b = newPoint("[L,0]");
const c = newPoint("[L,W]"), d = newPoint("[0,W]");
polyline([a, b, c, d], true);
circle(newPoint("[L/2,W/2]"), "HOLE/2");
}
});
| Property | Meaning |
|---|---|
name, description |
As shown when choosing it; the new drawing's title is its name. |
category |
Groups it in the list: Packaging › Sleeve with glue flap. |
parameters |
Its options, asked for before it is built. |
build(parameters) |
Makes the drawing with the drawing API; parameters holds the values chosen, by name. |
Keeping it parametric
What a template makes is only as parametric as the way it is made:
setVariable("L", p.length)makes the parameter a variable;newPoint("[L,0]")a point defined on it. ChangeLlater and the point moves, and everything drawn from it.offset(a, "W/2", 0),polar(a, 30, "R")andbetween(a, b, 0.5)make points defined from others.circle(c, "HOLE/2"): sizes given as expressions follow their variables.- A number or
{x, y}is fixed where it is, as if typed in: right for that size, but it won't follow.
The new drawing's layer table is the packaging one: layer 0 cut, layer 1 crease, and so on; layer(n) chooses the
layer for the elements after it.
Templates that come with the app
- Plate with corner holes (General): length, width, corner radius, hole diameter and inset.
- Sleeve with glue flap (Packaging): four panels (length, width, length, width), the height, and a tapered glue flap, creased between the panels.
They are in templates.js in the app's Extensions folder.
See also: Extensions, Drawing API, Variables, Points.