µcad showcase
A tour of the µcad language and its tools with short examples. Each example is a complete µcad
file, which can be checked with mucad check and exported with mucad export, see
Get started.
Content
Language
Sketches and parts
A sketch creates 2D geometry, a part creates 3D geometry. Their parameters have a type or a
default value.
sketch Plate(size: Length, hole = 4mm) {
Rect(size) - Circle(d = hole);
}
part Cup(radius: Length, height: Length, wall = 2mm) {
outer = std::geo3d::Cylinder(r = radius, h = height);
inner = std::geo3d::Cylinder(r = radius - wall, h = height).translate(z = wall);
outer - inner;
}
Cup(radius = 20mm, height = 30mm);Values and units
Values have types, and quantities have units, which are converted automatically.
const WIDTH = 1.5cm + 2mm; // A length: 17mm
const ANGLE = 15°; // An angle
const COUNT = 4; // An integer
const SCALE = 50%; // A scalar: 0.5
sketch Stripes() {
Rect(width = WIDTH * SCALE, height = 2mm)
.translate(y = [1..COUNT] * 4mm)
.rotate(ANGLE);
}
Stripes();
Further types are e.g. Bool (true, false), String ("µcad"), lists ([1mm, 2mm])
and tuples with named values, e.g. a point (x = 10mm, y = 0mm).
The statements without name are the output of a sketch or part, e.g. outer - inner. A sketch
outputs 2D geometry, a part 3D geometry.
Combining shapes
Shapes are combined with operators: | is the union, & the intersection and - the difference.
Methods like translate, rotate or extrude are chained with ., a group { ... } combines
several shapes.
use std::geo3d::*;
body = Sphere(r = 12mm) & Cube(20mm);
holes = {
Cylinder(d = 8mm, h = 30mm);
Cylinder(d = 8mm, h = 30mm).rotate(x = 90°);
};
body - holes;Lists create copies
A list in place of a single value creates a copy for each value. A range [1..5] is the list of
the integers from 1 to 5.
// Five rings with radii of 10mm to 50mm.
std::geo3d::Torus([1..5] * 10mm, minor_radius = 3mm);// A plate with a hole in each corner: x and y each have two values, which makes four holes.
sketch DrillPlate(size = 100mm, hole_size = 10mm) {
l = size / 2 - hole_size;
holes = Circle(diameter = hole_size).translate(x = [-l, l], y = [-l, l]);
Rect(size) - holes;
}
DrillPlate();Initializers
An init creates a sketch or part with other parameters, e.g. a disc with its diameter instead
of its radius.
sketch Disc(radius: Length) {
init(diameter: Length) {
radius = diameter / 2;
}
Circle(radius);
}
Disc(radius = 5mm);
Disc(diameter = 20mm).translate(x = 20mm);Properties
A prop is a property of a model, which can be used from outside, e.g. a calculated value.
sketch Panel(width: Length, height: Length) {
prop area = width * height;
Rect(width, height);
}
Panel(30mm, 20mm);
mucad compile panel.mu --display model shows the model with its properties, e.g.
area [out]: Area = 600mm².
Operations
An op changes the geometry it is called on, which is @input.
op raise(height: Length) {
@input.extrude(height);
}
Rect(10mm).raise(5mm);Functions
A function calculates a value. Its parameters and its return value have types.
use std::math::PI;
/// The circumference of a circle.
fn circumference(radius: Length) -> Length {
2 * PI * radius
}
sketch Disc(radius: Length) {
prop circumference = circumference(radius);
Circle(radius);
}
Disc(10mm);Attributes
Attributes give a model further information, e.g. its color, the file to export it into or the resolution of round shapes.
#[color = "red"]
#[export = "disc.svg"]
#[resolution = 200%]
Circle(r = 10mm);Conditions
if and else choose the geometry.
sketch Hole(size: Length, round: Bool) {
if round {
Circle(d = size)
} else {
Rect(size)
}
}
// A red circle
#[color = "red"]
Hole(10mm, round = true).translate(x = -5mm);
// A blue rectangle
#[color = "blue"]
Hole(10mm, round = false).translate(x = 5mm);Modules and the standard library
A mod groups symbols, pub makes them usable from outside, and use imports them.
mod shapes {
pub sketch Square(size: Length) {
Rect(size);
}
}
use shapes::Square;
Square(1cm) | shapes::Square(5mm).translate(x = 1cm);
The standard library std has modules for 2D shapes (std::geo2d, e.g. Circle, Rect,
Text), 3D shapes (std::geo3d, e.g. Cube, Sphere, Cylinder, Torus), operations
(std::ops, e.g. translate, rotate, extrude, revolve) and maths (std::math, e.g. PI,
X, Y, Z).
Common symbols like Circle, Rect, translate or extrude can be used without use.
Documentation and checks
Comments starting with /// document the symbol after them, //! documents the file or module.
std::assert stops with an error, if a condition is false.
//! A frame for pictures.
/// A square frame with a thickness.
sketch Frame(size: Length, thickness: Length) {
std::assert(thickness < size / 2, "The frame is too thick");
Rect(size) - Rect(size - 2 * thickness);
}
Frame(50mm, 5mm);Tools
| Command | Description |
|---|---|
mucad check model.mu | Check a file for errors and warnings |
mucad export model.mu | Export a model into SVG (2D) or STL (3D) files |
mucad export model.mu --dry-run | List the files an export would write |
mucad watch model.mu | Export a model again whenever a source file changes |
mucad format model.mu | Format a file in the µcad style |
mucad create model | Create a new file from a template |
mucad compile model.mu --display model | Show the model tree with its properties, also ast, ir, library |
- Viewer:
mucad-viewer model.mushows a model and updates it, whenever the file changes. - Playground: the playground compiles and shows µcad code in the browser, with an editor, automatic updates and exports to STL and SVG. The playground is a hosted instance of the web viewer, which can be run locally.
- Editors: the µcad language server shows errors and warnings, completes code and formats it, e.g. in Zed and VS Code.