You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A block is a nested list of instructions, contained inside `{ ... }` braces. Some commands, such as [builders](builders.md) or [CSG operations](csg.md), accept a block parameter instead of a simple value like a number or [vector](literals.md#vectors-and-tuples).
5
+
6
+
Instructions inside a block are executed within the [scope](scope.md) of the command that invoked them. Typically that means that any transforms or material changes made inside the block will only apply to geometry created inside the same block. This also applies to any symbols that you define inside the block.
7
+
8
+
You can define your own blocks using the `define` command. Here is a block that creates a five-pointed star:
9
+
10
+
```swift
11
+
define star {
12
+
path {
13
+
for1 to 5 {
14
+
point 0-0.5
15
+
rotate 1/5
16
+
point 0-1
17
+
rotate 1/5
18
+
}
19
+
point 0-0.5
20
+
}
21
+
}
22
+
```
23
+
24
+
You can call it by simply referencing its name, like this:
25
+
26
+
```swift
27
+
star
28
+
```
29
+
30
+

31
+
32
+
**Note:** there is a subtle distinction between the code above and the code below:
33
+
34
+
```swift
35
+
define star path {
36
+
for1 to 5 {
37
+
point 0-0.5
38
+
rotate 1/5
39
+
point 0-1
40
+
rotate 1/5
41
+
}
42
+
point 0-0.5
43
+
}
44
+
```
45
+
46
+
In the original code, we defined a new block symbol that creates a star-shaped path. In the code above we've defined a symbol whose value is a star-shaped path. The former code is evaluated at the point when it is *called*, whereas the latter code is evaluated at the point when it is *defined*.
47
+
48
+
The end-result is the same in this case, so it may seem like the distinction doesn't matter, but the advantage of the former approach is that we can add *options* to vary the behavior of the code when it is called.
49
+
50
+
## Options
51
+
52
+
To add an option to a block, you use the `option` command. This works in a similar way to the [define](symbols.md) command, but it allows the specified value to be overridden by the caller.
53
+
54
+
The code below extends the `star` definition with options for the radius and number of points:
55
+
56
+
```swift
57
+
define star {
58
+
option radius 1
59
+
option points 5
60
+
path {
61
+
for1 to points {
62
+
point 0-0.5
63
+
rotate 1/ points
64
+
point 0-radius
65
+
rotate 1/ points
66
+
}
67
+
point 0-0.5
68
+
}
69
+
}
70
+
```
71
+
72
+
Now we can use those options to create a star with 6 points if we choose:
0 commit comments