Openscad Examples

From Hackerspace Brussels
Revision as of 02:36, 15 March 2014 by Gratefulfrog (Talk | contribs) (Using this in OpenScad)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Making 3 cones[edit]

This example will produce 3 solid cones that look like this:

3cones.png

The code comprises:

  • global variable definitions
  • module ,i.e. procedure definitions
  • calls to the procedures to execute the model.

Global Variables[edit]

// cone construction defs
// a cone is a cylinder with 2 radii, upper and lower
r_upper=0.5;
r_lower= 20;
cone_height=20;

// the next var determines how 'smooth' the curves will be, 
// i.e. the number of faces, all openscad objects can take this
definition=100;

// cone placement defs
// these variables are used to grow and place the cones
cone_h_step=10;
cone_step= 2*r_lower;

Modules[edit]

Modules are callable procedures. We will define 2 of them:

  • The first makes a single cone and places it at the origin of the coordinate system [0,0,0]
  • the second calls the first nb times, making cones and translating (moving) them along the x-axis.
// 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);   // the 'smoothness'
}

// this makes 'nb' cones,
// each is taller than the previous,
// while shifting them along the x-axis
// note:this code will not make proper STL, because of 'holes' between the cones

module make_cones(nb){
  for (i= [0:nb-1]){
    translate([cone_step*i,0,0])
      cone(cone_height+(cone_h_step*i));
  }
}

The Work-Horse Code[edit]

// this does the work
make_cones(3);

Using this in OpenScad[edit]

  1. First download the File:3conesExampleCode.txt but save it as 3cones.scad.
  2. Load it into Openscad
  3. Hit F5 to visualize the result. You should see the 3 cones.
  4. Hit F6 to compile, and see if your object is ok to become a.STL file. You will see that it is NOT ok because "simple" is "no"
Top Level object is a 3D object.
Simple:   no

To make it simple you have to reduce the cone_step so there is overlap, for example:

cone_step = r_lower-0.1;

Hope this helps someone!