Difficulty:: Easy.
Next Tutorial: The Random Forest (adding colors)
In that section we're going to see how we can transform an object by inverting it through a homothety axis. First, we create a component like this:
Component object = Cube(20,10,15,false) - Cube(5,5,5,false).translate(15,2.5,10) - Cube(5,5,10,false).translate(7.5,5,0);
We declare a component called object, which will be the result of the difference between several cubes. Main cube is a off-center cube with dimensions of 20x10x15 mm³. This object has two holes which consist of an off-center cube moved 15 mm along x axis, 2.5 mm along y axis and 10 mm along z axis, with dimensions of 5x5x5 mm³; and an off-center cube moved 7.5 mm along x axis and 5 mm along y axis, with dimensions of 5x5x10 mm³.
Now, we move object component 5 mm along y axis.
object.translate(0,5,0);
Finally, we apply the mirroredCopy function, which returns a mirrored component. The needed argument is the normal vector on a plane through the origin. We're going to use the x-z plane to mirror.
If we want to generate the OpenSCAD code for this cylinder, we simply run:
IndentWriter writer; writer << object; writer << object.mirroredCopy(0,1,0); std::cout << writer;
This will send to the standard output the following:
translate(v=[0.000, 5.000, 0.000]) { difference() { cube(size=[20.000, 10.000, 15.000], center=false); translate(v=[15.000, 2.500, 10.000]) { cube(size=[5.000, 5.000, 5.000], center=false); } // End translate translate(v=[7.500, 5.000, 0.000]) { cube(size=[5.000, 5.000, 10.000], center=false); } // End translate } // End difference 19 } // End translate mirror(v=[0.000, 1.000, 0.000]) { translate(v=[0.000, 5.000, 0.000]) { difference() { cube(size=[20.000, 10.000, 15.000], center=false); translate(v=[15.000, 2.500, 10.000]) { cube(size=[5.000, 5.000, 5.000], center=false); } // End translate translate(v=[7.500, 5.000, 0.000]) { cube(size=[5.000, 5.000, 10.000], center=false); } // End translate } // End difference 19 } // End translate } // End mirror
The full code is:
int main() { // Create a component called object. Create a 20x10x15 off-center cube. // Create a 5x5x5 off-center cube and translate it. // Create a 5x5x10 off-center cube and translate it. // At main cube subtract the others. Component object = Cube(20,10,15,false) - Cube(5,5,5,false).translate(15,2.5,10) - Cube(5,5,10,false).translate(7.5,5,0); // Translate object 5 mm in positive x. object.translate(0,5,0); //Generate the OpenScad code IndentWriter writer; writer << object; writer << object.mirroredCopy(0,1,0); std::cout << writer; return 0; }
Next Tutorial: The Random Forest (adding colors)