02 Feb 2021 - tsp

Last update 02 Feb 2021

3 mins

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:

- With
`linear_extrude`

one can model the base surface using any CSG or polygon to extrude along a perpendicular axis. Linear extrude can also twist the body like`rotate_extrude`

- Somewhat different is the behavior of
`rectangular_extrude`

that just extrudes an rectangle around the path or polygon you’re passing.

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.

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);
}
```

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

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

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

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