UsingOpenSCAD

From Clothbot

Jump to: navigation, search

Contents

Using OpenSCAD

This page is to hold my notes on using OpenSCAD.

Syntax

Comments

Comments are escaped with the // character prefix.

// This is a comment

Variable Declarations

Variables are defined using <name> = <value> ; syntax.

overlap = 10;

Module Declarations

Modules with no arguments:

module peg()
{
	union() {
		translate([0,0,320-overlap]) cylinder(h=180+overlap, r=245, center=false);
		cylinder(h=320, r=245+55, center=false);
	}
}
module thread_hole()
{
	translate([0,0,-overlap]) cylinder(h=180+320+2*overlap, r=100, center=false);
}


A module with one argument:

module peg_hole(shift_bottom=200)
{
	translate([0,0,-overlap]) union() {
		cylinder(h=180+320+2*overlap, r=150, center=false);
		translate([0,0,overlap-1+shift_bottom])
			cylinder(h=250+1, r1=250+1, r2=0, center=false);
		cylinder(h=overlap+shift_bottom, r=250, center=false);
	}
}

Module declarations can contain sub-module declarations:

module disc_button()
{
	module bottom_disc_threadholes(shift_middle=5)
	{
		union() {
		translate([0,0,200+shift_middle]) cylinder(h=130+shift_middle,r=500,center=false);
		translate([400,0,0]) thread_hole();
		translate([-400,0,0]) thread_hole();
		translate([0,400,0]) thread_hole();
		translate([0,-400,0]) thread_hole();
		}
	}
	module bottom_disc()
	{
		difference() {
			cylinder(h=320, r=800, center=false);
			bottom_disc_threadholes(shift_middle=0);
		}
		translate([400, 400, 0])
			peg();
		translate([400, -400, 0])
			peg();
		translate([-400, -400, 0])
			peg();
		translate([-400, 400, 0])
			peg();
	}
	module bottom_disc_holes()
	{
		translate([400, 400, 0])
			peg_hole(shift_bottom=180+overlap);
		translate([400, -400, 0])
			peg_hole(shift_bottom=180+overlap);
		translate([-400, -400, 0])
			peg_hole(shift_bottom=180+overlap);
		translate([-400, 400, 0])
			peg_hole(shift_bottom=180+overlap);
	}
	difference() {
		bottom_disc();
		bottom_disc_holes();
	}
}

Modules are called/instantiated:

disc_button();

Primitive Shapes

Cylinders and Cones

Simple cylinder; origin at center:

cylinder(h=100, r=10);

Simple cylinder; origin at h=0 end:

cylinder(h=100, r=10, center=false);

Tapered cylinder:

cylinder(h=100, r1=10, r2=20, center=false);

Cone (degenerate tapered cylinder case):

cylinder(h=100, r1=10, r2=0, center=false);
Personal tools