Skip to content

joewalnes/toybrick

Repository files navigation

toybrick()

OpenSCAD program to generate toy bricks compatible with many popular toy brick systems that I cannot name here.

The resulting models can be made real through 3D printers or CNC machines.

This is an example of how to use OpenSCAD to program 3D models and thinking in terms of constructive solid geometry. Follow along...

Intro to OpenSCAD

Before you begin, learn the 4 important concepts in my quick deck:

Ok, done that? Onwards.

What is this?

This is a very simple 3D library that has one function:

toybrick(length, width);

It outputs a 3D model.

Examples

Pssst! Did you know GitHub has an interactive 3D viewer? Mind. Blown.

Code Top Bottom
toybrick(1,1) 3D viewer 1x1-top 1x1-bottom
toybrick(4,2) 3D viewer 4x2-top 4x2-bottom
toybrick(5,3) 3D viewer 5x3-top 5x3-bottom
toybrick(6,6) 3D viewer 6x6-top 6x6-bottom
toybrick(10,2) 3D viewer 10x2-top 10x2-bottom

Implementation

Woah there. If you skipped the Programming 3D Objects with OpenSCAD deck, go back. It's really short. You need to understand the 4 fundamental concepts first.

High level

A toy brick consists of:

  • a body (main hollow brick)
  • n × m top studs
  • (n-1) × (m-1) inner tubes

That's it.

Note: The variables used for dimensions are defined in toybrick-dimensions.scad

Body

Well this is easy. Take a block (scad cube()), then hollow it out by calling difference() on another block:

module body(units_wide, units_long) {
	difference() {
		cube([
				units_wide * length,
				units_long * length,
				height]);
		translate([wall_thickness, wall_thickness, 0]) {
			cube([
					units_wide * length - wall_thickness * 2,
					units_long * length - wall_thickness * 2,
					height - wall_thickness]);
		}
	}
}
cube1 cube2 difference(cube1, cube2)
cube1 cube2 difference(cube1,cube2)
Stud

Even easier, it's just a cylinder() translated to the right place on top of a block:

module stud(unit_x, unit_y) {
	translate([
			(unit_x + 0.5) * length,
			(unit_y + 0.5) * length,
			height]) {
		cylinder(d=stud_diameter, h=stud_height);
	}
}
cylinder
cylinder
Inner tube

Another cylinder(), hollowed out using the difference() of an inner cylinder():

module tube(unit_x, unit_y) {
	translate([
			unit_x * length,
			unit_y * length,
			0]) {
		difference() {
			cylinder(d=tube_diameter, h=height - wall_thickness);
			cylinder(d=tube_diameter - wall_thickness * 2, h=height - wall_thickness);
		}
	}
}
cylinder1 cylinder2 difference(cylinder1, cylinder2)
cylinder1 cylinder2 difference(cylinder1,cylinder2)
Bringing it all together

Now we call our smaller modules to create the body(), and a for loop to add the stud()s and tube()s:

module toybrick(units_wide, units_long) {

	// Step 1: Make the body (main hollow brick)
	body(units_wide, units_long);

	// Step 2: Now loop over x,y...
	for (x=[0 : units_wide - 1], y=[0 : units_long - 1]) {

		// Top stud
		stud(x, y);

		// If beyond the first column and row, inner tube
		if (x > 0 && y > 0) {
			tube(x, y);
		}

	}

	// That's it!
}

Tada!

Code Top Bottom
toybrick(4,2) 3D viewer 4x2-top 4x2-bottom

The final code:

Building on it

Now we have a basic toybrick() we can make programs that use it to make other things.

Like a pyramid:

layers = 6;

for (i = [ 1 : layers ]) {
  translate([length * i * -1,
             length * i * -1,
             height * (i * -1 + layers)]) {
    toybrick(i * 2, i * 2);
  }
}

pyramid

3D viewer

What next?

Go read the OpenSCAD manual, look for inspiration on Thingiverse and follow me on Twitter.

ABC: Always Be Creating

About

OpenSCAD program to generate toy bricks compatible with existing systems

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published