Notebook of Simple models with OpenSCAD

OpenSCAD is a free software to build 3D models. Unlike many other softwares, it does not come with a drag and drop interface. OpenSCAD relies on programming for building 3D models.

I have previously done some work on OpenSCAD (see here), but realized that I can have a blog with some basic commands that shows how it works. This will also help me remember the different functions and how they work when I need them later. I will keep on adding other modules that I find interesting. I am adding a list of items below for easy navigation. All shapes below uses a $fn of 50.

Hollow cube

I will add two different types of hollow cubes here. One of them has a hollow cube inside and the other is cylindrical.

N.B. Named colors can be found from link here.

module cuboid_hollow_cube(outer_size, inner_size) {
    difference() {
        // main cube
        color("FireBrick") {
            cube([outer_size, outer_size, outer_size], center=true);
        }
        
        // hollow it, add a +1 shim
        color("LightSalmon") {
            cube([outer_size + 2, inner_size, inner_size], center=true);
            cube([inner_size, outer_size + 2, inner_size], center=true);
            cube([inner_size, inner_size, outer_size + 2], center=true);
        }
    }
}

cuboid_hollow_cube(30, 17);
module cuboid_hollow_cylinder(outer_size, radius) {
    difference() {
        // main cube
        color("SeaGreen") {
            cube([outer_size, outer_size, outer_size], center=true);
        }
        
        color("PaleGreen") {
            // hollow it, add a +1 shim
            cylinder(h=outer_size+2, r=radius, center=true);
            rotate([90, 0, 0])
                cylinder(h=outer_size+2, r=radius, center=true);
            rotate([0, 90, 0])
                cylinder(h=outer_size+2, r=radius, center=true);
        }
    }
}

cuboid_hollow_cylinder(30, 11);

The only thing to note on the above is addition of ship while taking a difference. If we do not add a shim, it becomes difficult for OpenSCAD to know if we want to keep the thin layer on top or remove it. By adding shim we ensure that we take out more material than available.

Linear Extrude Text

module extrude_text(txt, height, rows, twists) {
    color("SteelBlue") {
        linear_extrude(height, twist=twists, slices=rows) {
            text(txt);
        }
    }
}

extrude_text("suturf-com.ibrave.host", 5, 1, 0);

Rotational Extrude

This type of extrusion is extensively used for making vases. We start with a 2D shape and then extrude in a circular fashion. I have just put some random points on a polygon below as an example and extruded it.

module rotate_polygon() {
    
    p0 = [0, 15];
    p1 = [20, 18];
    p2 = [30, 25];
    p3 = [35, 36];
    p4 = [40, 45];
    p5 = [42, 54];
    p6 = [40, 62];
    p7 = [30, 70];

    color("PeachPuff") {
        translate([0, 100]) {
            rotate_extrude()
                polygon([p0, p1, p2, p3, p4, p5, p6, p7]);
        }
    }
}

rotate_polygon();

Yoyo

module half_yoyo(outer_radius, inner_radius, side_length, half_mid_length, clr) {

    color(clr) {
        translate([0, 0, half_mid_length/2])
            cylinder(h=half_mid_length, r=inner_radius, center=true);

        translate([0, 0, side_length/2+half_mid_length])
            // Create a funnel    
            cylinder(h=side_length, r1=inner_radius, r2=outer_radius, center=true);
    }
}

module yoyo(outer_radius, inner_radius, side_length, mid_length, clr) {
    half_mid_length = mid_length/2;

    color(clr) {
        minkowski() {
            union() {
                half_yoyo(outer_radius, inner_radius, side_length, half_mid_length);
                // mirror
                mirror([0, 0, 90]) {
                    half_yoyo(outer_radius, inner_radius, side_length, half_mid_length);
                }
            }
            
            sphere(0.5);
        }
    }
}

yoyo(10, 3, 5, 2, "LightSalmon");

Measuring Peg

