Tag Archives: OpenSCAD

Using AngelScript CSG

Download AngelScript CSG version V2.0-02, with IDE included
Windows 64bit here.
Linux (K)ubuntu 15.10 64bit here.

AngelScript CSG is a work in progress, see the previous articles on this blog. Today's article is providing practical information on installing and using the software.

Windows 64bit

The AngelScript CSG setup package will install the script compiler as_csg.exe and the editor/IDE as_ide.exe. ​​ However, before installing the setup package, please make sure to install the Visual C++ Redistributable Packages for Visual Studio 2013, issued by Microsoft. You will also need a recent version of OpenSCAD installed on your system.

Linux (K)ubuntu 15.10 64bit

The angelscript_csg.tar.gz contains the binaries for AngelScript CSG script compiler as_csg and the editor/IDE as_ide. Extract the contents to a suitable folder ~/angelscript_csg is recommended. ​​ You will also need a recent version of OpenSCAD installed on your system. If you extracted to folder ~/angelscript_csg, add this to bottom of your ~.bashrc file:

 export PATH=$PATH:~/angelscript_csg

export LD_LIBRARY_PATH==LD_LIBRARY_PATH=:~/angelscript_csg

A small taste of the AngelScript language

If you are familiar with C, C++ or similar languages the learning curve will be short and painless. AngelScript is similar in most respects. A relatively complete description of the general language is found here, but as a short primer here are a few key points to know

  • The language is strongly typed, variables must be declared with a type as in C or C++

  • Line comment lines are preceded by double slash //

  • Comment blocks begin with /* ​​ and end with */

  • There are two forms of objects, reference types and value types.

The value types are like the primitives

 int i  ​​​​ ​​ = 0;  ​​ ​​ ​​ ​​ ​​ ​​​​ // an integer value type

doble pi = 3.13159; ​​ // a floating point value type

The reference types uses object handles. Object handles are used to hold references to other objects. When calling methods or accessing properties on a variable that is an object handle you will be accessing the actual object that the handle references, just as if it was an alias.

 double radius = 3;

 sphere@ s ​​  = sphere(radius);  ​​ ​​ ​​ ​​ ​​​​ // handle to a sphere

cylinder@ s = cylinder(10,radius); ​​ // handle to a cylinder

cylinder@ s2 = @s;    // 2nd handle to same cylinder

  • Functions are defined as in C/C++, returning value types or reference types

double sum(double a, double b)


 ​​ ​​ ​​​​    ​​​​ return a+b;


solid@ sum(cylinder@ c, sphere@ s)
   ​​​​ return union3d(c,s);

  • Arrays may be defined for value types or reference types

int[] iarr = {0,1,2,3,4,5};

solid@[] sarr = { cylinder(10,3), sphere(3) };


// build a growing array of spheres with increasing radius

sphere@[] spheres;

for(int i=0; i<10; i++) spheres.push_back(sphere(i));


// report the size of the array to terminal (answer will be 10)

cout << spheres.size() << endl();

The above is just a small taste of the language. If it catches your interest, you may want to look at the the full language description for more details. Remember also that withing the AngelScript CSG IDE you can use Help → View Documentation to find more specifics on how to construct the various CSG objects that are not described in the general AngelScript language description.

Another topic is transformations, but we leave that for another day.

AngelScript CSG IDE

Download AngelScript CSG version V2.0-02, with IDE included
Windows 64bit here.
Linux (K)ubuntu 15.10 64bit here.

This is an update to previous articles part 1, ​​ part 2 and part3 on AngelScript CSG. It is a work in progress, the goal is to establish a powerful modelling system enabling Constructive Solid Geometry (CSG) modelling based on the AngelScript scripting language.
Today's update is to introduce phase 2 of this project – the ​​ AngelScript CSG IDE. The first phase focused on the implementation of the scripting compiler, that is the command line program that interprets AngelScript .as files and turn them into OpenSCAD .csg files. The addition in phase 2 is an editor/IDE that allows easy editing of multiple AngelScript files with syntax highlighting, brace matching etc. It also runs the script compiler and highlights any errors in an easy manner. For model visualisation, launch OpenSCAD from within the IDE. If the .as file is changed/recompiled then OpenSCAD can be made to refresh automatically.

