Can Text-to-CAD Generate STL Files? CLI and Python API Guide

Yes, text-to-cad can generate binary STL files using OpenCascade's StlAPI_Writer to serialize CAD geometry as a side-car artifact during STEP generation.

Text-to-cad is an open-source Python framework (earthtojake/text-to-cad) that transforms textual descriptions into manufacturable CAD models. The library includes built-in STL export functionality accessible via command-line flags and direct Python API calls, enabling seamless integration with 3D printing slicers and additive manufacturing workflows.

How STL Export Works in Text-to-CAD

The STL generation pipeline spans three architectural layers: CLI argument parsing, generation orchestration, and low-level geometry serialization using OpenCascade.

CLI Entry Point and Option Parsing

The command-line interface resides in skills/cad/scripts/step/cli.py. When you provide the --stl <path> argument, the code instantiates a StepImportOptions object and populates the stl field with your target path (lines 48-52). This configuration object propagates through the generation pipeline, triggering the mesh export workflow.

Generation Flow Orchestration

In packages/cadpy/src/cadpy/generation.py, the system inspects the StepImportOptions immediately after loading the STEP scene. If the stl attribute is present, the generator calls export_part_stl_from_scene before completing the workflow (lines 285-298). This design ensures STL files are produced synchronously with CAD generation, maintaining path consistency and error handling context.

Low-Level STL Writing Implementation

The actual binary serialization occurs in packages/cadpy/src/cadpy/stl.py. The export_shape_stl function implements the export logic:

  • Creates missing parent directories for the target file path
  • Instantiates an StlAPI_Writer from the OpenCascade kernel
  • Explicitly configures binary mode via writer.ASCIIMode = False
  • Writes the geometric shape to the specified destination
  • Raises a RuntimeError with a repo-relative path description (via _display_path) if the writer fails

This implementation guarantees compact, standards-compliant binary STL output suitable for direct consumption by manufacturing equipment.

Generating STL Files via Command Line

You can produce STL side-car files when processing existing STEP files or Python generator scripts.

Basic STEP to STL Conversion


# Generate STEP and accompanying STL

python scripts/step my_model.step --kind part --stl meshes/my_model.stl
  • my_model.step is the input CAD file
  • --kind part specifies single-part extraction (required for STL export)
  • --stl meshes/my_model.stl creates the binary mesh in the specified directory

Generating from Python Generator Scripts

For procedural models defined in Python (e.g., my_model.step.py), the syntax remains identical:

python scripts/step my_model.step.py --stl meshes/generated.stl

The generator executes, constructs the STEP scene internally, and exports the STL geometry to your target path.

Programmatic STL Export with the Python API

For custom automation pipelines, import the export functions directly from the cadpy package.

from pathlib import Path
from cadpy.step_scene import load_step_scene
from cadpy.stl import export_part_stl_from_scene

# Load the STEP scene into memory

step_path = Path("my_model.step")
scene = load_step_scene(step_path)

# Export the part geometry to STL

stl_path = export_part_stl_from_scene(
    step_path,
    scene,
    target_path=Path("meshes/my_model.stl")
)

print(f"STL written to: {stl_path}")

Implementation details:

  • load_step_scene returns a LoadedStepScene object containing parsed OpenCascade geometry
  • export_part_stl_from_scene extracts the part shape and delegates to export_shape_stl, using the same binary writer as the CLI
  • The function returns the resolved Path object upon successful completion

Supported Workflows and Output Characteristics

Text-to-cad treats STL as a side-car artifact, generating it alongside the primary STEP file. The framework also supports GLB and 3MF exports through analogous hooks in packages/cadpy/src/cadpy/generation.py.

The export process always produces binary STL (never ASCII), ensuring optimal file sizes for complex geometries. Because the underlying OpenCascade kernel handles the tessellation, the output maintains proper watertight manifold properties required for 3D printing slicers.

Summary

  • Text-to-cad generates binary STL files via OpenCascade's StlAPI_Writer as implemented in packages/cadpy/src/cadpy/stl.py
  • Enable export using the --stl <path> option in skills/cad/scripts/step/cli.py (lines 48-52)
  • The generation orchestrator in packages/cadpy/src/cadpy/generation.py (lines 285-298) triggers export_part_stl_from_scene after STEP scene loading
  • Use export_part_stl_from_scene and load_step_scene from the Python API for headless workflows
  • Output is strictly binary format with automatic directory creation and descriptive error messages via _display_path

Frequently Asked Questions

Does text-to-cad support ASCII STL format?

No. The source code in packages/cadpy/src/cadpy/stl.py explicitly sets writer.ASCIIMode = False when configuring the StlAPI_Writer. This design choice prioritizes file size efficiency and parsing performance, producing compact binary output suitable for production manufacturing environments.

Can I generate an STL file without creating a STEP file?

Yes. While STL generation typically occurs as a side-car artifact, you can skip the STEP file write operation using the --skip-step-write flag while retaining the STL export functionality. This workflow is useful when you need mesh geometry for slicing but don't require the parametric CAD data for downstream editing.

What Python class handles the actual STL serialization?

The serialization uses OpenCascade's StlAPI_Writer class, not a custom Python implementation. The export_shape_stl function in packages/cadpy/src/cadpy/stl.py instantiates this writer, configures it for binary output, and executes the write operation. If the export fails, the function raises a RuntimeError with a user-friendly path description generated by the internal _display_path helper.

Is STL export limited to single parts or does it support assemblies?

The current API in packages/cadpy/src/cadpy/stl.py provides export_part_stl_from_scene, which is optimized for single solid bodies. The CLI requires the --kind part flag for STL generation, indicating the export pipeline targets individual parts rather than multi-body assemblies. For complex assemblies, export individual components separately or use the STEP format which preserves hierarchical assembly structures.

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 →