The children module


If you want to make a ring, how can you do? You may perform the difference operation on a bigger circle and a smaller circle, such as …

module circle_ring(radius, thickness) {
    difference() {
        circle(radius, $fn = 48);
        circle(radius - thickness, , $fn = 48);
    }
}

circle_ring(3, 1);

The code creates a ring below.

The children module

The other way is using the offset transformation. For example.

module circle_ring(radius, thickness) {
    difference() {
        circle(radius, $fn = 48);
        offset(r = -thickness) circle(radius, , $fn = 48);
    }
}

circle_ring(3, 1);

Both cases achieve the same results. You can specify the r or delta parameters, and the chamfer parameter when using offset. The figures shown below is from OpenSCAD User Manual/Transformations. They clearly demonstrate how to use these parameters.

offset

offset

Next, how to make a frame? Similarly, you may use offset.

module square_frame(dimension, thickness) {
    difference() {
        square(dimension);
        offset(r = -thickness) square(dimension);
    }
}

square_frame([10, 5], 1);

The result is below.

The children module

Moreover, how to make a triangle frame, a star frame, or even a heart frame? I believe you can do all of them now.

Now that it's such common requirement to make a frame, could we write a general frame module which generates a frame from any 2D module? No problem.

module frame(thickness) {
    difference() {
        children();
        offset(r = -thickness) children();
    }
}

frame(1) circle(3, $fn = 48);
frame(1) square([10, 5]);

We pull out the same code in circle_ring and square_frame into a new frame module. Next, we replace circle and square with children. The modules listed after frame are the children of frame. For example, circle(3, $fn = 48) is a child of frame in the above example, so is square([10, 5]). The code will generate models below.

The children module

Use an index if there are more than one child. The children index from 0. For example.

module lineup(space) {
  for (i = [0 : $children - 1])
    translate([space * i, 0, 0 ]) children(i);
}

lineup(100) { sphere(10); sphere(10); }
lineup(100) { cube(35); cube(35); cube(35); }

The lineup module can lines up children listed after it. When listing multiple children, use curly braces to include them and separate children with a comma. OpenSCAD sets the total number of children to $children so you can know how many children there are in the above example.

The children module

If you observe some reusable operations, such as moving, rotating, etc, try to extract them and define a template module. These template modules will come in handy when you design something in the future.