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",
)
)
[03:23:20 PM] INFO Auto-named Polygon instance to 'box' masks.py:231
[15:23:20] INFO Using mask 'default' (implicitly selecting builder.py:801
shapes [Polygon 0 'box'])
INFO Extruding shape 0 from mask 'default' with name builder.py:2794
'box' by 3 at z=0
INFO Creating new physical group with name builder.py:2580
'box_bottom'
INFO Creating new physical group with name builder.py:2580
'box_side'
INFO Creating new physical group with name 'box_top' builder.py:2580
INFO Creating new physical group with name builder.py:2580
'box_hull'
INFO Creating new physical group with name 'box' builder.py:2580
INFO Setting z-coordinate to 3 builder.py:903
[15:23:21] INFO Saving figure builder.py:1926
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,
)
)
INFO Importing model from builder.py:708
/home/hiro/Documents/roam_code/code/qtcad_flake/
qtcad/packages/builder/examples/operations/model
s/demo.STEP
[15:23:22] INFO Creating new physical group with name builder.py:2580
'imported_shape_bottom'
INFO Creating new physical group with name builder.py:2580
'imported_shape_side'
INFO Creating new physical group with name builder.py:2580
'imported_shape_top'
INFO Creating new physical group with name builder.py:2580
'imported_shape_hull'
INFO Fragmenting... fragmenter.py:218
[15:24:07] INFO Creating new physical group with name builder.py:2580
'box.imported_shape_bottom'
INFO Creating new physical group with name builder.py:2580
'box.imported_shape'
INFO Creating new physical group with name builder.py:2580
'box.imported_shape_side'
INFO Creating new physical group with name builder.py:2580
'box_top.imported_shape'
INFO Creating new physical group with name builder.py:2580
'imported_shape'
[15:24:08] INFO Saving figure builder.py:1926
<qtcad.builder.builder.Builder object at 0x75dd41920d70>
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: (0 minutes 50.907 seconds)