Ditch OpenSCAD for C++

There’s an old saying that a picture is worth a thousand words. If you’ve ever tried to build furniture or a toy with one of those instructions sheets that contains nothing but pictures, you might disagree. 3D design is much the same for a lot of people. You think you want to draw things graphically, but once you start doing complex things and making changes, parametric modeling is the way to go. Some CAD tools let you do both, but many 3D printer users wind up using OpenSCAD which is fully parametric.

If you’ve used OpenSCAD you know that it is like a simple programming language, but with some significant differences from what you normally use. It is a good bet that most Hackaday readers can program in at least one language. So why learn something new? A real programming language is likely to have features you won’t find readily in OpenSCAD that, in theory, ought to help with reuse and managing complex designs.

I considered OpenJSCAD. It is more or less OpenSCAD for JavaScript. However, JavaScript is a bit of a scripting language itself. Sure, it has objects and some other features, but I’m more comfortable with C++. I thought about using the OpenCSG library that OpenSCAD uses, but that exposes a lot of detail.

Instead, I turned to a project that uses C++ code to generate OpenSCAD output, OOML (the Object Oriented Mechanics Language)). OpenSCAD does the rendering, exporting, and other functions. Unfortunately, the project seems to have stalled a few years back and the primary web-based documentation for it seems to be absent. However, it is very usable and if you know how to find it, there is plenty of documentation available.

Why not OpenSCAD?

Obviously, there’s nothing in OOML that you can’t do in OpenSCAD. Which is like saying you can do everything C++ does in assembly language: it is true, but kind of misses the point. First, OOML defines many more basic objects (see below for a graphic taken from a white paper about OOML). If you want, for example, a RoundedTablet, you simply create one and the final output will have the necessary OpenSCAD.

Sure, you could define that stuff with a module. There’s also a library of parts including Arduino outlines, bearings, and other useful components. But the real value is that you can bring to bear all of the C++ features to developing new shapes.

Here’s a simple example. One of the parts available is the shape of a generic ultrasonic transducer module. The class provides a constructor that lets you set the different attributes like the size of the board, the size of the sensors, and more.

There is also a part that is a specific brand of ultrasonic sensor (one from Seeed Studios). Here’s how to create one of those in an OOML C++ program: SeeedUSSensor sensor;

That’s it. Here’s the definition of the SeeedUSSensor constructor:


SeeedUSSensor(bool fill_drills = true) :
USSensor(43,20,15,2,40,17,1,16.1/2,20-16.1/2,fill_drills)
{
rebuild();
}

That’s out of context, but there’s nothing else significant in that object. All the rendering is in the USSensor base class. I could as easily make my own class for a Seeed sensor on a bracket, for example. I would just derive a new class and provide the additional rendering (or I could use any other C++ technique such as making my new object instantiate the sensor object).

Another feature that isn’t necessarily compelling is that I personally like the syntax. Here’s how you translate an object in OpenSCAD:


translate([10,10,0]) cube([5,5,10]);

Here’s the same code in OOML:


Component cube(Cube(5, 5, 10));
cube.translate(10,10,0);

If you want to take a union and a difference of some OOML objects:


result=cube1+cube2-boltholes;

Documentation is Like Oxygen

At one time there was a Wiki with documentation about the project, but it has either moved or is gone. However, the project is set up to do Doxygen documentation. It isn’t complete, but it is over 2,000 HTML files (which is probably why it isn’t on GitHub). You can generate it easily, but to save you the trouble, I forked the project and added the documentation as a ZIP file you can download.

The other way to learn about OOML is to read the source code. In particular, the test subdirectory has everything from a simple cube, to some fairly complex shapes. If you dive into the src directory you can also find examples of interesting things like the ultrasonic sensor I showed you earlier.

Usability

Having the power of C++ at your design fingertips is nice. What isn’t nice is losing the immediate design cycle you get with OpenSCAD. Being able to just tweak a number, hit F5, and repeat is pretty addicting. Using things like the octothorpe (I have too many British friends to call it a pound sign) to visualize a cut is a big loss, too.

You can’t do much about the special characters unless you read through the OpenSCAD code and find the right spot to put it (which somewhat defeats the purpose of using OOML). However, you can help get a little more immediate feedback with some setup.

All you need to do is open up the generated OpenSCAD file one time. Then make sure that “automatically reload” is checked on the Design menu. Now when you rerun your executable program and write over the OpenSCAD, the program will detect that, ask you to confirm and reload the file. Not quite as handy, but not bad.

If you are just making a simple keychain, this is probably more overhead than you want. But if you are doing a complex design with a lot of pieces that you reuse, with small differences, it can be just the thing.

Something Simple

Here is a pretty simple OOML program. I factored out the drawing code from the part that writes the output.


#include
#include
#include
#include
#include

void render(IndentWriter &writer)
{
Component obj(RoundedTablet(100,100,5,25,true,false,false,false));
Component cyl(Cylinder(25,10));
cyl.translate(22,22,-2);
writer << obj-cyl;
}


int main(int argc, char **argv)
{
IndentWriter writer;
render(writer);
std::cout << writer;
return 0;
}

What that means is you can ignore the code in main and focus on the render function. I build a RoundedTablet with only one corner rounded (that’s not well documented, but you can figure it out from the parameter names). Then I cut a cylinder out of one corner. Granted, this wouldn’t be very hard to do in OpenSCAD and it doesn’t really show off the features you could use, but I wanted something simple to let you see how it works.

You can see the resultant OpenSCAD rendering at the start of this post. Once you have it at this point, you can do anything OpenSCAD can do.

Building

Since OOML doesn’t do any of the actual graphics generation itself, it is easy to build. I built a copy under Cygwin, but it should be about the same on Linux. You do need cmake installed. Here are the basic steps:


cmake .
make
make install

I did have to play with the DLL locations a bit on Windows, but that’s nothing new. To compile my example, I issued the following command:


g++ -o had -L ../lib -I ../src had.cpp -lOOMLCore -lOOMLComponents -lOOMLParts && ./had >had.scad

Note the part after the && actually runs the resulting program and then you still have to open the had.scad file (although just once if you turn on auto reload).

Give OOML a try. It is open source, so if there’s anything missing, you could add it to your own fork of the code. While I’d start small, the real power is when you want to build complex objects or systems of objects using C++. I can imagine a base class for chess pieces, for example. Or pulling data from a database to drive object creation.


Filed under: 3d Printer hacks

from Hackaday http://ift.tt/2hNXWgu
via IFTTT