Skip to main content

Extruding 2d Shapes

Let get 3 dimensional#

We've looked at this 2d shape long enough, we can give it some depth by extruding it, linear_extrudeing to be precise, let's add a new variable hingeLength too.

// ... other variables abovehingeLength=30;linear_extrude(hingeLength){  offset(1)offset(-2)offset(1){    translate([0,pivotRadius,0]){      circle(pivotRadius);    }    square([pivotRadius,pivotRadius]);    translate([pivotRadius,0,0]){      square([baseWidth,baseThickness]);    }  }}

Live Demo

We do have a bit of a problem though because while want the base to go the full length of the hinge, the pivot should only go half that to make room for the other part of the hinge. Red scribbles shows what we want to remove.

The best way around this problem is to extrude the base again separately so to stretch further than the pivot.

// ... variables abovelinear_extrude(hingeLength/2){  offset(1)offset(-2)offset(1){    translate([0,pivotRadius,0]){      circle(pivotRadius);    }    square([pivotRadius,pivotRadius]);    translate([pivotRadius,0,0]){      square([baseWidth,baseThickness]);    }  }}linear_extrude(hingeLength){  offset(1)offset(-1)translate([pivotRadius,0,0]){    square([baseWidth,baseThickness]);  }}

Live Demo

Math Operations#

Great, starting to take shape, couple things to go over, first you may have noticed that we just did some inline math with hingeLength/2. This works fine, normal math operations can be preformed and it's fine to mix variables with numbers (also 2 here doesn't count as a magic number since hingeLength/2 is easy to read as "half of hingeLength".

You may notice that we've included the code that forms the hinge base:

translate([pivotRadius,0,0]){  square([baseWidth,baseThickness]);}

within both of the linear_extrudes, at first it might seem like we could remove it from first linear_extrude since it only goes half the hingeLength but this would cause us to loose our internal fillet.

As it needs to be within offset group for this fillet to work.

Okay so we leave it in both linear_extrudes but this leaves us in a situation similar to before we introduced variables, in that we have repeated code that would be difficult to determine why to someone reading our code. Well similar to variables we can solve this with modules.