JavaSCAD - transformations

How can you change the primitives

As we described in the last article, only the primitive types generate any output in JavaSCAD. Now, let's see how we can modify the primitives with transformations.

There are 9 translations in JavaSCAD. These are the followings in alphabetical order: Colorize, Difference, Intersection, Mirror, Rotate, Scale, Slicer, Translate and Union. We can categorize them into two types: translations which changes one object and translations which handle interactions between two or more objects.

Every object of a transformation could be a primitive or an already transformed object. This way an object tree can be built, where the leaves are always primitive objects and the other vertices are transformations.

Transformations with one object

Colorize

Displays the child elements using the specified color. In OpenSCAD this is only used for the preview as CGAL and STL do not currently support color. The colors are respected when you directly export to PLY format using PolygonFile

Mirror

Mirrors the child element on the X, Y or Z plane. There are three static method to do so: Mirror.mirrorX(Abstract3dModel model), Mirror.mirrorY(Abstract3dModel model) and Mirror.mirrorZ(Abstract3dModel model)

Rotate

Rotates its child by the given angle about the origin. If the given angle specifies multiple axes then the rotation is applied in the following order: x, y, z.

There are convenient constants of the Angle3d class for the most commonly used rotations. There are constants for every 90 degrees rotation, so you can easyly write this, if you want to rotate something with 90 degrees around the X axes:

 new Rotate(object, Angles3d.ROTATE_PLUS_X);

Scale

Scales its child elements using the specified vector.

Slicer

Slice a model into pieces; the result is one of the slices. It is very useful if the model is too big to be printed in one piece. The result of a slice can be sliced again, so you can slice a model first in the X direction and then in the Y.

Sometimes it is useful not just to slice your big object into several pieces and then print them one-by-one, but simply as a translator to cut your object. In the next example we use the Slicer to halve the scaled sphere:

  // creates a new sphere with a radius of 100, scale it to make it an ellipsoid and then cut the upper half out
  new Slicer(new Scale(new Sphere(100), new Coords3d(1.1, 1.6, 0.9)), Direction.Z, 2, 0);

Translate

Moves its child elements along the specified vector.

Transformations with two or more objects

Difference

Subtracts the given list of objects in the constructor's second parameter from the first one.

 // creates two cilinders, rotates the second one by 90 degrees and substract from the first
  new Difference(
	new Cylinder(4, 1),
	new Rotate(new Cylinder(4, 0.9), Angles3d.ROTATE_MINUS_X)
  );

Intersection

Creates the intersection of all child nodes. This keeps the overlapping portion.

 // creates two cilinders, rotates the second one by 90 degrees and intersect them
  new Intersection(
	new Cylinder(4, 1),
	new Rotate(new Cylinder(4, 0.9), Angles3d.ROTATE_MINUS_X)
  );

Union

Creates a union of all its child nodes. This is the sum of all children.

 // creates two cilinders, rotates the second one by 90 degrees and union them
  new Union(
	new Cylinder(4, 1),
	new Rotate(new Cylinder(4, 0.9), Angles3d.ROTATE_MINUS_X)
  );

Convenient methods of Abstract3dModel

Move

This is a helper method to move the object. The following two code snippets are roughly equivalent:

 // using the move method
object3d.move(new Coords3d(10, 20, 30));
// using the Translate translator
new Translate(object3d, new Coords3d(10, 20, 30)); 

There are several small difference though. First two consecutive move() method call will result in one Translate call in the generated SCAD code. Second the move method changes the object3d, so any interaction with it later will be affected with the move operation. You can avoid this by using cloneModel() method call

Rotate

This is a helper method to rotate the object. The following two code snippets are roughly equivalent:

 // using the rotate method
object3d.rotate(new Angles3d(10, 20, 30));
// using the Rotate translator
new Rotate(object3d, new Angles3d(10, 20, 30)); 

There are several small difference though. First two consecutive rotate() method call will result in one Rotate call in the generated SCAD code. Second the rotate method changes the object3d, so any interaction with it later will be affected with the move operation. You can avoid this by using cloneModel() method call

On sale