OpenScad

With OpenScad http://www.openscad.org3D objects can be created a bit special since code has to be typed in and this will then be converted into a 3D object. This sounds a bit odd, however when creating precise technical things then it is a quite effective way.

//coin
diameter1=23;
diameter2=27;
height=2;
shift=20;
diameterhole=3;
hole1pos=-8;
hole2pos=30;

difference(){
    union(){
      cylinder(h=height,r=diameter1/2);
      translate([0,shift,0])
        cylinder(h=height,r=diameter2/2);
    }
    #translate([0,hole1pos,-height/2])
      cylinder(h=height*2,r=diameterhole/2,$fn=16);
    #translate([0,hole2pos,-height/2])
      cylinder(h=height*2,r=diameterhole/2,$fn=16);
}

Pressing F5 for compile or automatic reload and compile makes it appear as 3D. Moving the mouse while left pressing its button rotates the view around one axes. While doing that pressing the shift button rotates it around the other axis.

The above creates an object with two fake coins put together to be used on shopping mall carts (in Switzerland).

Some things in the code are not so obvious:

Figure 10.4. OpenScad

openscad


After Compile and Render using CGAL the work can be exported to STL and the mesh is really nice and clean (No manual mesh fixing as often necessary with other tools). OpenScad uses Constructive Solid Geometry (CSG) to create the objects and not Vertices, Edges and Faces. It can operate with primitive objects as cube, cylinders and so on. When the work is done it can convert it into a mesh file format that uses Vertices, Edges and Faces.

If some dimension after 3D printing is not ok, simply change the parameter value and the work is done.

Note

There are two manuals that are not consistent the more complete one is https://en.wikibooks.org/wiki/OpenSCAD_User_Manual and the one when clicking help in OpenScad https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language

Splitting into different files

It is worth to split a design into different files as having all the values in on file or when creating an assembly that takes multiple parts from individual files:

include <hydroclip.scad>

color("red") 
  hydroclip();
color("blue") 
  rotate([180,0,0]) 
  hydroclip();

Note

There is include but also use. The difference is include runs the code and use not but gets the definitions.

The file hydroclip.scad being included needs a module definition as

module hydroclip(){ 
<add the commands here>
}

Libraries can be included as regular scad files without path information if there are in a place OpenScad looks into, as /usr/share/openscad/libraries/. But since the MCAD libraries are in a subdirectory the subdirectory must be added:

include <MCAD/gears.scad>
demo_3d_gears();

OpenScad libraries

When splitting the work to different files than it is worth to reuse the files also in other project. Therefore the files must be in a location that can be easily found. There are 3 ways where libraries can be found:

The installed library path /usr/share/openscad/libraries

The path per user at ~/.local/share/OpenSCAD/libraries

And finally the OPENSCADPATH environment variable that can contain more than one path.

OpenScad Hints

Rotation of the objects take the origin of the coordinate system and not the center of the objects. Therefore alway rotate the object first and then move (translate) it. This needs some discipline but removes the complexity of dealing with many coordinate systems.

Difference and object that already used difference seems to be critical when the result needs to be exported to stl for later 3D printing.

Extruding 2D to 3D

A strong feature is also extruding 2D to 3D. Not just linear extrusion is possible also rotation extrusion. There are also options as twist during extrusion.

Some advanced thing is the surface command that reads in a file that has a 2 dimensional array of different surface heights. One application would be using an image to emboss or impress it.

Variables

Variables are more like constants in OpenScad, since they can not easily be modified at runtime. To still modify them at runtime, the assign function must be used. Then a value can be assigned inside a block defined by the {} characters.

for (z = [0:3]){
  assign(angle = z*90 ){
    rotate([0,0,angle]) 
    translate([10,0,0])
      cube(4,center=true);
  }

There is also the key word function where a value can be passed and a result is returned:

function headh(d)=screw_cyl[d][1]; 

Animation

Openscad allows to animate the 3D objects.

The special variable $t holds a value between 0 to 1 and can be used to create different pictures per tick (as translation, rotation).

From a coding point of view something as

if($t==0){
  <do what needs to be done when time is 0>
}

or simply animate

translate([0,0,$t])cube(10);

or to get more effect multiply $t.

translate([0,0,10*$t])cube(10);

In View the Animate icon needs to be clicked. The animation parameters appear:

Time is the value of $t (usually nothing needs to be inserted here, since it gets overwritten once the animation runs)

FPS is the frames per second 0 and blank means stop. On every tick the 3D objects are calculated.

Steps holds how many pictures are created. 0 stops the animation other than 0 starts it.

The checkbox "Dump pictures" then creates png pictures for every step. Programs as gimp can open all files as layers and the export it to an animated gif.

Changing the View from Orthogonal to Perspective is considered.


Linurs Hosttech startpage