Code Mode basics
Learn the fundamentals of creating 3D models with code in CADit using TypeScript and the Manifold library.
Code Mode basics
This guide covers the fundamentals of creating 3D models programmatically in CADit. You will learn the basic structure of a code project, how to create primitives, and how to combine shapes with boolean operations.
Prerequisites
- Basic familiarity with JavaScript or TypeScript
- Understanding of 3D coordinates (X, Y, Z)
The basic structure
Every code project needs to export a result variable containing the 3D geometry you want to render:
import { Manifold } from 'manifold-3d/manifoldCAD';
const cube = Manifold.cube([20, 20, 20], true);
export const result = cube;
The export const result line is required. Without it, nothing appears in the 3D preview.
Understanding the import
The Manifold object provides all the methods for creating and manipulating 3D geometry. Import it from manifold-3d/manifoldCAD at the top of your file.
Units
All dimensions in Code Mode are in millimeters. A cube with size [20, 20, 20] is 20mm on each side.
Creating primitives
Cube (box)
Create a rectangular box with specified dimensions:
// Cube with size [width, height, depth]
const box = Manifold.cube([30, 20, 10]);
// Centered cube (second parameter = true)
const centeredBox = Manifold.cube([30, 20, 10], true);
Without the center parameter, the cube starts at the origin (0, 0, 0) and extends in the positive X, Y, and Z directions. With center set to true, the cube is centered at the origin.
Cylinder
Create a cylinder with specified height and radius:
// Cylinder with height 20mm and radius 10mm
const cylinder = Manifold.cylinder(20, 10);
// Cylinder with different top and bottom radii (cone shape)
const cone = Manifold.cylinder(20, 10, 5);
// Cylinder with more segments for smoother curves
const smoothCylinder = Manifold.cylinder(20, 10, 10, 64);
Cylinders are created along the Z axis by default. The optional fourth parameter controls the number of circular segments (default is 32).
Sphere
Create a sphere with specified radius:
// Sphere with radius 15mm
const sphere = Manifold.sphere(15);
// Smoother sphere with more segments
const smoothSphere = Manifold.sphere(15, 64);
The second parameter controls the number of segments used to approximate the sphere surface.
Transformations
Move, rotate, and scale shapes using transformation methods.
Translate (move)
Move a shape along the X, Y, and Z axes:
const cube = Manifold.cube([10, 10, 10]);
// Move 20mm along X, 10mm along Y, 5mm along Z
const movedCube = cube.translate([20, 10, 5]);
Rotate
Rotate a shape around the X, Y, and Z axes. Rotation values are in degrees:
const cylinder = Manifold.cylinder(20, 5);
// Rotate 90 degrees around the X axis (tips the cylinder on its side)
const rotatedCylinder = cylinder.rotate([90, 0, 0]);
Scale
Scale a shape by factors along each axis:
const sphere = Manifold.sphere(10);
// Scale 2x in X, 1x in Y, 0.5x in Z (creates an ellipsoid)
const scaledSphere = sphere.scale([2, 1, 0.5]);
Boolean operations
Combine shapes using boolean operations to create geometry.
Subtract
Remove one shape from another:
const cube = Manifold.cube([30, 30, 30], true);
const cylinder = Manifold.cylinder(35, 8);
// Create a cube with a cylindrical hole through it
const cubeWithHole = cube.subtract(cylinder);
export const result = cubeWithHole;
Subtract removes the volume of the second shape from the first.
Add (union)
Combine two shapes into one:
const cube = Manifold.cube([20, 20, 10], true);
const cylinder = Manifold.cylinder(15, 8).translate([0, 0, 10]);
// Combine the cube and cylinder into a single shape
const combined = cube.add(cylinder);
export const result = combined;
Intersect
Keep only the volume where two shapes overlap:
const cube = Manifold.cube([20, 20, 20], true);
const sphere = Manifold.sphere(15);
// Keep only the overlapping volume
const intersection = cube.intersect(sphere);
export const result = intersection;
2D shapes and extrusion
Create 2D shapes with CrossSection, then extrude them to 3D.
Circle
import { CrossSection } from 'manifold-3d/manifoldCAD';
const circle = CrossSection.circle(10);
// Extrude to 3D (5mm tall)
const cylinder = circle.extrude(5);
export const result = cylinder;
Square
import { CrossSection } from 'manifold-3d/manifoldCAD';
// Square with size [width, height]
const square = CrossSection.square([20, 15]);
// Centered square
const centeredSquare = CrossSection.square([20, 15], true);
const box = centeredSquare.extrude(10);
export const result = box;
2D transformations
CrossSection shapes support 2D transformations before extrusion:
import { Manifold, CrossSection } from 'manifold-3d/manifoldCAD';
const circle = CrossSection.circle(5);
// Move the circle 20mm along X before extruding
const movedCircle = circle.translate([20, 0]);
// Extrude the moved circle
const offsetCylinder = movedCircle.extrude(10);
export const result = offsetCylinder;
Example: Creating a bracket
This example combines multiple techniques to create an L-shaped bracket with a mounting hole:
import { Manifold } from 'manifold-3d/manifoldCAD';
// Create the vertical part of the bracket
const vertical = Manifold.cube([20, 5, 40]);
// Create the horizontal part
const horizontal = Manifold.cube([20, 30, 5]);
// Combine them
const bracket = vertical.add(horizontal);
// Create a mounting hole
const hole = Manifold.cylinder(10, 4).translate([10, 15, 0]);
// Subtract the hole from the bracket
const bracketWithHole = bracket.subtract(hole);
export const result = bracketWithHole;
Running your code
The code editor runs your code automatically as you type, with a brief delay to avoid running incomplete code. The 3D preview updates to show the result.
If your code has errors:
- TypeScript errors appear as red underlines in the editor
- Runtime errors appear in the error panel below the editor
- The 3D preview shows the last successful result until you fix the error
Tips
- Use autocomplete by typing
Manifold.to see available methods - Start with basic primitives and build up complexity
- Test each step by checking the 3D preview before adding more operations
- Use centered shapes (
trueas second parameter) when combining shapes at the origin
Next steps
- Code Mode overview - Learn about the full code editor features
- Multi-file projects - Organize code across multiple files
- Parameters - Create parametric designs with adjustable values