Tuesday, May 16, 2017

A Solvespace tutorial, the Fidget Spinner

Another Solvespace tutorial. 

In this tutorial I'm going to create a Fidget Spinner. For those who don’t know, a Fidget Spinner is a stress-relieving toy. A basic Fidget Spinner consists of a bearing in the center of a design made from any of a variety of materials. I got this idea from Paul Randall’s YouTube channel. Paul has a great channel with an increasing number of excellent OpenSCAD and FreeCAD tutorials. Last week Paul uploaded two video tutorials where he creates a Fidget Spinner, one in OpenSCAD and one in FreeCAD. I thought it was a good idea to add a Solvespace tutorial to this and leave it for the user to judge which of these open source 3D CAD programs is best for these kind of models.

The Fidget Spinner that we create in this tutorial can easily be 3D printed. However it may require some adjustments of the dimensions before it can be succesfully assembled into a working Fidget. When this is finished insert a bearing and three nuts and the Fidget is ready for use.

Sunday, May 7, 2017

Video on 2D Supershapes created in OpenSCAD

2D Supershapes

I just uploaded a video on 2D Supershapes created in OpenSCAD, the open source 3D CAD program. 2D Supershapes are based on an equation, the Superformula, proposed by John Gielis around 2000. Gielis suggested that the formula can be used to describe many complex shapes and curves that are found in nature. The possibilities with the Superformula seem endless. OpenSCAD not only let you recreate these Supershapes but also enables the you to 3D print the shapes.

Wednesday, April 12, 2017

TIE fighter in OpenSCAD

Introduction

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.

 

Design

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.

//Modules
module solar_array (outer_radius, inner_radius) {
    difference() {
        union() {
            cylinder(r=outer_radius,h=2,$fn=6);
            cylinder(r=inner_radius,h=4,$fn=6);
        }
        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);
    echo(x,y);
    list = [[-inner_radius/2,0],[inner_radius/2,0],[x,y],[-x,y]];
    linear_extrude(1)
    polygon(list);
}

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>
<html>
    <head>
    <title>
    This is a cube
    </title>
    </head>
    <body>
        <iframe src="cube.html", height="600", width="800"></iframe>
    </body>
</html>

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: http://solvespace.com/download.pl.

Friday, March 17, 2017

Creating a mathematical rose in OpenSCAD

Introduction

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)]];

a=rose_points(5,7,100);
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

circle(20);

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)]];
polygon(circle(20));

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.

Conclusion

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

Introduction

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.

Openhome.cc

Along the way I discovered that I'm not the only one using OpenSCAD for creative programming. I like to mention the website openhome.cc 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.