# OpenSCAD for the newbie 3d printer

Well there are plenty of new things to get to grips with when learning to use a 3D printer, but before long you will have various setting dialled in and be happy with the latest dust gather from thingiverse that you’ve managed to print.  However its so much more rewarding and useful to be able to design your own models to print.

There are numerous different ways to design 3D models but, for ease of use and absolute precision OpenSCAD can really get you up and running very quickly.  The real bonus is that even if you are not a coder it’s a real buzz to see you code come to actual physical life, now I did mention coding but fear not with a few simple tricks in your bag even the least confident programmer can find OpenSCAD most rewarding.

The fist thing to mention is that the “stable” release version is practically out of the ark! Look for the “nightly” version which is a snap shot of the latest development version, don’t worry so far its been very stable for me and just works…

Lets use a very simple command just to introduce a few ideas key to coding in this environment.

`cube([3,2,1],center=true);`

Unsurprisingly the cube command (or “action”) creates a cube, if you look in the help menu the cheat sheet option is an invaluable aide-mémoire.  The size is defined by an array of three values (the square brackets), and although you don’t have to name parameters, it often makes code more readable for when you come back to it later, especially for boolean values…

The cube command is an example of an “action”, these usually create some kind of object and end with a semicolon ( ; ) , in order to manipulate actions we use operators before the action, these do not need the finishing semicolon.

`translate([0,4,.5]) rotate([0,0,45]) cube([3,2,1]);`

As you can see you can have multiple operators one after the other effecting an action, its worth mentioning that translate (movement) and rotate operators are said to be “non-commutative” which is just a fancy way to say that a movement followed by a rotate will have a different effect than a rotate followed by a translation.

```for ( a = [0:60:359] ) {
rotate([0,0,a]) translate([0,4,.5]) rotate([0,0,45]) cube([3,2,1]);
}```

A loop is very powerful and can for example allow you to have multiple rotated features or arrays of objects, the values in the for loop specify, the start value, the step or amount the variable changes each loop, and finally the end value, the loop ends if the next value would exceed this value.

#### Constructive Solid Geometry

This is one of the most powerful features of OpenSCAD and allows you to accurately create very complex and featureful objects – in tradition of CSG lets have a look at a simple example that features possibly the most over used shape in the CAD world!

```difference() {
cube(1,center=true);
sphere(.65,center=true,\$fn=32);
}```

The difference operator subtracts all the following actions from the first action (don’t forget you can offset or rotate these actions within the difference operator, the \$fn variable is worth mentioning, you can either set this globally (effecting everything) at the top of your code, or you can pass it (with different values) to each action, setting this tells OpenSCAD the number of faces you want to use for an action.

As general guidance most things are quite happy when printed with a \$fn value of 32 when printed, occasionally you might need to go up to say 128 for larger features (remembering that a large number of faces will take longer to render later) very occasionally you might need a higher value, for example if you are taking the edge of a scaled sphere out of an object to make a slight dish shape.

Related to difference there are the union and intersection operations, union “welds” multiple objects together and intersection leaves only material where all the objects overlap.

Don’t forget that you can have for example a union as the first action in a difference operator, this can all get quite complex (not to mention powerful) which reminds me to mention that its very important to comment your code, possibly even more than when writing an application, take the time to label each of the features of your work so you can later see what is going on, especially if the feature is a none visible “tool” you are using to carve a shape out of the model you are working on.

```difference() {

union() {
cylinder(r=10,r2=4,h=10,\$fn=8);
translate([0,0,10]) sphere(4.35,\$fn=8);
}

for (a=[0:45:359]) {
rotate([0,0,a+22.5]) translate([0,0,10])
translate([0,3.5,0])  cube(2,center=true);
}

}```

While I’m not sure the above is actually useful for, it does show you some of the power of OpenSCAD in a small amount of code, and all this only scratches the surface of what available.  If the linear extrude operator doesn’t work out your vase phase then probably nothing will, other useful features are things like importing STL files (which you can then carve about and add features to) and lets not forget the easy ability to build up your own library of useful functions…

Even with this brief skim through the most basic of features of OpenSCAD, I’m sure you can see just how powerful it is…