Sunday, 17 November 2013

Learning OpenSCAD

I have been using openscad to make 3D models, now that I have something reliable on which to print them. It is a scripted CAD system, which is nice. It has basic primitives for 2D (circle, square, etc) and 3D (sphere, cube, cylinder), basic transformations (rotate, scale, translate), and Boolean operators (intersection, union, difference) which allow complex shapes to be combined. It also has basic program control (loops, conditionals) and variables as well as functions/modules and means to include them from files allowing libraries to be created. Overall it is pretty damn impressive.

Of course, even given the basics, one has to learn programming techniques that make sense with any new language.

There are a couple of rather fun functions, one is hull which makes a convex hull of a set of objects, and minkowski which creates a Minkowski sum of a set of objects.

Example of hull() on two spheres
The hull function is very useful for constructing more complex objects. E.g. what if you want a cylinder with rounded ends? You could make two spheres, and a cylinder jointing them, or just make a hull of the two spheres. If you are joining several simple objects using the hull function you can create quick complex objects with rounded corners and seamless joins.

Example of angular shape Minkowski with a sphere.
The other function, minkowski, I had to look up. It effectively means creating the object that is the union of one object when repositioned at an origin in every place within another object. This sounds complex, but can allow some very simple tricks. Even if you want to grow an object by a defined amount, just minkowski a sphere with the object. The only catch is that it can take a lot of time to calculate. The good news is that openscan is smart enough to cache the minkowski functions, allowing you to redraw quickly as long as you have not changed that part of the design (it may cache lost more, but this is where it is most noticeable).

Now, this growing a shape a bit trick is actually quite useful if you want to print two things that do not quite touch. For example a hinge. I printed a letter box and the hinge is a block with a cone each end, and needs a hole in the surrounding to match, but there has to be a clearance so they are not printed as one solid lump. I found 0.5mm clearance to be fine.

So I made a function for the flap with the hinge, and then used it with the difference function to make a hole in the surrounding. I used minkowski to make it 0.5mm larger all round to create the clearance, and even included versions rotated at 45 degrees and 90 degrees to ensure the hole covered the flap when open as well as closed.
I actually used a minkowski with a cube and not a sphere for the simple reason of speed. Openscad allows you to define how many parts are used to make a sphere, but even with very few the minkowski sum takes ages, and a sphere does the job fine for just making consistent clearance.

I am sure I will learn more tricks over time.

[P.S. I know that buying a letterbox from homebase would be a lot cheaper and quicker, but I would learn nothing, OK, so don't even say it]

No comments:

Post a Comment