OpenSCAD User Manual/Commented Example Projects

From Wikibooks, open books for an open world
Jump to: navigation, search

Dodecahedron[edit]

//create a dodecahedron by intersecting 6 boxes
module dodecahedron(height) 
{
        scale([height,height,height]) //scale by height parameter
        {
                intersection(){
                        //make a cube
                        cube([2,2,1], center = true); 
                        intersection_for(i=[0:4]) //loop i from 0 to 4, and intersect results
                        { 
                                //make a cube, rotate it 116.565 degrees around the X axis,
                                //then 72*i around the Z axis
                                rotate([0,0,72*i])
                                        rotate([116.565,0,0])
                                        cube([2,2,1], center = true); 
                        }
                }
        }
}
//create 3 stacked dodecahedra 
//call the module with a height of 1 and move up 2
translate([0,0,2])dodecahedron(1); 
//call the module with a height of 2
dodecahedron(2); 
//call the module with a height of 4 and move down 4
translate([0,0,-4])dodecahedron(4);
The Dodecahedron as rendered from the example.

Bounding Box[edit]

// Rather kludgy module for determining bounding box from intersecting projections
module BoundingBox()
{
        intersection()
        {
                translate([0,0,0])
                linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                projection(cut=false) intersection()
                {
                        rotate([0,90,0]) 
                        linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                        projection(cut=false) 
                        rotate([0,-90,0]) 
                        children(0);

                        rotate([90,0,0]) 
                        linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                        projection(cut=false) 
                        rotate([-90,0,0]) 
                        children(0);
                }
                rotate([90,0,0]) 
                linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                projection(cut=false) 
                rotate([-90,0,0])
                intersection()
                {
                        rotate([0,90,0]) 
                        linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                        projection(cut=false) 
                        rotate([0,-90,0]) 
                        children(0);

                        rotate([0,0,0]) 
                        linear_extrude(height = 1000, center = true, convexity = 10, twist = 0) 
                        projection(cut=false) 
                        rotate([0,0,0]) 
                        children(0);
                }
        }
}

// Test module on ellipsoid
translate([0,0,40]) scale([1,2,3]) sphere(r=5);
BoundingBox() scale([1,2,3]) sphere(r=5);


Bounding Box applied to an Ellipsoid

Linear Extrude extended use examples[edit]

Linear Extrude with Scale as an interpolated function[edit]

 //Linear Extrude with Scale as an interpolated function
 // This module does not need to be modified, 
 //  - unless default parameters want to be changed 
 //  - or additional parameters want to be forwarded (e.g. slices,...)
 module linear_extrude_fs(height=1,isteps=20,twist=0){
   //union of piecewise generated extrudes
   union(){  
       for(i = [ 0: 1: isteps-1]){
           //each new piece needs to be adjusted for height
           translate([0,0,i*height/isteps])
             linear_extrude(
               height=height/isteps,
               twist=twist/isteps,
               scale=f_lefs((i+1)/isteps)/f_lefs(i/isteps)
             )
               // if a twist constant is defined it is split into pieces
               rotate([0,0,-(i/isteps)*twist])
                 // each new piece starts where the last ended
                 scale(f_lefs(i/isteps))
                   obj2D_lefs();
       }
   }
 }
 // This function defines the scale function
 //  - Function name must not be modified
 //  - Modify the contents/return value to define the function
 function f_lefs(x) = 
   let(span=150,start=20,normpos=45)
   sin(x*span+start)/sin(normpos);
 // This module defines the base 2D object to be extruded
 //  - Function name must not be modified
 //  - Modify the contents to define the base 2D object
 module obj2D_lefs(){ 
   translate([-4,-3])
     square([9,12]);
 }
 //Top rendered object demonstrating the interpolation steps
 translate([0,0,25])
 linear_extrude_fs(height=20,isteps=4);
 linear_extrude_fs(height=20);
 //Bottom rendered object demonstrating the inclusion of a twist
 translate([0,0,-25])
 linear_extrude_fs(height=20,twist=90,isteps=30);
Example Linear Extrude of a rectangle with scale following part of a sine curve function


Linear Extrude with Twist as an interpolated function[edit]

 //Linear Extrude with Twist as an interpolated function
 // This module does not need to be modified, 
 //  - unless default parameters want to be changed 
 //  - or additional parameters want to be forwarded (e.g. slices,...)
 module linear_extrude_ft(height=1,isteps=20,scale=1){
     //union of piecewise generated extrudes
     union(){
         for(i = [ 0: 1: isteps-1]){
             //each new piece needs to be adjusted for height
             translate([0,0,i*height/isteps])
               linear_extrude(
                 height=height/isteps,
                 twist=f_left((i+1)/isteps)-f_left((i)/isteps),
                 scale=(1-(1-scale)*(i+1)/isteps)/(1-(1-scale)*i/isteps)
               )
                 //Rotate to next start point
                 rotate([0,0,-f_left(i/isteps)])
                   //Scale to end of last piece size   
                   scale(1-(1-scale)*(i/isteps))
                     obj2D_left();
         }
     }
 }
 // This function defines the twist function
 //  - Function name must not be modified
 //  - Modify the contents/return value to define the function
 function f_left(x) = 
     let(twist=90,span=180,start=0)
     twist*sin(x*span+start);
 // This module defines the base 2D object to be extruded
 //  - Function name must not be modified
 //  - Modify the contents to define the base 2D object
 module obj2D_left(){
    translate([-4,-3]) 
      square([12,9]);
 }
 //Left rendered object demonstrating the interpolation steps
 translate([-20,0])
 linear_extrude_ft(height=30,isteps=5);
 linear_extrude_ft(height=30);
 //Right rendered object demonstrating the scale inclusion
 translate([25,0])
 linear_extrude_ft(height=30,scale=3);
Example Linear Extrude of a rectangle with twist following part of a sine curve function

Linear Extrude with Twist and Scale as interpolated functions[edit]

 //Linear Extrude with Twist and Scale as interpolated functions
 // This module does not need to be modified, 
 //  - unless default parameters want to be changed 
 //  - or additional parameters want to be forwarded
 module linear_extrude_ftfs(height=1,isteps=20,slices=0){
     //union of piecewise generated extrudes
     union(){  
       for(i=[0:1:isteps-1]){
         translate([0,0,i*height/isteps])
           linear_extrude(
             height=height/isteps,
             twist=leftfs_ftw((i+1)/isteps)-leftfs_ftw(i/isteps), 
             scale=leftfs_fsc((i+1)/isteps)/leftfs_fsc(i/isteps),
             slices=slices
           )
             rotate([0,0,-leftfs_ftw(i/isteps)])
               scale(leftfs_fsc(i/isteps))
                 obj2D_leftfs();
       }
     }
 }
 // This function defines the scale function
 //  - Function name must not be modified
 //  - Modify the contents/return value to define the function
 function leftfs_fsc(x)=
     let(scale=3,span=140,start=20)
     scale*sin(x*span+start);
 // This function defines the twist function
 //  - Function name must not be modified
 //  - Modify the contents/return value to define the function
 function leftfs_ftw(x)=
     let(twist=30,span=360,start=0)
     twist*sin(x*span+start);
 // This module defines the base 2D object to be extruded
 //  - Function name must not be modified
 //  - Modify the contents to define the base 2D object
 module obj2D_leftfs(){
      square([12,9]);
 }
 //Left rendered objects demonstrating the steps effect
 translate([0,-50,-60])
 rotate([0,0,90])
 linear_extrude_ftfs(height=50,isteps=3);
 
 translate([0,-50,0])
 linear_extrude_ftfs(height=50,isteps=3);
 //Center rendered objects demonstrating the slices effect
 translate([0,0,-60])
 rotate([0,0,90])
 linear_extrude_ftfs(height=50,isteps=3,slices=20);
 
 linear_extrude_ftfs(height=50,isteps=3,slices=20);
 //Right rendered objects with default parameters
 translate([0,50,-60])
 rotate([0,0,90])
 linear_extrude_ftfs(height=50);
 
 translate([0,50,0])
 linear_extrude_ftfs(height=50);
Example Linear Extrude of a rectangle with twist and scale following part of a sine curve function