Skip to content

Simulate a G-code Bookmark

Category Creative & Real-World AI Builds
Status โœ… Completed
Repo nextwork-simulate-a-g-code-bookmark
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Simulate a G-code Bookmark guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Creative & Real-World AI Builds
Status: โœ… Completed

Overview

G-code is just a list of moves, so a simulator can be small: the same parser logic lives in Python for the report and in JavaScript for the drawing. Travel moves (G0) draw in grey, extrusion moves (G1 with E) in colour, and the analyzer attaches numbers to every warning. I checked Claude Code's work by running its tests with real pytest and driving the page in a headless browser instead of trusting its summary.

Summary

I had the real Claude Code CLI build the whole thing from one prompt: a generator for a 40 x 120 mm, three-layer bookmark (two base layers, then five star motifs on the centre line), an analyzer that reports layers, travel versus extrusion, filament, print time and open-chamber PLA findings, a single-file viewer with replay, and 34 tests. python generate.py 4 adds a fourth layer that raises only the centre star. I ran the real pytest run and a headless-Chrome check of the viewer myself, because Claude Code's sandbox couldn't.

The checks behind this are in docs/verification.txt.

Skills & tools

Claude Code (headless), G-code, 3D printing, Python, pytest, Canvas visualization

What I built

  • generate.py: Marlin-style G-code for the bookmark, with PLA start and end code, fan off on layer 1, G0 travels and G1 extrusions. python generate.py 4 out.gcode raises only the centre star.
  • analyze.py: layers, travel versus extrusion, filament, time and a per-layer table, plus findings with severity and numbers (text, or --json).
  • viewer.html: file picker, layer filter, Replay button, and finding cards over the drawing; it parses the G-code in the page. docs/viewer.png is a screenshot of a replay.
  • tests/: 34 pytest tests for the generator, analyzer and 4-layer rule. viewcheck.py opens the viewer in headless Chrome and checks the drawing, the layer filter, replay and cards.
  • prompts/ and docs/session-log.md: my one prompt and the readable log of the session, including what Claude Code said it couldn't verify.

How to run

python3 generate.py 4 out.gcode && python3 analyze.py out.gcode
python3 -m pytest -q
open viewer.html   # choose out.gcode

Verification

I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.

python3 generate.py 4 /tmp/gv4.gcode && python3 analyze.py /tmp/gv4.gcode | grep -q 'Layers:            4' && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider | grep -q '34 passed' && python3 viewcheck.py $PWD/viewer.html /tmp/gv4.gcode /tmp/gv4.png | grep -q 'VIEWER OK' && echo GCODE OK

Notes

Claude Code couldn't install pytest or run the page's JavaScript in its sandbox, so it ran the tests with a throwaway runner and said so; I ran the real pytest and the browser check afterwards. It also added one thing I hadn't asked for: layers under 12 seconds are slowed so the top layer can cool. The findings logic exists in both analyze.py and viewer.html, so a threshold has to be changed in both.


Original project: https://nextwork.ai/projects/2f426b4f-76e6-484e-bbd7-ad65b56dc20d ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ python3 generate.py 4 /tmp/gv4.gcode && python3 analyze.py /tmp/gv4.gcode | grep -q 'Layers:            4' && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider | grep -q '34 passed' && python3 viewcheck.py $PWD/viewer.html /tmp/gv4.gcode /tmp/gv4.png | grep -q 'VIEWER OK' && echo GCODE OK

wrote /tmp/gv4.gcode: 4 layers
GCODE OK

[exit code 0]