Below is a quick introduction to how it works. The user (you) writes AngelScript CSG code in the IDE and compiles it using either the Build menu, the green arrow in the toolbar or simply pressing F5 on the keyboard. This will save the *.as file to the disk and run the command line compiler, producing an OpenSCAD *.csg file. If you have not already done so you can launch OpenSCAD from the Build menu (or press F6) to view the 3d model.

If you set OpenSCAD to automatically reload modified files, you can simply continue editing the *.as file and press F5 and it will be updated automatically in OpenSCAD when required.

Angelscript CSG and OpenSCAD working together.

Below is an illustration of what happens if you make a coding mistake, you get an error message in red in the console window at bottom, and a red marker indicates the line of the first error. In the exampe, the user wrote 'doublex' by mistake instead of 'double'.

Highlighting coding errors (Windows)

The program also runs on Linux Kubuntu 15.10 64 bit. Below is the Find & Replace feature used under linux to fix a similar programming error.

Fixing coding errors (Linux)

All for today :-) Please give it a try and leave your comments below.


A second experiment: Boolean operations

In the previous post, it was shown how it is possible to convert a real object into a 3d computer model, suitable for replication using a 3d printer.  This was done simply using a simple flatbed scanner and some software.  The object chosen there (the wrench/spanner) was 2-dimensional if you ignore the thickness, so some may say this was cheating a bit. Could we achieve a similar effect with a more truly 3-dimensional object? The following object is our second replication challenge:


This object is not entirely flat, so it is a more challenging task to create a virtual replica of it.  If we put it on the flatbed scanner and scan it from 2 projections, from below and from the side, we get the result below (scanner lid open). The only thing done here is to present the two projections in the same image and crop away irrelevant areas to the left and right.


We now give these images the same treatment as in the first experiment. That means stretching the histogram, blurring the surfaces and using curve tools in a bitmap editor.  The goal is to emphasize the edges in the to projections, and remove anything else in the images. Below, the resulting projections are shown together for illustration purposes, but observe that each projection is treated separately.


We then give both of these  images the same treatment as before, using potrace, inkscape and pstoedit. Again, the results are simplified profiles in DXF file format, using only LINE segments:





This time, we employ some more of the powerful tools of OpenSCAD, that is ‘Boolean operations’.  For the uninitiated it can be compared to mathematical set operations,  for example intersection, union and difference.  But instead of operating on mathematical sets, OpenSCAD operates on 3-dimensional solid objects. Watch what happens if we define 3 solid objects (box, thing_A and thing_B) and subtract them from each other in the right order:


Not bad, huh? A small miracle… Again, how did this happen? Look at the solids we used. Below shows “thing_A” in yellow and “thing_B” in transparent grey. These were the bodies extruded from the image projections.


We may compare “box” (red) and “thing_A”  (transparent grey) in a similar manner:


What happens is two subsequent Boolean operations:

1. The red box is the original positive body, and “thing_A” gets subtracted from it.  That makes the “thing” without the holes.

2. Then, “thing_B” is subtracted from the result of 1. It is as if the holes get punched out using a punching tool. In many ways, that is exactly what happens.

The final result is the green “thing” as shown in OpenSCAD above. We can also save this as an STL file,  a collection of 3d triangles, and present them in wireframe mode:

Such triangles are what 3d printers need. Or to be more precise, it is the starting point of 3d printing. When printing, the triangles are cut with horizontal planes from bottom to top, also a kind of Boolean operation, the resulting intersections are horizontal line segments that can be used to generate G-code to steer the printer motors.

But that subject is for some other time.

A reverse 3d-printing experiment

I am in the process of buying a 3d-printer, so a good idea is to look at ways of creating 3d models to print. I have some time yet until I get the printer, so it is a good time to learn about the software you may need to master. Of course, the printer has its own software, but you also have to use other programs that are independent of the actual printer. In this post we shall look at some possibilities using mostly free, open source programs. The main exception is use of Photoshop, but I presume Gimp or even inkscape could do the same job as Photoshop here, I’m just using what I know a bit better.

The starting point when using a 3d printer is a virtual 3d computer model of the object you are printing. But sometimes you have a real object and want to create a 3d replica. This will be the subject of our “reverse 3d-printing experiment”:  Create a 3D computer replica of a real object. Below is our test specimen, a nostalgic object as it is a metal wrench (or ‘spanner’ if you are in the UK) I got as a kid. It came with my very first bicycle.  Can we make a computer replica?


If we place the wrench on our cheap flatbed scanner, maybe there is a way to obtain an accurate profile of it? let us try:


Below left is the raw output from the flatbed scanner,  a BMP file.  On the right is the same image after slight manipulation using an old Photoshop CS2. The features employed was to select the background with the “colour range” feature,  adding some feathering to create a smooth edge.  Then one left-over background area was clipped.  After that, the inverse of the selection was chosen, and the “levels” feature was used to blacken the wrench. Finally some “Gaussian blur” was applied to the whole image in order to soften the edges even more, and remove any remains of edge highlights from the scanning. The result is basically a black and white image of the wrench. But it is still just a raster image.


What we need is a vectorized representation of the wrench edges.  The following steps are a little convoluted, but It can really be simplified by improving the DXF file support in one of the open source programs. But until we have that, we can do the following:

Vectorizing the bitmap image

The first thing we do is to run potrace, a program  that boasts the feature we want:  “Transforming bitmaps into vector graphics” . We are going to require a file in DXF format, describing the wrench profile, and potrace can generate DXF files. However, it creates a DXF file with some features not understood by other programs, so we have to take a detour via SVG format and Encapsulated Postscript (EPS) format before we return to a simpler representation of DXF that can be used. That means a version with only simple LINES. Below is how I did it, using both Windows and linux along the way. First we run ‘potrace’ to get the SVG file from the fixed-up BMP file:


Let us copy that SVG file over to a Linux Kubuntu machine and run a couple of programs there. First we install inkscape and another program called pstoedit, based on some tips found here.

$ sudo apt-get install inscape
$ sudo apt-get install pstoedit

Now that we have the required software to complete our vectorization detour, let us use them.  First we create an intermediate EPS file using inkskape

$ inkscape -E intermediate.eps Wrench_fix.svg

Second we create the final, simplified DXF file using pstoedit, using the option “-polyaslines” to create a simplified DXF file with individual, straight lines. No polylines or spline curves. The final vectorized file is ‘wrench_os.dxf’ here

$ pstoedit -dt -f dxf:-polyaslines\ -mm intermediate.eps wrench_os.dxf

We can now open and view the created DXF file in for example LibreOffice and observe what we have created. It is no longer a bitmap image, but instead a trace of the wrench edges, i.e. a series of vectors.


Creating a 3D model

This is where the fun begins in earnest.  There is a really good, and totally free program called OpenSCAD which has some extremely powerful features that enables modelling of 3D objects.  This includes so called “Boolean operations” in CSG modelling, but also features for extruding 3D objects from 2D profiles like we have just created.  So let us try the following single command in OpenSCAD and watch what happens:

linear_extrude(height = 10) import(“Wrench_os.dxf”);

From that single line, we got something we recognise!image

What happened here? We had created the wrench profile in the DXF file. To understand what happened, you can read the above OpenSCAD commands right to left.

First, we imported the DXF file containing a profile in the XY-plane. Second, we extruded (a ‘sweep’ if you prefer) the complete profile 10 units in the Z-direction. The result was a totally recognizable virtual 3d wrench, looking just like the original, nostalgic bicycle wrench. 

With this model, we have everything required for creating a 3d printed replica, the next logical step in such a printing process would be to create an STL-file, which simply contains a number of 3-dimensional triangles describing the outer surface of the wrench model.


To prove that it works, we can view the generated STL file in a free STL viewer (chosen by random):


The STL file is available (zipped) here.


There are some incredibly powerful and free software tools available that can be used in combination with a bit of creativity to arrive at some rather impressive results. This is just great. OpenSCAD is a key tool, so this author will spend some time learning it better.  A great introduction to OpenSCAD are these tutorials (recommended):

How to use Openscad (1), tricks and tips to design a parametric 3D object
How to use Openscad (2): variables and modules for parametric designs
How to use Openscad (3): iterations, extrusions and more modularity!
How to use Openscad (4): children and advanced topics

There will be more on 3d printers from this blog.