How to Export CAD Models to STL and GLB Formats Using text-to-cad
To export CAD models to STL and GLB formats in the text-to-cad repository, use the scripts/step CLI with the --stl and --glb flags, which generate side-car files derived from the primary STEP artifact.
The text-to-cad repository follows a STEP-first principle where STEP (or STP) files serve as the canonical CAD artifacts. When you export CAD models to STL and GLB formats, the system first validates the STEP export, then creates mesh side-cars using the internal cadpy generation pipeline. This workflow ensures that all derived formats reflect the current, validated geometry.
Understanding the STEP-First Architecture
The repository enforces a strict hierarchy where STEP files are always generated before any mesh exports. According to the source code in skills/cad/references/supported-exports.md, STL, 3MF, and GLB files are treated as side-cars that cannot exist independently of their parent STEP file.
The export configuration is managed through the StepImportOptions dataclass defined in skills/cad/scripts/step/cli.py. This class carries optional output paths for side-car formats along with shared mesh tolerances. When you invoke the CLI, the _step_import_options_from_args function (around line 90) constructs this options object from your command-line flags.
Exporting via the Command Line Interface
The scripts/step CLI provides the most direct method to export CAD models to STL and GLB formats. The tool accepts a target—either a Python generator file, an existing STEP file, or a SOURCE.py=OUTPUT.step pair—and processes it through the generation pipeline.
Basic STL Export
Run the following command to generate an STL side-car from a Python generator:
python -m scripts.step my_assembly.py --stl builds/my_assembly.stl
The CLI executes the gen_step() function within my_assembly.py, writes the canonical STEP file, then calls export_part_stl_from_scene from packages/cadpy/src/cadpy/stl.py to create the binary STL mesh.
Basic GLB Export
To generate a GLB file from an existing STEP artifact:
python -m scripts.step path/to/part.step --glb outputs/part.glb
The system loads the STEP scene, processes it through the GLB topology pipeline in cadpy.glb_topology, and writes a Y-up GLB file to the specified location.
Simultaneous STL and GLB Export
You can export both formats in a single command while controlling mesh quality:
python -m scripts.step assembly.py \
--stl outputs/assembly.stl \
--glb outputs/assembly.glb \
--mesh-tolerance 0.001
This ensures both side-cars share identical mesh tolerances, producing consistent geometric detail across formats.
Programmatic Export with the Python API
For CI pipelines or automated workflows, import the generation utilities directly instead of using the CLI. The generate_step_targets function in packages/cadpy/src/cadpy/generation.py orchestrates the entire export process.
from cadpy.generation import generate_step_targets
from cadpy.catalog import StepImportOptions
opts = StepImportOptions(
stl='my_part.stl',
glb='my_part.glb',
mesh_tolerance=0.0005,
)
generate_step_targets(
targets=['my_part_generator.py'],
step_options=opts,
force=True,
)
This approach mirrors the CLI behavior described in cli.py, creating the STEP artifact first, then queueing side-car jobs for STL and GLB generation based on the provided StepImportOptions.
Technical Implementation of Side-Car Generation
The export pipeline handles STL and GLB creation through distinct internal mechanisms, both triggered within generate_step_targets after successful STEP validation.
STL Export Pipeline
When options.stl is set, the generation pipeline adds a side-car job named "STL". This job invokes export_part_stl_from_scene from packages/cadpy/src/cadpy/stl.py (lines 22-27):
export_part_stl_from_scene(step_path, scene, target_path=options.stl)
This function extracts the shape from the STEP scene using scene_export_shape(scene) and writes a binary STL file via the internal export_shape_stl utility.
GLB Export Pipeline
For GLB exports, the pipeline references part_glb_path from packages/cadpy/src/cadpy/render.py (lines 57-58) to resolve the canonical output location. The generation code in packages/cadpy/src/cadpy/generation.py (around lines 1750-1760) adds a "GLB" side-car job when options.glb is present. This job serializes the STEP topology using the GLB topology utilities, producing a native GLB file with Y-up orientation suitable for web viewers and downstream CAD pipelines.
Summary
- STEP-first workflow: The text-to-cad repository requires successful STEP generation before creating any STL or GLB side-cars.
- CLI flags: Use
--stland--glbwith thescripts/stepmodule to specify export targets. - Configuration: The
StepImportOptionsdataclass incli.pycarries export paths and mesh tolerances into the generation pipeline. - STL generation: Handled by
export_part_stl_from_sceneinpackages/cadpy/src/cadpy/stl.py, which converts the STEP scene to a binary mesh. - GLB generation: Orchestrated through
generate_step_targetsusing the GLB topology pipeline, with paths resolved bypart_glb_pathinrender.py.
Frequently Asked Questions
Can I export STL and GLB files without generating a STEP file?
No. According to the source code in supported-exports.md and the implementation in generation.py, the repository enforces a STEP-first principle. The STL and GLB exports are side-cars derived from the validated STEP artifact; the pipeline always generates or validates the STEP file first before processing side-car jobs.
What is the default output location for side-car files?
If you do not specify explicit paths via --stl or --glb, the system uses canonical path resolvers defined in packages/cadpy/src/cadpy/render.py. The part_stl_path(step_path) and part_glb_path(step_path) functions derive default locations based on the source STEP file's directory and basename.
How do I control mesh quality for STL exports?
Use the --mesh-tolerance flag in the CLI or set the mesh_tolerance parameter in StepImportOptions. This value is passed through the generation pipeline to the meshing algorithms in cadpy/stl.py, controlling the geometric fidelity of the exported STL relative to the original STEP geometry.
Are these exports suitable for 3D printing?
Yes. The STL side-cars generated by export_part_stl_from_scene produce binary STL files compatible with standard slicer software. For 3D printing workflows, specify an appropriate --mesh-tolerance (typically between 0.001 and 0.01 depending on your printer resolution) to balance file size with surface detail.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →