Note
Go to the end to download the full example code.
Importing CAD modelsο
In this brief example, we demonstrate how to import pre-made CAD models.
We first initialize a new instance of qtcad.builder.Builder
and create a box for reference.
from qtcad.builder import Builder, Direction, Polygon, Mask
from pathlib import Path
import sys
script_dir = Path(sys.argv[0]).resolve().parent
box = Polygon.box(10, 10).centered()
default_mask = Mask("default", shapes=[box])
builder = (
Builder()
.add_mask(default_mask)
.use_mask("default")
.extrude(3)
.view(
angles=(-45, 0, 0),
volume_labels=True,
surface_labels=True,
surfaces=True,
save="figs/default_shape.svg",
)
)
[01:49:19 PM] INFO Auto-named Polygon instance to 'box' masks.py:208
[01:49:20 PM] INFO Using mask 'default' builder.py:750
INFO Extruding shape 0 from mask 'default' with name 'box' by 3 at z=0 builder.py:2695
INFO Creating new physical group with name 'box_bottom' builder.py:2482
INFO Creating new physical group with name 'box_side' builder.py:2482
INFO Creating new physical group with name 'box_top' builder.py:2482
INFO Creating new physical group with name 'box' builder.py:2482
INFO Setting z-coordinate to 3 builder.py:849
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/default_shape.svg builder.py:1863
We then import a CAD model from a STEP file using qtcad.builder.Builder.import_model.
- Builder.import_model(file_path: str | Path, format: str = '', translate: Iterable[float] | None = None, scale: float = 1.0, rotate: float = 0.0, rotation_axis: Iterable[float] | str = Direction.z) Self
[ποΈ Operation] Import a Gmsh model from file
file_path. The model can be in any format that Gmsh supports (e.g..iges,.step,.brep).- Parameters:
file_path β The path to the file to import.
format β The format of the file. If empty (default), the format will be guessed from the file extension. Can be βigesβ, βstepβ, βbrepβ.
translate β If provided, a 3-vector by which to translate the model after import.
scale β Scaling factor to apply to the model after import.
rotate β Angle in degrees by which to rotate the model around its centre of mass.
To set the name of the resulting volume group (and surface groups),
we use set_group_name before importing.
(
builder.set_group_name("imported_shape")
.overlay_mode()
.import_model(
script_dir / "models" / "demo.STEP",
rotation_axis=Direction.x,
rotate=90,
scale=0.07,
translate=(0, 0, 3),
)
.view(
angles=(-45, 0, 0),
volume_labels=True,
surface_labels=True,
surfaces=True,
save="figs/imported_shape.svg",
font_size=5,
)
)
[01:49:22 PM] INFO Importing model from /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/models/demo.STEP builder.py:673
[01:51:05 PM] INFO Creating new physical group with name 'imported_shape_bottom' builder.py:2482
INFO Creating new physical group with name 'imported_shape_side' builder.py:2482
INFO Creating new physical group with name 'imported_shape_top' builder.py:2482
INFO Fragmenting... fragmenter.py:234
[01:52:25 PM] INFO Creating new physical group with name 'box.imported_shape_bottom' builder.py:2482
INFO Creating new physical group with name 'box.imported_shape' builder.py:2482
INFO Creating new physical group with name 'box.imported_shape_side' builder.py:2482
INFO Creating new physical group with name 'box_top.imported_shape' builder.py:2482
INFO Creating new physical group with name 'imported_shape' builder.py:2482
INFO Saving /home/hiro/Documents/org/roam/code/qtcad_flake/qtcad/packages/builder/examples/operations/figs/imported_shape.svg builder.py:1863
<qtcad.builder.builder.Builder object at 0x7f026d9f12b0>
Note that the resulting surfaces are automatically labeled base on
their orientation. The imported shape is integrated into the
existing geometry according to the current
qtcad.builder.GroupingMode (see also Extrusion and fragmentation modes).
Total running time of the script: (3 minutes 13.963 seconds)