Difficulty:: Easy.
Next Tutorial: Creating a Cylinder and Rotating it

Creating a Cube and Translating it

The Cube is one of the primitive objects existing in OOML. The documentation of the Cube class can be found in the Doxygen page.

The constructor of the cube takes three parameters: x,y,z, indicating the lengths of the cube sizes, and 'center', by defaut true, indicating if the cube is centered on the origin. If we want to declare a cube in our code we write:

Component cube = Cube(10,20,30);

This declares a variable cube, representing a cube of dimensions 10x20x30. The first cube is centered on the xyz origin, the second is not, and to bottom left corner will on the origin.

If we want to generate the OpenSCAD code for this cube, we simply run:

IndentWriter writer; 
 
writer << cube;
 
std::cout << writer;
//or
writer.dump(std::cout);

This will send to the standard output the following

cube([10,20,30],center=true);

We will use the IndentWriter object to collect all the Open Scad generated code.

If we want to generate the code of both cubes, the code should be:

IndentWriter writer; 
 
writer << cube;
writer << othercube;
 
 
std::cout << writer;
//or
writer.dump(std::cout);
If we want to translate the cube and generate its Open Scad code, we should write:

cube.translate(20,35,-5);
writer << cube;
std::cout << writer;

which will generate:

translate([20,35,-5]) 
cube([10,20,30],center=true);

If we want to create a translated cube of the same dimesions keeping the original cube unmodified we write:

Component transCube = cube.translatedCopy(20,35,-5);
writer << transCube;
std::cout << writer;

Note that
cube.translate(10,4,2);
translated the component cube, while

cube.translatedCopy(10,4,2);

does not translates the cube component, but creates another component which is the original cube translated.

int main()
{
        IndentWriter writer;
	// Cube of lengths: 10, 20, 30
	Component myCube=Cube(10,20,30);
 
	//generate OpenScad code of myCube
        writer << myCube;
	//OR myCube.gen_scad(writer);
        std::cout << writer;
 
	//create a new object translating myCube;
	Component translatedCube = myCube.translatedCopy(20,35,-5);
 
	//generate OpenScad code of the translated cube;
	IndentWriter otherWriter;
        otherWriter << translatedCube;
        // OR translatedCube.gen_scad(otherWriter);
        std::cout << otherWriter ;
 
	return 0;
}

Next Tutorial: Creating a Cylinder and Rotating it

Recent changes RSS feed Creative Commons License Donate Minima Template by Wikidesign Driven by DokuWiki