How to extrude polygons in OpenJSCAD

02 Feb 2021 - tsp
Last update 02 Feb 2021

Since I always have to lookup how this works when I’m doing more complex 3D designs I decided to do a short summary on how to use the extrusion functions to extrude simple polygonal 2D paths. I used this in many of my OpenJSCAD models and libraries.

The idea is pretty simple - if you have an prism with a rather complex (but polygonal) side surface that you want to extrude - one can use this to approximate any shape and it’s easy to write routines that trace any mathematical function in OpenJSCAD - there are some simple methods that one can use:

Using linear extrude

The two basic patterns that I’m using (note that you can use any CAG) - both of my patterns are using polygons - is generating a polygon either using the CSG.Polygon2D object or the polygon method. Both of them accept an arbitrarily sized array that contains a list of 2D coordinates in a single plane.

Linear extrude without twist

Let’s say one wants to extrude an object with hexagonal shape with defined outside radius / circumference. In this case one knows that one has six points separated by $\frac{360}{6} = 60$ degrees. Thus one can simply calculate coordinates for all six points in a simple fashion:

[ x_n = r * sin(n * 60) \\ y_n = r * cos(n * 60) \\ ]

These points are pushed as 2D arrays into a 2D array in a simple loop, then one can create a Polygon2D object out of them and pass this to linear_extrude:

function hexagon(outerRadius, thickness) {
    let edges = [];
    for(let i = 0; i < 6; i++) {
        let xn = outerRadius * Math.sin(Math.PI/180 * 60 * i);
        let yn = outerRadius * Math.cos(Math.PI/180 * 60 * i);

        edges.push([ xn, yn ]);
    }

    let path = new CSG.Polygon2D(edges, /* Polygon is closed */ true );

    return linear_extrude( { height : thickness }, path );
}

/* Simple demo function to use the hexagon generator */

function main() {
    return hexagon(10, 5);
}

The extrusion function also allows one to create twisted objects (which is something similar to what I’m using in my gears library by simply setting the twist and slices parameters. Twist tells the extrusion function how far the object should be twisted around the extrusion axis in degree - for example 360 means turn the object exactly one time, 720 would mean twist it two times.

function hexagon(outerRadius, thickness) {
    let edges = [];
    for(let i = 0; i < 6; i++) {
        let xn = outerRadius * Math.sin(Math.PI/180 * 60 * i);
        let yn = outerRadius * Math.cos(Math.PI/180 * 60 * i);

        edges.push([ xn, yn ]);
    }

    let path = new CSG.Polygon2D(edges, /* Polygon is closed */ true );

    return linear_extrude( { height : thickness, twist : 90, slices : 360 }, path );
}

/* Simple demo function to use the hexagon generator */

function main() {
    return hexagon(10, 5);
}

Hexagon rotated by 90 degrees in 360 steps

The slices parameter determines how many steps the algorithm should make to approximate the twisted figure using polygons. The higher the number the better the approximation but the worse the polygon count. If one reduces the number of slices from 360 in the example above to only 10 the resolution would drop significantly

Hexagon rotated by 90 degrees in 10 steps

Maybe I’ll extend this blog post later on to demonstrate the usage of rectangular_extrude and rotate_extrude

This article is tagged: Programming, OpenJSCAD, 3D printing


Data protection policy

Dipl.-Ing. Thomas Spielauer, Wien (webcomplains389t48957@tspi.at)

This webpage is also available via TOR at http://jugujbrirx3irwyx.onion/

Valid HTML 4.01 Strict Powered by FreeBSD IPv6 support