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.
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:
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: