2014-03-09

OpenSCAD: 3D Model Programming

In a recent trip to the local FabLab, I learned about a cool 3D modeling tool unlike most others.  In OpenSCAD, you write code to make models.

It is really cool and fast to make very precise 3D models.

Here's a quick code sample that defines a procedure to make cones, then calls it!
To run it in openSCAD:
F5: quick visualize
F6: compile and check (note, this code is only to demo the programming, it cannot generate a proper STL file for 3D printing)

// cone construction defs
r_upper=0.5;
r_lower= 20;
// the next var determines how 'smooth' the curves are
definition=100;

// cone placement defs
cone_height=20;
cone_h_step=10;
cone_step= 2*r_lower;

// this makes a single cone of 'height' tall,
// at [0,0,0]
module cone(height){
  cylinder(h=height,
           r1=r_lower,
           r2=r_upper,
           $fn=definition);
}

// this makes 'nb' cones,
// each is taller than the previous,
// while shifting them along the x-axis
// note:this code will not make STL, because of 'holes'
module make_cones(nb){
  for (i= [0:nb-1]){
    translate([cone_step*i,0,0])
      cone(cone_height+(cone_h_step*i));
  }
}

// this does the work
make_cones(3);

Here's the Result:
Cool, eh?

No comments: