/ / navigation

Introduction
NYS Standards

Introduction



Modules are basically functions that you can execute with or without parameters in order to render a model. They allow you to define and reuse code.

To define a module:
module myModuleName(){

}

To call a module:
myModuleName();




The code below defines a module that takes parameters (arguments):
// parameters are: 
// width: 1 =standard 4x4 duplo width.
// length: 1= standard 4x4 duplo length
// height: 1= minimal duplo height
// nibbles: true or false

 
 
module duplo(width,length,height,nibbles) {
    //size definitions
 
    ns = 8.4;  //nibble start offset
    no = 6.53; //nibbleoffset
    nbo = 16; // nibble bottom offset
 
    duplowidth = 31.66;
    duplolength=31.66;
    duploheight=9.6;
    duplowall = 1.55;    
 
 
    //the cube
    difference() {
        cube([width*duplowidth,length*duplolength,height*duploheight],true);
        translate([0,0,-duplowall])     
            cube([width*duplowidth - 2*duplowall,length*duplolength-2*duplowall,height*duploheight],true);
    }
 
    //nibbles on top
         if  (nibbles)
            {
            for(j=[1:length]){
            for (i = [1:width]){
                translate([i*ns+(i-1)*no,j*ns+(j-1)*no,6.9+(height-1)*duploheight/2]) duplonibble();
                translate([i*-ns+(i-1)*-no,j*ns+(j-1)*no,6.9+(height-1)*duploheight/2]) duplonibble();
                translate([i*ns+(i-1)*no,j*-ns+(j-1)*-no,6.9+(height-1)*duploheight/2]) duplonibble();
                translate([i*-ns+(i-1)*-no,j*-ns+(j-1)*-no,6.9+(height-1)*duploheight/2]) duplonibble();
            }
        }
    }
 
    //nibble bottom
    for(j=[1:length]){
        for (i = [1:width]){
            translate([(i-1)*nbo,(j-1)*nbo,0]) duplobottomnibble(height*duploheight);
            translate([(i-1)*-nbo,(j-1)*-nbo,0]) duplobottomnibble(height*duploheight);
            translate([(i-1)*-nbo,(j-1)*nbo,0]) duplobottomnibble(height*duploheight);
            translate([(i-1)*nbo,(j-1)*-nbo,0]) duplobottomnibble(height*duploheight);
        }
    }
    //little walls inside[http://www.thingiverse.com/Domonoky Domonoky]
    difference(){
        union(){
            for(j=[1:length]){    
                for (i = [1:width]){
                    translate([0,j*ns+(j-1)*no,0 ]) cube([width*duplowidth,1.35,height*duploheight],true);
                    translate([0,j*-ns+(j-1)*-no,0 ]) cube([width*duplowidth,1.35,height*duploheight],true);
                    translate([i*ns+(i-1)*no,0,0 ]) cube([1.35,length*duplolength,,height*duploheight],true);
                    translate([i*-ns+(i-1)*-no,0,0 ]) cube([1.35,length*duplolength,height*duploheight],true);
                }
            }
        }
        cube([width*duplowidth - 4*duplowall,length*duplolength-4*duplowall,height*duploheight+2],true);
    }
}
 
 
module duplonibble(){
    difference() {
        cylinder(r=4.7,h=4.5,center=true,$fs = 0.01);
        cylinder(r=3.4,h=5.5,center=true,$fs = 0.01);
    }
}
 
module duplobottomnibble(height){
    difference() {
        cylinder(r=6.6,h=height,center=true,$fs = 0.01);
        cylinder(r=5.3,h=height+1,center=true,$fs = 0.01);
    }
 
}
Read through the code above and create a duplo brick where the width and length are both 1 standard unit and the height is 2 standard units. The model should have nibbles.

This is the dimensions of a lego brick (Duplo doubles the dimensions):
300px-Approximate-lego-dimensions.png

Here is another example of how you might want to use modules:
module TearDrop(){
	union(){
		cylinder(h=200,r=20, center=true);
		intersection(){
			rotate(45,[0, 0, 1])
			cube([40, 40, 200], center=true);
			translate([55, 0, 0])
			cube([80, 40, 200],center=true);
		}
	}
}
difference(){
	cube([50, 300, 100],center=true);
	translate([0, -100, 0])
	rotate(-90,[0, 1, 0])
	TearDrop();
	rotate(-90,[0, 1, 0])
	TearDrop();
	translate([0, 100, 0])
	rotate(-90,[0, 1, 0])
	TearDrop();
}



Here is the same code, but with parameters:
module TearDrop(radius){
	union(){
		cylinder(h=200,r=radius, center=true);
		intersection(){
			rotate(45,[0, 0, 1])
			scale([1,1,20])
			cube(size=2*radius, center=true);
			translate([1.75*radius, 0, 0])
			scale([1,1,20])
			cube(size=2*radius,center=true);
		}
	}
}
difference(){
	cube([50, 300, 100],center=true);
	translate([0, -100, 0])
	rotate(-90,[0, 1, 0])
	TearDrop(10);
	rotate(-90,[0, 1, 0])
	TearDrop(20);
	translate([0, 100, 0])
	rotate(-90,[0, 1, 0])
	TearDrop(30);
}



Objective: Using Modules create a parametric module so that a use can pass in dimensions and a scale.

 
 $("p:not(p:eq(2))").css("border","2px solid pink").css("padding", ".5em");