Putting things together, and taking them away again

So so far we have talked about how to make some basic shapes in OpenSCAD, but if all you could do make was cubes, spheres and other simple shapes then what use would the program be. Ofcourse this isn’t all you can do, so lets start building up some usefull objects. So as an example we shall build the following item

Shroud

This item will show the 3 basic ways of building up complex objects, unions, differences and intersections, where a union is adding two items together, a difference is subtracting one from the other, and an intersection is finding the space in which two objects are both present.

Lets start of with the union command, union(){...}, which takes a list of objects and adds them together. now technically you dont need to use the union command as any time you place two objects they automatically form a union, but you can also explicitly define objects as a union to help you read the file later on. A quick example of using the union command:



union(){
  translate([0,100,5]){
   rotate([90,90,0]){
    cylinder(r=3, h=200);
   }
  }
  translate([100,0,5]){
   rotate([180,90,0]){
    cylinder(r=3,h=200);
   }
  }
}

So here we use the translation and rotation commands we discussed last time to place two cylinders in the correct place, and then create a union out of them.

Now next up is difference(){...}. This takes two or more objects, and subtracts the second object (or a union of all but the first object is you have more than one object) away from the first. This can then be used to help make hollow shapes, and holes in objects for things like screw holes etc.



difference(){
  union(){
   cylinder(d=10,h=50);
   translate([0,0,5]){
    cube([10,10,10],center=true);
   }
   translate([0,0,48]){
    cylinder(d=11,h=2);
   }
  }
  translate([0,0,0.5]){
   cylinder(r=4.5,h=50);
  }
}

So here we have made a stack of cylinders and cubes of various sizes giving us the outside shape of the object we are building, and we then hollow this shape out by subtracting a cylinder from the centre. This is one of the places where explicitly using the union command might come in handy. lets say you have 3 objects AB and C, and you want to take the shape outlined by A and B and subtracting C, if we simply write



difference(){
 A;
 B;
 C;
}

We would then get the union of B and C subtracted from A. Think of it like putting parenthesise in an equation for example (A*B)+C vs A*(B+C), the brackets tell us what order to do things, and by telling the two objects to form a union, we are telling the program to do that first, then do the subtraction.

At this point a useful tool when designing your object includes the difference or union commands, sometimes you want to see the original object still, to do this you just need to place a % before the object you would like to remain visible for example %cylinder(r=5,h=10) would remain visible as a semi-transparent grey object even after it was subtracted from another object, alowing you to see the object while you make adjustments.

Finally we have the intersection command, so lets use the object from the last example and perform an intersection with a cylinder



intersection(){
  difference(){
   union(){
    cylinder(d=10,h=50);
    translate([0,0,5]){
     cube([10,10,10],center=true);
    }
    translate([0,0,48]){
     cylinder(d=11,h=2);
    }
   }
   translate([0,0,0.5]){
    cylinder(r=4.5,h=50);
   }
  }
  cylinder(d=12,h=50);
}

As you can see the cube at the bottom of the object have been rounded off, to see whats going on set this line cylinder(r=4,h=50) to remain showing after the intersection has been excicuded. you can see that where the cube extends beyond the cylinder it gets cut away.

So lets put all of this together and finish off the object we started with



$fn=100;
difference(){
 intersection(){
  difference(){
   union(){
    cylinder(d=10,h=50);
    translate([0,0,5]){
     cube([10,10,10],center=true);
    }
    translate([0,0,48]){
     cylinder(d=11,h=2);
    }
   }
   translate([0,0,0.5]){
    cylinder(r=4.5,h=50);
   }
  }
  %cylinder(d=12,h=50);
 }
 union(){
  translate([0,100,5]){
   rotate([90,90,0]){
    cylinder(r=3, h=200);
   }
  }
  translate([100,0,5]){
   rotate([180,90,0]){
    cylinder(r=3,h=200);
   }
  }
 }
}

Now we have taken the cross we created in the beginning and used it to create a set of windows at the bottom of out cylinder. In theory this should be enough for you to make any object you would like in OpenSCAD, but there are some additional features that can be very useful in making things easier, most of which relate to using OpenSCAD more like a programming language from here on out.

And for those of you wondering what the object you have just drawn up is, it is the vacuum shield used in a cryostat with places to put windows so that you can cool something very cold and still shine a light onto it, similar to those i use in work.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *