## Tiny Organ Chips

dark mode light mode Search Menu
Search

Rainer Stropek on Flickr

Recently, we looked at getting started with the free and open-source OpenSCAD software which allows us to create 3D models using code scripts. Check out that article (https://beanzmag.com/openscad-3dgetting-started) for links to download OpenSCAD and how to create, move, and combine basic shapes and other fundamental OpenSCAD skills.

After you have made a few objects in OpenSCAD, you might come across the issue that you want to make multiples of similar parts. Imagine, for example, you are designing a small table. You can design one of the table legs and then copy and paste that entire block of code three times to make three more legs and move them into position. While that approach absolutely works, you do end up with a lot of repetitive code in the editor and that makes your code harder to read.

A simple and more streamlined approach is to create a module. A module is a selection of code packaged up so that you can create or call up a copy of the module inside your design just by scripting the module name. As a basic example, let’s create a simple table leg and then convert it into a module. First, as we discussed in the Getting Started with OpenSCAD article, let’s set the number of fragments for the whole project up to 60 so that our cylindrical parts are a reasonable resolution.

``\$fn = 60;``

Next, let’s create a wider cylinder for the foot of our model table leg. As a quick re-cap, the parameters inside the cylinder brackets are the height, radius 1 & radius 2, and whether we want the part to be centered around the origin point.

``cylinder(10,6,6,false);``

Press F5 to see the cylinder in the preview window. Next, let’s add another thinner cylinder for the upper part of our table leg. The code for the 2 cylinders should look like the code below. We covered the use of “translate” in the Getting Started with OpenSCAD article but as a refresher, the values inside the square brackets are for the distance moved along the x, y, and z axes. As we want to add a cylinder to the top of our earlier cylinder we translate along the z axis the height of the first cylinder.

``````cylinder(10,6,6,false);
translate([0,0,10])
cylinder(10,3,3,false);``````

Pressing F5 again you should now have the two cylinders stacked to form a very basic table leg. A You could of course create four table legs this way but it would become slightly complex and long-winded as you would have to translate each of the 2 cylinders per leg into the desired final position resulting in quite a lot of code. This is a great case for a module.

To create a module, we first need to give it a name, move to above the current leg code and add the following code.

``module leg(){``

Notice that we have opened a set of curled brackets but we haven’t closed them. For the next step, use the tab key to indent our two original table leg lines inwards, and then on a new line at the end add the closing curly bracket. Your code should now read;

``````module leg(){
cylinder(10,6,6,false);
translate([0,0,10])
cylinder(10,3,3,false);
}``````

Congratulations, you have just created a reusable code module!

However, if you press F5 now your existing leg will disappear. This is because we need to call the module to actually create a leg in our design. You can now call a copy of the leg module by simply adding;

``leg();``

It doesn’t matter if you call a module before or after the module code in the script, but it’s a good idea to keep things tidy. We tend to keep all our modules towards the bottom of the script and all the code-calling modules higher up, but you can find your own preferred style.

Of course, we can call multiples of the same module and we can apply other actions to modules such as translating them. Try adding another leg using the line;

``translate([30,0,0])leg();``

What we have learned so far is incredibly useful, but there are even more useful ways we can use modules.

When we call a module, we use the module name and then type a pair of brackets. If we think about when we call a cylinder, we can put the parameters of the cylinder inside these brackets—the height and the radii, etc. We can create our own input parameters for the modules making them even more versatile. Let’s adapt our “leg” module to use this technique.

First let’s change the opening line of the module that currently reads

``module leg(height, radius){``

Adding this means that when we later call leg(), if we add values inside the brackets, these will be assigned to a variable called “height” and a variable called “radius”. The variable names can be anything, but it’s usually worth naming them something relevant to what they will mean in the code.

Next, make the rest of the module code look like this :

``````module leg(height,radius){
translate([0,0,height/2])
}``````

Reading this through you might be able to work out what is happening. The first cylinder uses “height/2” to set the height of that part of the leg. So if we eventually say that the height variable is 30 then the height of this first cylinder will be 15. The first cylinder takes the value of “radius” as its radius.

Looking at the second cylinder we can see that it is moved or translated by “height/2”, which means it will be placed at the top of the first cylinder, no matter what height is input to the module. The second cylinder is also half the overall height and this is the thinner part of the leg which is created by dividing the overall radius in half.

With this code in place, you can now call an instance of the leg module but add any height and radius parameters when you call it. For example, input the following and press F5.

``leg(20,6);``

In Computer-Aided Design (CAD) this approach is often referred to as working “parametrically” or “parametric design”. With our parametric module set up, it’s really easy to now create and move multiple leg modules in our design.

``````leg(20,6);
translate([40,0,0])leg(20,6);
translate([0,40,0])leg(20,6);
translate([40,40,0])leg(20,6);``````

While we have learned a lot in this article, we couldn’t leave our leg modules without a tabletop. Let’s add a cube object to finish off our tiny table design.

To add a cube shaped as a table top we can add a line to create a cube object and translate it into place by adding the following line at the end of the script.

``````translate([-10,-10,20])
cube([60,60,5], false);``````

Your code script should now match the code in the main picture at the top of this article and you should have a small table in the preview that looks similar! You probably realize by now that you could add more variables and parameters to the leg module, in fact, why not challenge yourself to write a module that could parametrically generate a complete table?