Wednesday, April 12, 2017

TIE fighter in OpenSCAD


OpenSCAD is an excellent 3D CAD tool to create space craft because of their symmetrical, non-organic shape. This video of a space ship is a good example. However I couldn't find much Star Wars models made in OpenSCAD. So I decided to do it myself and start with a simple TIE fighter.

TIE fighter made in OpenSCAD.



When I create a model like this I start with simple primitives and get the position right. In this case a sphere that represents the command pod, two cones that act as pylons and two hexagons as solar arrays. Once I have a basic TIE fighter I start shaping the different parts. Since all these parts are modules this doesn't interfere with the positioning.

Most basic shape of a TIE-fighter consisting of a sphere, two cones and two hexagons. Hexagons still need a 30 degrees rotation around the x-axis.

I use a couple of global constants that mainly determine the positioning of the different parts of the TIE fighter. The're seperate modules for the command pod, solar array and the wing pylon. Also a seperate module exists for the solar panels that are part of the solar array.

Most of the code is pretty standard OpenSCAD maybe with the exception of the solar array. The solar panel is created with a polygon which is a trapezoid. The trapezoid needs to fit between the inner en outer hexagon therefore the points of the trapezoid are depending on these hexagons of the solar array. The angle of the trapezoid is 120 degrees enabling me to calculate the all points of the trapezoid solar panel with basic trigonometry. The variable delta in the solar_panel module enables us to create and edge on the solar_array.

