How to Slice CAD Models to G-code with text-to-cad

The text-to-cad repository provides a G-code skill that converts 3D mesh files (STEP, STL, 3MF, GLB) into printer-ready FDM G-code through a three-stage pipeline involving input validation, command-line slicer execution, and interactive browser-based preview.

The earthtojake/text-to-cad open-source project includes a dedicated G-code skill that automates the conversion of CAD geometries into machine-readable instructions. This skill integrates mesh parsing, external slicer orchestration, and real-time visualization to streamline the transition from digital models to physical prints.

The G-code Skill Architecture

The slicing pipeline implemented in skills/gcode/ follows a structured three-layer approach to transform raw meshes into executable toolpaths.

Input Handling and Geometry Validation

Before slicing begins, the system validates incoming mesh files using the shared cadjs library. This parsing layer ensures that STEP, STL, 3MF, or GLB files are well-formed and geometrically sound before processing.

In viewer/src/client/components/workbench/fileStatusItems.js, the code manages G-code-specific warnings and validation states. This component supplies status indicators that alert users to potential issues such as unsupported units or malformed geometry prior to slicer invocation.

Slicing Engine Execution

Once validated, the mesh passes to a command-line slicer such as CuraEngine or Slic3r. The orchestration logic resides in skills/gcode/scripts/gcode/cli.py, which handles the external process invocation.

According to skills/gcode/SKILL.md, the skill accepts a printer-profile JSON defining critical parameters including nozzle diameter, filament type, layer height, and print temperature. The CLI constructs the appropriate command-line arguments, executes the slicer, and captures the resulting G-code stream.

Preview and Post-Processing Visualization

After generation, the G-code feeds back into the CAD Viewer for verification. The GcodeFileSheet component in viewer/src/client/components/workbench/GcodeFileSheet.js utilizes cadjs/lib/gcode/buildPreviewMesh to construct an interactive 3D visualization.

This preview renders distinct geometric elements including layer outlines, travel moves, and extrusion paths directly in the browser. Users can inspect toolpath efficiency and verify print parameters before committing to physical production.

Command-Line Usage Examples

The G-code skill supports both CLI invocation and direct Python API integration.

Skill-CLI Interface


# Install the skill (once)

npx skills install earthtojake/text-to-cad

# Slice a mesh to G-code

npx skills run gcode \
  --input models/part.stl \
  --output prints/part.gcode \
  --profile printers/my_printer.json

Python API Integration

from skills.gcode import slice_to_gcode

gcode = slice_to_gcode(
    mesh_path="models/part.glb",
    printer_profile="printers/my_printer.json",
    extra_args=["--infill", "20"]
)

with open("prints/part.gcode", "w") as fp:
    fp.write(gcode)

CAD Viewer Preview


# Start the viewer

npm --prefix viewer run start

# Open the file in the browser; the viewer will render the G-code preview

http://localhost:3000/?file=gcode%2Fpart.gcode&theme=dark

Key Implementation Files

Understanding the source structure helps when customizing the slicing pipeline:

Summary

  • The text-to-cad G-code skill converts STEP, STL, 3MF, and GLB files into FDM-ready G-code through a validated three-stage pipeline.
  • Input validation occurs via the cadjs library in fileStatusItems.js before external slicers like CuraEngine or Slic3r process the geometry.
  • The slice_to_gcode Python function and Skill-CLI provide both programmatic and command-line interfaces for automation.
  • Generated G-code can be visualized interactively in the CAD Viewer using the GcodeFileSheet component and buildPreviewMesh utility.
  • Printer-specific configurations are defined via JSON profiles passed to the slicer CLI.

Frequently Asked Questions

What mesh formats does the text-to-cad G-code skill support?

The skill accepts STEP, STL, 3MF, and GLB files as input. The cadjs library parses these formats in viewer/src/client/components/workbench/fileStatusItems.js to ensure geometric validity before slicing begins.

Which slicer engines are compatible with the G-code skill?

The skill invokes command-line slicers such as CuraEngine or Slic3r as specified in skills/gcode/SKILL.md. The cli.py script constructs the appropriate command-line arguments based on the provided printer profile JSON.

How can I preview G-code before printing?

The CAD Viewer renders interactive previews through the GcodeFileSheet component in viewer/src/client/components/workbench/GcodeFileSheet.js. This component uses cadjs/lib/gcode/buildPreviewMesh to visualize layers, travel moves, and extrusion paths directly in the browser.

Can I customize slicing parameters like infill density?

Yes. When using the Python API, pass additional slicer arguments via the extra_args parameter in slice_to_gcode(). For CLI usage, modify the printer profile JSON or append slicer-specific flags according to the skill documentation in skills/gcode/SKILL.md.

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 →