OpenSCAD two types of function

I’ve been experimenting with OpenSCAD for some time and was building some plates to attach some components together. For these I needed a rounded corner rectangle with some regularly spaced holes. I knew that you could create module functions that generate objects but I’d not realised that there was a second type which acted on objects, called an operator module.

RoundedRectangle

To create my rounded rectangle I take four cylinders and act on them with the hull function. This turns them into a single object, with rounded corners. Originally I had the cylinder repeated 4 times with different translates. But after reading I realised that I could do that in code too.

module RoundedRectangle(width,length,height,radius)
{
if (radius > width/2 || radius > length/2) {
echo("Warning radius too large");
}
hull() {
ForRectangle(width/2-radius,length/2-radius)
{
cylinder(h=height,r=radius,centre=true);
}
}
}

To produce my four cylinders I’ve created an operator module which repeats any children of the function over a rectangular array. Looking back the the previous function we can see that a cylinder is passed in as the child object. So these operator modules are good for repeating or arranging items.

module ForRectangle(offsetx,offsety) {
for(i = [[-1,-1], [1,-1], [1,1], [-1,1]])
{
translate([offsetx*i[0],offsety*i[1],0])
children();
}
}

This function is used again in my board object module, to create 4 mounting holes.

module Board()
{
difference() {
RoundedRectangle(20,28,2,2);
translate([0,0,-1])
ForRectangle(7,11)
{
cylinder(h=7,d=2+(gap/2),$fn=200,centre=true);
}
}
}

Finally a call the board function. I’ve increased the smoothing parameter for the corners so we nice round corners rather than jagged ones.

$fn=200;
Board();

So by creating functions in this way you can avoid duplication in your code and make it shorter and easier to read and maintain. Having functions also gives you more chances to re-use code.

Simple garden bench – Finishing

Simple garden bench - Finishing

As the bench was going outside I knew it would need some protection from the elements. The metal brackets that supported the legs were given a coat of Hammerite. The top of the bench was sanded and then cleaned with

Continue reading Comments Add Comment

Simple garden bench

Simple garden bench

The last couple of weekends the family teamed up to help renovate my Mother in law’s garden. It was a big garden and had got overgrown as it was too much for her to maintain. After a general tidy, we

Continue reading Comments Add Comment

Primes

Primes

The reminders about Amazon Prime day got me thinking about Prime Numbers. Whilst I was coding a power efficient “blink” for a new 4Tronix board I joked that it was not calculating primes. But then I thought, “why not?” I

Continue reading Comments Add Comment

Dressing a grinding wheel

Dressing a grinding wheel

When I’d been grinding my tungsten for the TIG welder, I’d been a bit careless and had ground in the same place each time. The net result was that I ended up with a groove down the middle of the

Continue reading Comments Add Comment

Ernest Malinowski and the Central Trans-Andean Railway

Ernest Malinowski and the Central Trans-Andean Railway

Following on from the article on Marc Brunel, the next in the series is Ernest Malinowski, a Polish Engineer who put his hand to building a railway across the Andes mountain range. Ernest Malinowski was born in a troubled time

Continue reading Comments Add Comment

The other Brunel

Smithsonian Photo 49260-G

Pretty much everyone knows of Isambard Kingdom Brunel the great British Engineer. Fewer know that his father, was also a great engineer and an immigrant from Europe. Marc Isambard Brunel was born into a farming family in Normandy. He was

Continue reading Comments One Comment

3D Printing a knight

3D Printing a knight

Over the weekend, I printed out the parts for the servo activated knight. It is designed to be glue together but I’ve made the parts a sliding fit. See GitHub for the models. 3D printing the parts There were a

Continue reading Comments Add Comment


Next Page »