Note
Go to the end to download the full example code.
Cutting and etching
This example demonstrates the qtcad.builder.Builder.cut and
qtcad.builder.Builder.etch operations, as well as how to save and
restore the z-coordinate.
Initialization
from qtcad.builder import Builder, Polygon, Mask
Initializing the Builder
Below, we initialize a new instance of qtcad.builder.Builder and
define a couple of simple shapes we are going to use in the following.
footprint = Polygon.box(10, 10, name="base").centered()
circle = Polygon.regular(10, 1.8, name="circ").centered()
thin = Polygon.box(10, 0.4, name="thin").centered()
box_mask = Mask("footprint")
box_mask.add_shape(footprint)
box_mask.add_shape(circle)
box_mask.add_shape(thin)
builder = Builder().add_mask(box_mask)
builder.print_mask_tree()
Layout
└── Mask 0 "footprint"
├── 0 Polygon "base"
├── 1 Polygon "circ"
└── 2 Polygon "thin"
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
Base geometry
Next, select the generate a geometry with an uneven surface. We use
qtcad.builder.Builder.set_group_name to tell the Builder
to assign all subsequently created entities to the physical group
"model_base". We also use qtcad.builder.Builder.copy_shape to
create two translated versions of the circle shape. Using
qtcad.builder.Builder.save_z and qtcad.builder.Builder.set_z we
can save and restore the z-coordinate of the top of the "base"
volume.
builder = (
builder.set_group_name("model_base")
.set_mesh_size(10)
.use_mask("footprint")
.use_shape("base")
.extrude(2)
.save_z("box_top")
.use_shape("circ")
.copy_shape(translate=(0, 2))
.extrude(1)
.set_z("box_top")
.copy_shape(translate=(0, -4))
.extrude(1)
)
[01:53:17 PM] INFO Using mask 'footprint' builder.py:750
INFO Extruding shape 0 from mask 'footprint' with name 'base' by 2 at z=0 builder.py:2695
INFO Creating new physical group with name 'model_base_bottom' builder.py:2482
INFO Creating new physical group with name 'model_base_side' builder.py:2482
INFO Creating new physical group with name 'model_base_top' builder.py:2482
INFO Creating new physical group with name 'model_base' builder.py:2482
INFO Setting z-coordinate to 2 builder.py:849
INFO Extruding shape 3 from mask 'footprint' with name 'circ' by 1 at z=2 builder.py:2695
INFO Fragmenting... fragmenter.py:234
[01:53:18 PM] INFO Setting z-coordinate to 3 builder.py:849
INFO Setting z-coordinate to 2 builder.py:849
INFO Extruding shape 4 from mask 'footprint' with name 'circ' by 1 at z=2 builder.py:2695
INFO Fragmenting... fragmenter.py:234
INFO Setting z-coordinate to 3 builder.py:849
As promised, all volumes share the same physical group:
builder.view(
surfaces=False,
volume_labels=True,
angles=(-45, 0, 45),
save="figs/cut_base.svg",
font_size=15,
zoom=0.8,
)
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/cut_base.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
Cutting
- Builder.cut(depth: float, noclip: bool = False) Self
[🏗️ Operation] Cut the currently selected shape(s) into the model up from the current z coordinate down by
depthlength units. Ifnoclip=True, the cutting is performed on the whole model rather than clipped by the selected shapes.See also
etchwhich follows the contours of the top surface.- Parameters:
depth – The distance by which to etch the selected shapes down into the model.
noclip – Whether to cut the shape without horizontally clipping to the current shapes.
Next we will make a cut of depth 1 through the first tube shape.
builder.group_from_shape().use_shape("thin").copy_shape(
name="cut", translate=(0, 2)
).cut(1)
[01:53:19 PM] INFO Cutting shape 5 with name 'cut' by 1 at z=3 builder.py:528
INFO Extruding shape 5 from mask 'footprint' with name 'cut' by -1 at z=3 builder.py:2695
[01:53:20 PM] INFO Creating new physical group with name 'cut_bottom' builder.py:2482
INFO Creating new physical group with name 'cut_side' builder.py:2482
INFO Creating new physical group with name 'cut_top' builder.py:2482
INFO Fragmenting... fragmenter.py:234
[01:53:22 PM] INFO Creating new physical group with name 'cut' builder.py:2482
INFO Setting z-coordinate to 2 builder.py:849
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
The cut operation effectively extrudes the currently selected shape(s) downwards and then removes the intersection with existing geometry.
builder.view(
surfaces=True,
volume_labels=True,
surface_labels=False,
angles=(-45, 0, 45),
save="figs/cut_result.svg",
font_size=15,
zoom=0.8,
)
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/cut_result.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
The created surfaces are labeled base on the current naming mode (see Selection of shapes and naming modes).
builder.view(
surfaces=False,
volume_labels=False,
surface_labels=True,
angles=(-45, 0, 45),
save="figs/cut_surfs.svg",
font_size=15,
zoom=0.8,
groups=["cut_bottom", "cut_side"],
)
[01:53:25 PM] INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/cut_surfs.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
Etching
- Builder.etch(depth: float, noclip: bool = False) Self
[🏗️ Operation] Etch the currently selected shape(s) into the model up from the current z coordinate down by
depthlength units. This is done by cutting into each top surface. IfnoclipisTrue, then the surface is not clipped to the currently selected shapes.See also
cut,depositandgrow.- Parameters:
depth – The distance by which to etch the selected shapes down into the model. Must be positive.
In contrast to cutting, etching follows the contour of the top surface.
builder.use_shape("thin").copy_shape(name="etch", translate=(0, -2)).etch(1)
builder.view(
surfaces=False,
volume_labels=True,
surface_labels=False,
angles=(-45, 0, 45),
save="figs/etch_result.svg",
font_size=15,
zoom=0.8,
)
[01:53:26 PM] INFO Etching shape 6 with name 'etch' by 1 builder.py:560
[01:53:27 PM] INFO Creating new physical group with name 'etch_bottom' builder.py:2482
INFO Creating new physical group with name 'etch_side' builder.py:2482
INFO Creating new physical group with name 'etch_top' builder.py:2482
INFO Fragmenting... fragmenter.py:234
[01:53:30 PM] INFO Creating new physical group with name 'etch' builder.py:2482
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/etch_result.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
Again the resulting surfaces are named accordingly.
builder.view(
surfaces=False,
volume_labels=False,
surface_labels=True,
angles=(-45, 0, 45),
save="figs/etch_surfs.svg",
font_size=15,
zoom=0.8,
groups=["etch_bottom", "etch_side"],
)
[01:53:31 PM] INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/etch_surfs.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d8ea5d0>
Total running time of the script: (0 minutes 15.740 seconds)