module half_cup(outer_radius, inner_radius, side_length, half_mid_length, clr) {
    color(clr) {
        difference() {
            union() {
                translate([0, 0, half_mid_length/2])
                    cylinder(h=half_mid_length, r=inner_radius, center=true);

                translate([0, 0, side_length/2+half_mid_length])
                    // Create a funnel
                    cylinder(h=side_length, r1=inner_radius, r2=outer_radius, center=true);
            }
                
            union() {
                translate([0, 0, side_length/2+half_mid_length + 1])
                    // Create a funnel
                    cylinder(h=side_length, r1=inner_radius, r2=outer_radius, center=true);
            }
        }
    }
}

module cup(outer_radius, inner_radius, side_length, mid_length, clr) {
    half_mid_length = mid_length/2;

    color(clr) {
        half_cup(outer_radius, inner_radius, side_length, half_mid_length, clr);
        // mirror
        mirror([0, 0, 90]) {
            half_cup(outer_radius, inner_radius, side_length, half_mid_length, clr);
        }
    }
}

cup(10, 3, 15, 0, "GoldenRod");

This shape is very similar to the Yo-Yo above except that we make it hollow.

Lego Bricks

module lego_brick(x_stud, y_stud, clr) {
    // Distance between studs is 8mm
    // Each stud is 4.8mm dia
    // Height is fixed 9.6mm
    
    stud_height = 1.7;
    stud_dist = 8;
    stud_dia = 4.8;
    block_height = 9.6;
    block_thick = 1.6;
    
    cube_length = x_stud * stud_dist;
    cube_depth = y_stud * stud_dist;
    
    // Hollow piece
    color(clr) {
        linear_extrude(block_height - block_thick) {
            difference() {
                square([cube_length, cube_depth], center=true);
                offset(-block_thick) {
                    square([cube_length, cube_depth], center=true);
                }
            }
        }
        
        // Put the top
        translate([0, 0, (block_height - block_thick) + block_thick/2]) {
            cube([cube_length, cube_depth, block_thick], center=true);
        }
        
        // Now for the studs
        translate([-cube_length/2, -cube_depth/2, 0]) {
            for (y=[0:y_stud-1]) {
                for (x=[0:x_stud-1]) {
                    translate([x*stud_dist + stud_dist/2, y*stud_dist + stud_dist/2, stud_height])
                        cylinder(h=block_height, d=stud_dia);
                }
            }
        }
    }
}

lego_brick(2, 4, "GreenYellow");

Box with Lid

module create_box(length, width, height, thick, clrbody, clrlid) {
    color(clrbody) {
        difference() {
            cube([length, width, height]);
            translate([thick, thick, thick]) {
                cube([length-2*thick, width-2*thick, height]);
            }
            translate([length/2, thick/2, height]) {
                sphere(r=thick);
            }
            translate([length/2, width - thick/2, height]) {
                sphere(r=thick);
            }
        }
    }
    
    color(clrlid) {
        // Create lid
        translate([-2*width, 0, 0]) {
            cube([length, width, thick]);
            translate([thick/2, thick/2, thick]) {
                linear_extrude(thick) {
                    square([length-thick, width-thick]);
                }
            }
        }
    }
}

create_box(20, 15, 5, 1, "SaddleBrown", "SandyBrown");

Ball Inside

module ball_inside(radius, ball_radius) {
    hole_radius = 0.9 * ball_radius;
    difference() {
        intersection() {
            color("green") {
                cube([radius, radius, radius], center=true);
            }
            
            color("blue") {
                sphere(r=0.7 * radius);
            }
        }
        
        cylinder(h=radius+2, r=hole_radius, center=true);
        rotate([90, 0, 0])
            cylinder(h=radius+2, r=hole_radius, center=true);
        rotate([0, 90, 0])
            cylinder(h=radius+2, r=hole_radius, center=true); 
    }
    
    // Add the ball inside
    color("red") {
        sphere(r=ball_radius);
    }
}

ball_inside(30, 15);

Most of the models above are parameterized. You should be able to change the parameter and generate other models. Given below are some Lego pieces for inspiration.

module lots_of_lego() {
    colors = ["DarkOliveGreen", 
            "DarkSlateBlue", 
            "DarkRed", "DeepPink", 
            "GreenYellow", 
            "MediumSpringGreen"];
    for (i=[0:1:5]) {
        translate([0, i*20, 0]) {
            lego_brick(i+1, 2, colors[i]);
        }
    }
}

Hope you find this useful. Ciao for now!