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.

Prior rendering the 3D object is as it is designed, when showing edge this can be observed. Other views as wireframe is just possible after rendering.

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.

A practical method for a library is having two files (as used in C). One file to include having all the constants and an other file to use having the modules. In the file to use additional code to test, show and present the modules can be added. This code will show the 3D rendered modules when the scad file is opened but has no effect when it is included in an other scad file using use.

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 be modified at runtime.

Important

Also "for loops" will not overwrite the variable, they just create an other variable having the same name in an other name space (inside {}).

As example the following produces the warning: WARNING: Ignoring unknown variable 'l'. since l is just known within {}

for (small=[0,1]){
  if(small==0){
    l=10;
  }else{
    l=5;
  }
  translate([small*15,0,0])cube([l,l,l]);
} 

A solution is using a function

function l(small)=(small==1)? 5:10;
for (small=[0,1])
translate([small*15,0,0])cube([l(small),l(small),l(small)]); 

Or an other example:

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

Customizer

The customizer is disabled by default.

Often it is necessary to do small modification to a scad design as rotating for best 3d printing. The customizer allows this without editing the scad file by modifying the variables in the customizer gui. Special formatting of those variables in the scad file adapts the gui of the customizer.

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 startpage