Wednesday, October 30, 2013

OpenSCAD - 3-D modeling script language

I had been looking for some easy-to-use 3-D CAD software which runs on Linux, and is preferably open-source. The goal is to design and build stuff like this Do It Yourself Book Scanner, where all the pieces can be cut out of a 4x8ft sheet of plywood or maybe thick plexiglass.

I had initially passed over OpenSCAD because it seemed that it was more about assembling 3-D renderings from 2-D CAD files. However, I had cause to look at it again this week, and I like it a lot better this time.

The basic idea is that you create a script that draws out 3-D primitives (cube, sphere, cylinder, etc.) and add / subtract them to make objects. What follows is an example.

Here is a small module for a part.

module flange() {
  difference() {
    translate([1,0,-1]) cube([8,4,2]);
    translate([7,2,-1.1]) cylinder(h=2.2,r=1,$fn=100);
  }
  translate([-1,2,-1]) cube([2,2,2]);
  translate([-3,0,-1]) cube([2,4,2]);
}

Here's the code to actually call the module to render it.

color("LightBlue") flange();

And this is what it looks like:

The modules can be called multiple times, for example here to make interlocking parts:

rotate(a=[180,-90,0]) translate([0,-4,0]) flange();

Both of them rendered looks like this:

Where this is becoming handy (at least for me) is that I can create a module which renders the finished assembly, with all the component parts moved into the proper place for the design, similar to the above, and see how all the cut-out parts will fit together.

It is then easy enough to use the same components and have them all laid out in a single plane. I can then convert the drawing to DXF, open it in LibreCAD, set the actual dimensions, and go!

That is very neat. Overall, this is quite a different experience than what you get with a traditional CAD environment, but it more suits me as a programmer. Conditionals, math formulas, loops and such are possible with OpenSCAD, so that makes some kinds of designs much easier.

No comments: