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

> Learn to slice CAD models to G-code using the text-to-cad repository. Convert 3D mesh files into printer-ready G-code with a simple browser-based preview.

- Repository: [earthtojake/text-to-cad](https://github.com/earthtojake/text-to-cad)
- Tags: how-to-guide
- Published: 2026-07-22

---

**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`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/skills/gcode/scripts/gcode/cli.py), which handles the external process invocation.

According to [`skills/gcode/SKILL.md`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/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

```bash

# 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

```python
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

```bash

# 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:

- **[`skills/gcode/SKILL.md`](https://github.com/earthtojake/text-to-cad/blob/main/skills/gcode/SKILL.md)** – Documents the skill contract, required slicer CLI dependencies, and input/output specifications.
- **[`skills/gcode/scripts/gcode/cli.py`](https://github.com/earthtojake/text-to-cad/blob/main/skills/gcode/scripts/gcode/cli.py)** – Python entry point that orchestrates validation, invokes the external slicer, and handles file I/O.
- **[`viewer/src/client/components/workbench/GcodeFileSheet.js`](https://github.com/earthtojake/text-to-cad/blob/main/viewer/src/client/components/workbench/GcodeFileSheet.js)** – React component that generates interactive G-code previews using the cadjs library.
- **[`viewer/src/client/components/workbench/fileStatusItems.js`](https://github.com/earthtojake/text-to-cad/blob/main/viewer/src/client/components/workbench/fileStatusItems.js)** – Provides UI warnings and validation feedback for G-code files.
- **[`viewer/src/client/components/workbench/entryIconKind.js`](https://github.com/earthtojake/text-to-cad/blob/main/viewer/src/client/components/workbench/entryIconKind.js)** – Maps file extensions to appropriate UI icons, including the G-code file type indicator.

## 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`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/skills/gcode/SKILL.md). The [`cli.py`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/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`](https://github.com/earthtojake/text-to-cad/blob/main/skills/gcode/SKILL.md).