OpenSCAD linear extrude that interpolates between two shapes

Full source code is available for this thing on thingiverse.

OpenSCAD has proven to be an easy to use and flexible tool, but at times I'm left scratching my head to figure out ways around its limitations. In this case I had two squares with rounded corners, their sizes and the corner radius for both squares are configurable. Now I wanted a square tube to join these two pieces together.

Final shape
My hope was that I could get it close enough with some fancy parameters piped into the OpenSCAD linear_extrude function. It seemed possible since there are some pretty fancy parameters regarding rotations and twists. After checking out the documentation it didn't look like there was anything that would be close.

For this project I found a module by Felipe C. da S. Sanches to create squares with rounded corners. I'm sure there are many like it, but this was the first one I found and it worked as advertised.

So made a little module using this which wraps around a rectangle for one side, and another which fits into a rectangle for the other side:

This simply creates a rectangle with rounded corners and subtracts a slightly smaller rectangle from the middle, then extrudes the whole assembly some height. This piece goes around a rectangle with square corners, so the inside has square corners. Then I created a second tube designed to fit into a rectangle with rounded corners, so the outside of this tube must have rounded corners. After spacing them out I get something that looks like this:
Multiple rounded rectangles

This is where I realized I need some fancy function to extrude the bottom shape into something which meets up with the upper shape. The solution I ended up with works the same way as the 3D printer I'll print this object with: slice it up into multiple extrusions. For each extrusion I interpolate the inner radius, outer radius, width and length depending on how far into the overall extrusion we are.

With this I can now create a shape which connects my two adapters. The final product printed out nicely:
Printed object
Finished product!

Installed adapter
Fits perfectly.


  1. I would probably have done the same job by using the hull function.

  2. I would probably have done the same job by using the hull function.

  3. The rounded square you need is just:

    module rounded_square(x,y,r) {
    translate([r,r]) minkowski() {

  4. ... and this one is even simpler:

    module rounded_square(x,y,r) {
    translate([r,r]) offset(r=r) square([x-2*r,y-2*r]);


Post a Comment

Popular posts from this blog

Decommissioning the cloud: A self-hosted future

Taking Stuff Apart: Realistic Modulette-8

Using a JFileChooser to browse AWS S3