OpenSCAD Live Tutorial

Parametric design

Draw a logo in https://aggie.io/

Examples:

Mickey mouse mickey
Batman batman
Yin Yang yinyang

Try yourself:

Toyota logo toyota
Mitchubishi mitchubishi
Youtube youtube
Tinder logo tinder
Apple apple
Nike nike
Android android
Twitter twitter
XBox xbox
McDonalds mcd]

How to put a drawing into numbers?

  • Mickey
    • It’s all circles. What are the parameters of a circle?
  • Mitchubishi
    • How to parameterize triangles?

How to create more complicated shapes?

  • Tinder
  • Twitter
  • CSG Tree
    • Complicated shapes make a tree (hierarchical structure) of simple shapes.

OpenSCAD

Recreate Mickey Using the circle() and the translate() command.

Variables

Variables are not what you’re used to in imperative languages. Try:

myVariable = 42;
myOtherVariable = 1337;
myVariable = myOtherVariable;
echo(myVariable);
echo(myOtherVariable);

Lists (which are often vectors)

aVector = [1,2,3];
aListOfVectors = [[1,2,3],[4,5,6],[7,8,9]];

Transormations and transformation hierarchy

A single translation:

translate([10, 40, -30]){
    // Everything in this block is moved to [x:10, y:40, z:-30]
}

// if it's only one thing you want to move:
translate([10, 40, -30]) circle(100);

Nested translations:

translate([10, 40, -30]){
    // Everything in this block is moved to [10, 40, -30]
    translate([-10, 20, 50]){ 
        // Everything in this block is moved to:
        //      [10, 40, -30] + [-10, 20, 50] = [x:0, y:60, z:20]
    }
}

// if it's only one thing you want to move you don't need brackets:
translate([10, 40, -30]) 
        translate([-10, 20, 50])
                circle(100);

Boolean Operations

The same constructions works with operations:

difference() {
    sphere(100); // The first object in this list is going to be the positive
    translate([-50, 0, 0]) 
        cube(30); // all subequent objects are going to take chunks out of the first
}

Programmy features

For-loops and if-statements

//Create an array of spheres
for (iSphere = [0:0.5:10]) 
    // [0:0.5:10] -> a range datatype
    // At first iSphere = 0 and for each iteration grows to 10 in steps of 0.5
    translate([iSphere*10, 0, 0])
        sphere(10);

// nested for loops
for (iSphereCol = [0:0.5:10])
    for (iSphereRow = [0:0.5:10])
        translate([iSphereCol*10, iSphereRow*10, 0])
            sphere(10);

Modules


// Because
hexNutM8();
// reads way better than

difference(){
    cylinder(d=15, h=5, $fn=6);
    cylinder(d=8, h=5, $fn=50);
}

// you first have to put it in a module:

module hexNutM8(){
    difference(){
        cylinder(d=15, h=5, $fn=6);
        cylinder(d=8, h=5, $fn=50);
    }
}

// Or a reusable version for all sizes:
module hexNut(outerD=15, holeD=8, height=5){
    difference(){
        cylinder(d=outerD, h=height, $fn=6);
        cylinder(d=holeD, h=height, $fn=50);
    }
}

// defaults to M8
hexNut();
// or can be any size
hexNut(200, 300, 500);

Modelling

2D

square(); // which is almost never a square
circle(); // which is sometimes not a circle
polygon(); //which is always a polygon

3D

cube(); // which is almost never a cube
sphere(); // which is sometimes not a sphere
cylinder(); // which can also be a (truncated) cone

Some interesting ones:


// build geometry that encompass the list of objects drawn in it's scope
hull() {
    hull(){
    sphere(100);
    translate([300, 0, 200])
    sphere(100);
}

// Also works in 2d, great for rounded corners
hull(){    
    translate([-100, 100, 0]) circle(30);
    translate([-100, -100, 0]) circle(30);
    translate([100, -100, 0]) circle(30);
    translate([100, 100, 0]) circle(30);
}

// minkowsky also works but is a bit harder to grasp
minkowski()
{
  square([10,10]);
  circle(r=2);
}

extrude

Any 2d geometry can be extruded:

linear_extrude(h=10){
    circle(100);
    square(100);
}

Comments and directives

  • $fn
    • determines the curve approximation
  • #
    • Makes anything drawn after it visible in the output

Why OpenSCAD?

  • Commandline support (snowstorm)
  • Customizer
  • Webserver integration (Example: thingiverse customizer)
  • Randomness - Create something slightly different every time
  • Interoperability
    • Data physicalisation, data import
    • Blender export
    • Excel export
    • Text editor workflow (sublime, vscode)
    • surface import
    • text file
    • image
    • stl import
  • Hybrid workflow:
    • prebuild complicated shapes in visual CAD
    • Customize with openSCAD

Why not?

  • complicated one-off shapes that don’t need customization
  • visual trial and error.
    • I often try complicated things in another CAD package first
  • aestetically pleasing shapes
    • Lots of mathematics
  • organic shapes
  • If you dont like a rectangle to be called a square, an ellipsoid a sphere, etc…
  • if you have trouble finding names for everything

Time left?

  • Try to recreate some of the logos in OpenSCAD

OpenSCAD OpenSCAD cheatsheet Commandline