module solar_array (outer_radius, inner_radius) {
    difference() {
        union() {
        for (i=[1:6]) {
            rotate([0,0,i*360/6]) translate([0,inner_radius,0]) translate([0,0,1]) solar_panel(outer_radius,inner_radius);
            rotate([0,0,i*360/6]) translate([0,inner_radius,0]) translate([0,0,-0.3]) solar_panel(outer_radius,inner_radius);

module solar_panel(outer_radius, inner_radius) {
    //delta determines the size of the edge of the solar array.
    delta = 3;
    //x and y determines outer side of the solar panel trapezoid
    x = inner_radius/2 + sin(30) * (outer_radius-inner_radius-delta);
    y = cos(30) * (outer_radius-inner_radius-delta);
    list = [[-inner_radius/2,0],[inner_radius/2,0],[x,y],[-x,y]];

The TIE fighter model is largely parametric meaning that the most of the design of the model can easily can be changed by changing the parameters (constants). This model of the TIE fighter can is still rather basic but it can be expanded by modifiying the different modules.

3D printing

Until now I didn't feel the need for 3D printing the model (my extruder is in repair anyway) but with a few modifications printing shouldn't be a problem. I would print the command pod first and attach the solar array with wing pylon later.

The OpenSCAD file of the TIE fighter can be found here.

OpenSCAD is open source (GPLv2 license) and is well maintained by Marius Kintel et al. Besides the stable releases for Windows, OSX and Linux, development snapshots are available. I recommend using these development snapshots since they have all the latest features.

Wednesday, March 29, 2017

Creating interactive 3D models in your browser with Solvespace

Interactive 3D model

It may be convenient to view a interactive model made in Solvespace using a webbrowser. For instance if you want to share the model with someone who hasn't Solvespace installed. Luckily Solvespace has an option just for this under File -> Export Triangle Mesh. Now in the dialog window select Three.js-compatible mesh with viewer (html). Save the file. An html file is now available that can be viewed in the browser (I only had succes with Firefox, Chrome wouldn't show me the model). The html file created by Solvespace consist for a large part on Javascript and relying on the Three.js Javascript library. All the points, edges and faces of the 3D model are included in this file.

Screenshot of the html file created by Solvespace and opened in the Firefox browser.

When the html file is opened the 3D model can be rotated (left mouse button), moved (right mouse button) and zoomed (scroll wheel). The options are activated by a mouse click in the frame. Clicking on the scroll wheel deactivates these options.

One step further

This can be taken one step further. The model can be integrated in a website. The simplest way is to use the <iframe> tag in html. Below is a simple example where cube.html is the file that has been generated by Solvespace.

<!DOCTYPE html>
    This is a cube
        <iframe src="cube.html", height="600", width="800"></iframe>

That's it, the interactive model will show up in the webpage which is a pretty cool Solvespace feature. BTW: I didn't bother demonstrating it in Blogger because I think it's to much of a hassle editing the template.

For this tutorial I used Solvespace 2.3 on OSX.

Solvespace is open source (GPLv3 license) and is available for Window, OSX and Linux. It is developed by Jonathan Westhues and maintained by Whitequark and others. It can be downloaded here:

Friday, March 17, 2017

Creating a mathematical rose in OpenSCAD


In my previous blog post I explained how to create complex 2D shapes in OpenSCAD. Once a mathematical expression for a shape is known it's possible to translate it to OpenSCAD script. The Carthesian coordinates of mathematical rose, a rose shaped sinusoid, can be expressed by x = cos(kθ)cos(θ) and y = cos(kθ)sin(θ). If k is an integer a the shape will be relatively simple but is k is a fracture more complex shapes are created.

One of the shapes that can be created with the rose_points function (linear_extrude is used to create a 3D shape).

Writing the script

All the points needed to create the rose will be generated in the function rose_points. Rose
_points returns these points in a list. The function will have the form:

function rose_points(k,n,radius) = [...]

The parameters k and n will determine the shape of the rose. If n = 1 the curve will a simple rose shape but if n > 1 more complex shapes are drawn. Here is a link to all the possible shapes when varying k and n. The syntax of OpenSCAD for lists allows us to use if and for elements to construct a list. In the case of our function we can use these elements.

step = 1;
function rose_points(k, n , radius) = k%2 == 0 && n%2 ==1 ? [for (theta = [0 : step : 360 * n]) [radius * cos(k/n*theta) *sin(theta), radius * cos(k/n*theta) * cos(theta)]] : [for (theta = [0 : step : 180 * n]) [radius * cos(k/n*theta) * sin(theta), radius * cos(k/n*theta) * cos(theta)]];

color("red") polygon(a);

The function looks intimidating at first due to the list comprehensions.  The key is to just break it apart. The two parts between square brackets contain lists of points (generated by a for loop). The rest is an if, then, else statement: if k is even and n is uneven then [] else []. More information on the syntax for a list (list comprehensions) can be found here.

The function isn't perfect. With some combination of k and n (e.g. 6 and 2) nothing is displayed on the screen. The reason is that with this combination all points are repeated prompting the polygon function of OpenSCAD to draw nothing. In this cases it helps to change the range of theta.

Caveat: List comprehensions as shown in the rose_points function are only possible with OpenSCAD v2015.03 and above.

OpenSCAD is open source (GPLv2 license) and is well maintained by Marius Kintel et al. Besides the stable releases for Windows, OSX and Linux, development snapshots are available. I recommend using these development snapshots since they have all the latest features. 

Shape created with k = 2 and n = 1.

Shape created with k =8 and n = 7.

Friday, March 10, 2017

Creating complex 2D-shapes in OpenSCAD

Polygon and polyhedron

OpenSCAD allows the user to create complex shapes with the polygon function for 2D and polyhedron for 3D. Polygon and polyhedron both accept a list of 2D and 3D coordinates (points) respectively as parameters. A functions can generate a list of points eliminating the need to manually created these lists. This property can be used to create shapes that are impossible with the 2D and 3D shapes that are build-in in OpenSCAD. In this blog post I'll show how to create functions for some simple 2D shapes and explain how to manipulate the functions make more complex shapes with them.

Shapes created with functions and polygon in OpenSCAD.

Creating a 2D shape

To create a circle with a radius of 20 in OpenSCAD we just have to type


However OpenSCAD doesn't allow us to reshape this build-in function to for instance an ellipse. Alternatively we can write a function that generates a list of points needed for a circle and then use polygon with the points as parameter to draw the circle. The function uses the trigonometric formulas, x = r cos φ and y = sin φ, to convert polar coordinates to Cartesian coordinates.

function circle(radius) = [for (phi = [1 : 1 : 360]) [radius * cos(phi), radius * sin(phi)]];

When F5 is pressed a circle is drawn however the x,y coordinates of this circle are available to us. By adding echo(circle(20)); to our script the list of points is printed in the console. The circle function can easily be altered thus gaining a new shape. An example is shown below.

function circle(radius) = [for (phi = [0 : 1 : 720]) [radius * cos(phi/2), radius * sin(phi)]];
color("red") polygon(circle(20));

Shape create with x = r cos(φ/2) and y = r sin(φ)

Now let's take a look at the syntax of the function. Every function generates a value and in this case it is a list of points. In OpenSCAD a list of points in a two-dimensional space is represented by [[x1,y1],[x2,y2],[x3,y3],...] where all x's and y's are numbers. In this case of the circle function the point are generated in a for loop. The loop begin at 0 and ends at 720 with a step of 1. The radius * cos(phi/2) and radius * sin(phi) calculate each x,y coordinate for every given phi.

The ellipse, a generalisation of the circle, can now easily be created by slightly changing our function.

function ellipse(r1, r2) = [for (theta = [0 : 1 : 360]) [r1 * cos(theta), r2 * sin(theta) ]];
color("cyan") polygon(ellipse(120,80));

Ellipse created with the code above.

a second parameter is added. r1 is the radius in the x-direction and r2 is the radius in the y-direction. If r1 is equal to r2 a circle is drawn.


OpenSCAD allows the user to create complex 2D shapes using functions that generate lists of points This list is used as the argument in the polygon function of OpenSCAD. Every shape can be generated as long as the mathematical expressions are known and can be translated to OpenSCAD script. This opens up a world of possibilities.  The same is true for 3D shapes but instead of polygon the polyhedron function of OpenSCAD should be used. This however is a topic for a future blog entry.

Caveat: List comprehensions as shown in the functions of this  article are only possible with OpenSCAD v2015.03 and above.

OpenSCAD is open source (GPLv2 license) and is well maintained by Marius Kintel et al. Besides the stable releases for Windows, OSX and Linux, development snapshots are available. I recommend using these development snapshots since they have all the latest features. 

A special thanks to Xavier Faraudo who explained the advantages of functions in OpenSCAD to me.

Saturday, March 4, 2017

Getting creative with OpenSCAD


OpenSCAD is software to create solid 3D CAD Models but other than most 3D CAD programs a model can be created with a programming language. Although OpenSCAD is often used to create models for 3D printing it also very capable as a tool for creative coding. Creative coding is more about being expressive than being functional.

Sierpinski triangle made in OpenSCAD (recursive algorithm).

Creative coding

I've been using OpenSCAD for over a year now for my 3D printing projects but as of late I'm exploring the creative possibilities of this programming language. It all started when I was watching another episode of The Coding Train, a great and often hilarious YouTube channel from Daniel Schiffman. In this show Daniel Schiffman takes on coding challenges. These coding challenges can be a physics simulation, data visualization or computer vision. While watching the show I was wondering which tool would be suitable to take on these challenges myself. Schiffman is mainly using Processing and P5.js which aren't in my skill set. I have a basic understanding of Python but not enough to start coding these challenges. OpenSCAD has become the programming tool that I'm most familiar with so I figured to give it a try.

OpenSCAD is pretty well equipped for creative coding. No need to import additional modules or libraries to draw onto the screen. It also comes with ready to use 2D and 3D primitives, geometric transformations and boolean operations for 2D and 3D primitives. OpenSCAD is a functional programming language as opposed to most common programming languages such as Python, C and Java which are imperative languages. In OpenSCAD variable keeps one value so i  = i + 1 won't work and f(x) will always produce the same result for a given x (as in mathematics). Once you get your head around that working with OpenSCAD becomes easy.

OpenSCAD doesn't disappoint for creative coding. Although some challenges didn't seem possible with OpenSCAD, because of the limited animation features of the program (or my lack of understanding of these features, others could be easily achieved with it. Below are examples of challenges that I did with OpenSCAD.

Mathematical rose pattern made in OpenSCAD.

Phyllotaxis pattern made in OpenSCAD.

Along the way I discovered that I'm not the only one using OpenSCAD for creative programming. I like to mention the website which has an excellent OpenSCAD section. This section not only explains the underlying fundamentals of OpenSCAD but also has creative chapters about spirals, turtle graphics and maze.

OpenSCAD is open source (GPLv2 license) and is well maintained by Marius Kintel et al. Besides the stable releases for Windows, OSX and Linux, development snapshots are available. I recommend using these development snapshots since they have all the latest features.

Saturday, January 28, 2017

DIY Hangboard (for climbers)


My youngest son is an avid climber and boulderer and he wanted a hangboard in his room to strengthen is arm, hands and fingers etc. I had a lot of wood left from previous projects so I used some of it to build the hangboard. After searching the internet and seeing a lot of examples we came up with the drawing below.

My son using his newly acquired hangboard for training.

Simple hangboard design.
The backplate and frontplate are made of 12 mm (1/2") plywood while the beams and support beams are made of 30mm (1 1/4") scaffolding wood. The rails are of pine wood of various thickness, 1 1/4" being the thickest and 18mm (3/4") the thinnest. The support beams strengthen the construction considerably.

Parts list

Here are the parts that are needed for this build.
1 x backplate 90 x 61 cm (3' x 2') plywood (12mm or 1/2")
1 x frontplate 90 x 31 cm (3' x 1') plywood (12mm or 1/2")
2 x beam 30 x 19 cm (12"x 7.5") scaffolding wood (30mm or 1 1/4")
2 x support beam 41.5 x 6.2 cm (14" x 2") scaffolding wood (30mm or 1 1/4")
2 x rails 90 x 5 cm (3' x 2") pinewood of various thickness
8 x carriage bolts 6mm (1/2")
8 x wingnut 6mm (1/2")
4 x lag screws 7 x 60mm (5/16" x 2.5")
4 x wall plug fisher 10 mm (3/8")
18 x twinfast screw 4.5 x 40mm (3/16" x 1 1/2")

Building the board

Building the board is straight forward. First I'd cut the plywood to the plates with the size indicated above. Next I'd cut the scaffolding wood for the beams. I used a simple Dremel DSM20 for this but any circular saw will do. Lastly I'd cut the pinewood beams. I used a router to make a radius on one side of the beams. This radius ensures that the beams are easier on the hands.

With all the pieces cut, I drilled the holes in the backplate, frontplate and rails. Next I assembled all the parts. Starting with the backplate and the beams. I used a 3mm drill before fastening the beams and the plywood with the twinfast screws. This avoids that the wood splits open. The beam and supportbeam were fastened with a single twinfast screw. Next I fastened the frontplate to the beams again using twinfast screws. To finish the hangboard I fastened the beams to the frontplate using the carriage bolts and wingnuts. Lastly I applied a transparent oil to the hangboard to provide protection It's more pleasing to look at too.

Fastening the hangboard to the wall may differ from situation to situation but since we have concrete walls I used four large wall plugs (Fisher brand). The board was then attached to the wall using the lag screws with washers to protect the wood.

Assembling the hangboard. Clamps of various size are very useful in situations like this.

The finished hangboard attached to concrete wall with four large wall plugs, lag screws and washers.

Inside view of the hangboard. By removing four wingnuts the beams can be exchanged for smaller of wider ones.
Detailed view of the lag screw and washer on the plywood backplate.


The hangboard proves to be strong enough for my son (he weights approximately 65kg). I tried it myself (I'm 90kg) but my fingers just aren't strong enough to keep my weight. Nevertheless I'm confident that the construction is more than enough to keep a weight well over 65kg. As for esthetics the board looks pretty good (for a hangboard that is).

Monday, January 9, 2017

Peddle wheel boat (3D printed)


At the moment I'm making 3D design and 3D printing lessons for schools. A good idea that I'm exploring is the Paddle Wheel Boat. I found an example on Thingiverse, a rubber band powered boat with two peddle wheels, but it has two problems. First of all the author only provides .stl files and second the design is a bit flawed. I therefore decided to design the boat from the ground up with OpenSCAD. With OpenSCAD I'm not only able to edit my models quickly, I'm also able to share the OpenSCAD script allowing other to use and change it. Links to the downloadable files can be found here:

Assembled Paddle Wheel Boat.

Paddle Wheel Boat

The Paddle Wheel Boat that I created consists of four parts: the boat, the shaft and two peddles. The main change of the boat that I designed is the position of the shaft. It's been shifted to the middle of the boat providing more balance. I also enforced the attachment point of the rubber band to the boat making it impossible to break it.

The redesigned Paddle Wheel Boat with shifted positon of the shaft and enforced attachment point for the rubber band.

I also increased the size of the paddles enabling the boat to move faster. The shape of the paddles are rounded instead of square improving the dynamics of the paddles through the water. A problem with the old design is that the rubber band has to be fixed to the shaft with some tape. Not a very elegant solution. I added a square hole to the design of the shaft. The rubber band can be inserted through the hole and fixed to the shaft. No tape needed.

Simple square hole in the shaft. The rubber band fits right through the hole making it easy to fix it to the shaft.
Changes to the models are easily made. The OpenSCAD script is simple and the result of a change can be reviewed immediatly by pressing F5. E.g to move the shaft to the back of the boat can be achieved by just one simple change in the script.

Peddle wheel loaded in Cura 2.3.1.

Just four parts, the boat, shaft and two peddles, are needed to make the boat. If you don't want to make changes to the design just download the provided .stl files and open them in your favorite slicer. After printing the Peddle wheel boat can be assembled and tested. Have fun.

All printed parts plus the rubber band.