Rotating and translating the model

This example shows how to rotate the model around a specified axis and point. To allow adding shapes in other planes than the default x-y plane. We also demonstrate how to translate the model.

from qtcad.builder import Builder
from qtcad.builder import Mask, Polygon, Circle, Direction

Rotation

First we create a mask with two shapes, a large base and a smaller shape on top of it.

footprint = Polygon.box(10, 10, name="base").centered()
small = Circle(name="small", center=(0, 0), radius=2)

example_mask = Mask("footprint")
example_mask.add_shape(footprint)
example_mask.add_shape(small)
Circle(name='small', id=1, radius=2, center=(0, 0))

We create a Builder with this mask, extrude the base shape and then extrude the smaller shape on top of it.

builder = (
    Builder()
    .add_mask(example_mask)
    .use_shape("base")
    .extrude(10)
    .use_shape("small")
    .extrude(2)
    .view(
        volume_labels=True,
        surfaces=True,
        angles=(-45, 0, 45),
        save="figs/unrotated_model.svg",
        font_size=20,
        zoom=0.7,
    )
)
rotation
/home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/src/qtcad/builder/core/builder.py:3518: UserWarning: Removing orphaned entities: [(1, 14)]
  warnings.warn(

Now, we rotate the model 90 degrees around the x axis, using the base shape as the point to rotate around. We then extrude the small shape again on top of the rotated base shape.

builder.rotate_model(angle=90, axis=Direction.x, around="base").extrude(2).view(
    volume_labels=True,
    angles=(-45, 0, 45),
    save="figs/rotated_model.svg",
    font_size=20,
    zoom=0.8,
    surfaces=True,
)
rotation
/home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/src/qtcad/builder/core/builder.py:3518: UserWarning: Removing orphaned entities: [(1, 17), (1, 14)]
  warnings.warn(

<qtcad.builder.builder.Builder object at 0x7fe14a2d7d90>

Finally, we rotate the model 20 degrees around a tilted axis defined by the vector (1, 1, 1), again using the base shape as the point to rotate around. We then extrude the small shape in the middle of the rotated base shape.

builder.rotate_model(angle=20, axis=[1, 1, 1], around="base").set_z(5).extrude(2).view(
    volume_labels=True,
    angles=(-60, 0, 45),
    save="figs/rotated_model_arbitrary_axis.svg",
    font_size=20,
    zoom=0.8,
    surfaces=True,
    axes=3,
)
rotation
/home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/src/qtcad/builder/core/builder.py:3518: UserWarning: Removing orphaned entities: [(1, 17), (1, 14), (1, 20)]
  warnings.warn(

<qtcad.builder.builder.Builder object at 0x7fe14a2d7d90>

Translation

We can also translate the model. As an example, we first undo the previous rotation.

builder.rotate_model(angle=-20, axis=[1, 1, 1], around="base").view(
    volume_labels=True,
    angles=(-60, 0, 45),
    save="figs/befro_rotation.svg",
    font_size=20,
    zoom=0.8,
    surfaces=True,
    axes=3,
)
rotation
<qtcad.builder.builder.Builder object at 0x7fe14a2d7d90>

Then, we translate the model 5 units in the x direction and then extrude a scaled down version of the circle on top of the translated base shape.

builder.translate_model((6, 0, 0)).use_shape("small").copy_shape(
    scale=0.5, name="smaller"
).extrude(2).view(
    volume_labels=True,
    angles=(-60, 0, 45),
    save="figs/translated_model.svg",
    font_size=20,
    zoom=0.8,
    surfaces=True,
    axes=3,
)
rotation
/home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/src/qtcad/builder/core/builder.py:3518: UserWarning: Removing orphaned entities: [(1, 17), (1, 23), (1, 14), (1, 20)]
  warnings.warn(

<qtcad.builder.builder.Builder object at 0x7fe14a2d7d90>

Total running time of the script: (0 minutes 8.091 seconds)

Gallery generated by Sphinx-Gallery