Create a sketch for the base
Now, we want to turn the construction of the Lego brick base into a reusable, parametric component. In µcad, a reusable, parametric component that produces or transforms a geometry is called workbench.
There are three kinds of workbenches:
- sketches: produce 2D geometry, e.g.
Rect. - parts: produce 3D geometry, e.g.
Sphere. - op: Turn some input geometry into output geometry, e.g.
translate,unionorsubtract.
Definition of our first sketch
Let's encapsulate the construction of the frame into a sketch workbench called Base.
use std::geo2d::*;
use std::ops::*;
sketch Base(width: Length, height: Length, thickness = 1.2mm) {
frame = Frame(width, height, thickness);
struts = Ring(outer_d = 6.51mm, inner_d = 4.8mm)
.translate(x = [-1..1] * 8mm);
frame | struts;
}
Base(width = 31.8mm, height = 15.8mm);
If we examine the syntax of the above example, we can see the following things:
- Names of sketches are commonly written in
PascalCase, starting with a capital letter. - The sketch
Basehas 3 parameterswidth,heightandthickness. Together they are called the building plan ofBase. widthandheighthave the typeLengthand no default value, they are required.thicknessis also of typeLength, but implicitly, because we have defined a default value1.2mmwhich is aLengthof unitmm.- The body
{ ... }ofBaseconstructs the actual geometry. Base(width = 15.8mm, height = 31.8mm)is a call of the sketch.
And the best part: We don't even need additional value stores for our measures like thickness, width etc.
Every measure has a meaningful name in the parameters.
This makes the code clearer and changes easier.
An analogy to natural language
In the previous sections, we have been introduced to main concepts of µcad. If we draw an analogy to natural language, we can summarize:
- The workbenches
Base,FrameandCircleact like a noun, the subject of the sentence - it's the geometry being described or manipulated. - The operation
translatefunction like verbs, indicating operations being applied to the geometry. - The parameters
x = 20mmand45°serve as adverbs, specifying how the operations are carried out. - Groups
{}serve as subclauses. - Assignments
a = Rect(...)are used to give things a unique name:ais a rectangle.
This analogy helps illustrate how the µcad syntax is designed to be both readable and logical, resembling the structure of natural language in a way that makes the code easier to understand.
Now, we have seen all concepts to actually design our Lego brick in 3D.