Basic shapes in openSCAD

So lets start with a few disclaimers, firstly open source software can have a very fast update rate, so things I write here while working now may not necessarily work in the future if something big changes in openSCAD. secondly I am running fedora 22, a version of Linux, at the moment so it may occasionally be that something I do is specific to fedora or Linux, I will try to avoid doing so but let me know if you do spot me doing so and I can update things.

OK so lets get started. So if you open up openSCAD you will be greeted with the following window

OpenSCAD welcome screen

OpenSCAD welcome screen

 

The window is reasonably straight forward with a few example projects on the right, and recent projects on the left, for now lets just click on the new button.

main window in openSCAD

main window in openSCAD

So a brief description of the main parts of the window, firstly on the left is the editor window from which you will control openSCAD, unlike the other CAD packages I have used openSCAD works more like a programming language, and as such you have a text box on the left to control what you would like to make. The main window (top right window) is where the model view, and allows you to look at what you are programming, pan and zoom around so on. Finaly on the bottom right is the console window that acts as an output log.

So lets start of with a simple shape for you to create, lets start with a simple sphere. In the editor window enter the following text


sphere();


 

now hit the F5 key to preview the code in the model view, you should now have  a small blob at the centre of the axis but it probably doesnt look much like a sphere and it will be very small. don’t worry we can fix that, first of all lets fix the size of the sphere, inside the brackets we can tell the sphere how big to be. This can be done in several ways, first you could tell it to have a fixed radius by putting r=5  to set the radius to 5 mm, or if you would rather work with diameters we can use d=10 to set the spheres diameter instead. Now if you hit F5 again you should see that your sphere has gotten a lot bigger but still looks a little rough, this is because in openSCAD you can set the resolution of objects, this can be done with the $fn flag, now unlike setting the diameter/radius of the sphere we have a few options as to where this is defined. We can either define this globally (to all objects that you create) or make it specific to the object that you are defining. To define the resolution globally we add an additional line as follows


$fn = 30;
sphere(r=5);


Now it doesn’t matter if this line is above or below the sphere due to a quirk or openSCAD, rather than variables being set as they are run all parameters are set at the same time, which makes sense as you are defining an object not writing an interactive program, but non the less is something that can catch you out if you re-use variable names. One of the exceptions to this is that you can define a variable in a function that can be over written for example if we tell the sphere to have a higher resolution for just this sphere it wont overwrite any global setting on other spheres. To do this we can include r=5,$fn=30 within the brackets of the sphere object like we did when setting the radius of the sphere.

OK so now we know how to make a sphere, but what other types of object can openSCAD make? well the two other main object types are cubes and cylinder. Cubes in openSCAD really means a cuboid, as all the dimensions (x,y,z) are able to be set. To make a basic cube of size 1 along x, 2 along y and 3 along z we type:


cube([1,2,3]);


Note that the dimensions of the cube are within a set of square brackets, this is an important thing to remember for later on, and also remember that you are setting the size of the cube here so negative numbers will not make sense to the program (I will talk you through moving the objects around at a later point). As a default a cube object will be placed so that one corner touches origin (where x,y and z are equal to 0) and that it extends in the positive direction in x,y and z. we can however also tell the cube that we would rather it be placed so that its centre is at the origin instead. Again we do this inside the brackets by changing the inside of the brackets to [1,2,3],center=true , now this is a slight annoyance to those of us who code and speak English English as opposed to American English but you have to remember to miss-spell centre here.

And finally lets move on to cylinders, now for cylinders normally you need to define a radius (or diameter) and a height this is done by setting values for r,d and h. So to make a cylinder 10 units high, and 30 units in diameter we write


cylinder(r=30,h=10);


Again for cylinders we will want to keep an eye on the setting for the resolution $fn, but this can have some interesting advantages for example lets say that you wanted to make an object with a hexagonal top and bottom faces and square side faces such as a nut, or a place to fit a nut into. This can be done easily by setting $fn=6 for a cylinder object.

Now another thing that you can do with cylinders is set the radii of the top and bottom faces separately giving you a section of a cone, or indeed a code. To do this rather than defining the width or diameter of the cylinder using r or d we set them using r1,r2 or d1,d2, where r1 and d1 define the size of the bottom circle. So lets put this all together


cylinder(r1=30,r2=0,h=30,$fn=6);


6 sided pyramid

6 sided pyramid

so this makes a 6 sided pyramid, of hight 30 and bottom radius of 30. Now that’s all for the basic shapes, but should be enough to act as a starting point for most things, I will discuss custom polygons at a later date as these a a little more complicated to define. So next step will be to see how to move objects around and rotate them.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *