How to Export CAD Models to STL, 3MF, and GLB in Text-to-CAD

The earthtojake/text-to-cad repository exports primary STEP models to STL, 3MF, and native GLB formats by generating side-car mesh files through command-line flags or API calls that invoke specialized exporters in the cadpy and implicitjs packages.

The CAD skill in text-to-cad generates geometric models as STEP files by default, but supports additional mesh exports for 3D printing and visualization workflows. By specifying output paths with the correct extensions, the system automatically invokes format-specific exporters to create binary mesh side-cars. This capability is exposed through both a command-line interface and programmatic Python and JavaScript APIs.

Exporting via Command-Line Interface

The CLI implementation in skills/cad/scripts/step/cli.py defines the step command, which accepts optional flags for mesh generation. When you provide the --stl, --3mf, or --glb arguments followed by file paths, the parser constructs a GenerationSpec that instructs the engine to produce the corresponding mesh files alongside the primary STEP output.


# Generate STEP with STL, 3MF, and GLB side-cars

cad step \
  --input my_part.step \
  --output my_part.step \
  --stl meshes/my_part.stl \
  --3mf meshes/my_part.3mf \
  --glb meshes/my_part.glb

The system validates that provided paths end with the correct expected_suffixes before invoking the exporters.

Programmatic Export with Python

For custom workflows, the cadpy package located at viewer/packages/cadpy/src/cadpy/generation.py provides the generate() function. This function accepts a dictionary mapping format keys to output paths, then routes to the appropriate exporter based on the file extension.

The following exporters handle the mesh conversion:

  • export_part_stl_from_scene (from cadpy.stl) writes binary STL files
  • export_part_three_mf_from_scene (from cadpy.three_mf) creates 3MF containers
  • export_part_glb_from_scene (from cadpy.glb) generates GLB scenes
from cadpy.generation import generate
from pathlib import Path

spec = {
    "step_path": Path("output/assembly.step"),
    "stl_path": Path("output/assembly.stl"),
    "three_mf_path": Path("output/assembly.3mf"),
    "glb_path": Path("output/assembly.glb")
}

generate(spec)

Implicit CAD JavaScript Export

When generating models with the implicit CAD skill, the JavaScript exporter module at viewer/packages/implicitjs/src/lib/implicitCad/exporters.js provides the exportMesh() function. This utility maps format strings to the underlying export logic while handling mesh resolution parameters.

import { exportMesh } from '@cadskills/implicitjs/lib/implicitCad/exporters.js';

// Export an implicit CAD mesh to multiple formats
exportMesh(mesh, { format: 'stl', path: 'meshes/model.stl' });
exportMesh(mesh, { format: '3mf', path: 'meshes/model.3mf' });
exportMesh(mesh, { format: 'glb', path: 'meshes/model.glb' });

Viewer Integration and File Discovery

Once exported, mesh files are automatically indexed by the CAD Viewer without additional configuration. The server module viewer/src/server/catalog/cadDirectoryScanner.mjs maintains a set of recognized source extensions including .step, .stl, .3mf, and .glb, enabling the UI to list, preview, and download generated assets immediately after creation.

Summary

  • CLI flags (--stl, --3mf, --glb) in skills/cad/scripts/step/cli.py trigger side-car mesh generation when exporting CAD models.
  • The cadpy generation engine delegates to export_part_stl_from_scene, export_part_three_mf_from_scene, and export_part_glb_from_scene based on the GenerationSpec paths provided to generate().
  • Implicit CAD workflows use the exportMesh() function from viewer/packages/implicitjs/src/lib/implicitCad/exporters.js to produce JavaScript-driven exports.
  • The CAD Viewer automatically detects exported files through the extension registry in viewer/src/server/catalog/cadDirectoryScanner.mjs.

Frequently Asked Questions

Can I export multiple mesh formats simultaneously?

Yes. The generation engine supports multiple concurrent exports. When using the CLI, simply provide all desired flags (--stl, --3mf, --glb) in a single command. In Python, include multiple path keys in the specification dictionary passed to generate(). The engine processes each export sequentially using the appropriate format-specific wrapper.

What is the primary output format versus mesh side-cars?

The primary output is always a STEP (.step or .stp) file containing the precise B-Rep geometry. STL, 3MF, and GLB files are generated as secondary mesh side-cars for applications requiring triangulated surfaces. The underlying exporters convert the STEP geometry to meshes during the export process, ensuring the side-cars remain synchronized with the primary model.

Does the export require specific file extensions?

Yes. The exporters validate paths against expected_suffixes checks. STL outputs must use .stl, 3MF requires .3mf, and GLB expects .glb extensions. The CLI and Python API will raise errors if the provided paths do not match these requirements, ensuring downstream compatibility with the CAD Viewer's file scanner.

How does the Viewer detect newly exported files?

The Viewer server continuously monitors the output directory using the scanner in viewer/src/server/catalog/cadDirectoryScanner.mjs. This module maintains a hardcoded list of supported extensions (.step, .stl, .3mf, .glb), allowing it to index new exports immediately without restarting the service or manually refreshing the catalog.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →