Skip to content

About

Refit HP-GL polyline chords into native arc commands — an ArcWelder for 1980s pen plotters. ~185 lines, stdlib-only.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

hpgl-arcweld

An ArcWelder for HP-GL: detects runs of polyline chords that approximate circular arcs in plotter output and refits them into native AA (arc absolute) commands. ~185 lines, stdlib-only Python.

Provenance: extracted in August 2026 from the working script that drives my Roland DXY-1150 pipeline — the pre-extraction development happened outside version control, so history accrues from here.

ArcWelder does this for 3D-printer G-code; this does it for 1980s pen plotters.

The problem

Vector pipelines (vpype, Inkscape, etc.) flatten every curve into hundreds of tiny chords. A modern machine glides through them; the 8-bit motion planner in a plotter like the Roland DXY-1150 executes each chord as a separate move — accelerate, decelerate, dwell, parse the next command over a 9600-baud serial link. On arc-heavy drawings the pen visibly stutters, plots take forever, and the dwell marks show in the ink.

The plotter has native arc support the whole time. arcfit.py gives the flattened curves back to it.

The algorithm

  1. Parse the HP-GL, reconstructing absolute pen-down polylines (handles PU/PD/PA/PR; vpype's PU-absolute + PR-relative style).
  2. From each point, greedily grow a run: fit a circle through the run's first/mid/last points and extend while every point stays within TOL (10 plotter units = 0.25 mm) of the circle and the angles advance monotonically (no doubling back).
  3. Runs of ≥ 5 points become one AA x,y,sweep; command; everything else is re-emitted as compact PA polylines. Rejected as arcs and left as line segments: radii above MAX_R (effectively straight), radii below 4·TOL (circles so tiny the tolerance band is the whole circle), and centers beyond ±30000 (kept safely inside the plotter's ±32767 parameter range).

Tuning lives in the named constants at the top of the file: TOL, MIN_ARC_PTS, MAX_R, MAX_CENTER, MAX_SEG_RUN.

Usage

python3 arcfit.py in.hpgl out.hpgl    # in-place OK: same path twice

or as a module:

import arcfit
polys = arcfit.parse_polylines(hpgl_text)
hpgl_out, n_arcs, n_segments = arcfit.emit(polys)

Measured reduction

examples/demo.py flattens smooth curves into chords at 0.1 mm tolerance (what a typical vpype pipeline emits) and runs them through arcfit:

input chords output pen moves reduction
3 concentric circles 149 3 arcs + 3 segs 24.8x
3:2 lissajous 120 20 arcs + 18 segs 3.2x
combined 269 23 arcs + 21 segs 6.1x

A pen move is one PA coordinate pair or one AA arc — the unit the planner dwells between. (Counted as commands on the wire it is tighter still: the combined output is 30 commands — 29 drawing + 1 home move — since PA moves batch into chunks.)

Bytes over the wire: 2776 → 855 (3.2x). The win scales with how circular the content is — full circles collapse to a single AA, while curves with continuously varying curvature (lissajous, spirals) weld into shorter arc runs. Either way the plotter stops stuttering: one arc command replaces a burst of micro-segment moves.

python3 examples/demo.py     # regenerates examples/demo_{in,out}.hpgl
python3 test_arcfit.py       # unit tests (stdlib unittest)

Pipeline context

This is the smoothing pass in a plot pipeline for a Roland DXY-1150 hanging off a Raspberry Pi:

SVG → vpype (layout, simplify, sort, HPGL) → arcfit.py → serial (9600 8N1, CTS)

examples/plot.sh is the real driver script (set PLOTTER_HOST, e.g. pi@plotter.local); it copies arcfit.py to the Pi itself each run. The ~/plots/send_hpgl.py it calls is the author's own serial sender and is not included here — any HP-GL sender with hardware flow control will do. examples/harmonograph.py generates a damped-lissajous harmonograph SVG — a good arc-heavy torture test to feed the pipeline.

Known limitations

  • Only PU/PD/PA/PR pen geometry is preserved. Everything else — including SP pen-selects and CI circles — is dropped from the output, which contains only IN;DF; plus PU/PD/PA/AA. Multi-pen plots must handle pen changes outside this tool (which is why plot.sh --sp skips the arcfit pass).
  • Coordinates are rounded to integer plotter units (0.025 mm) on parse.
  • LB label text is not skipped by the parser, so labels can yield phantom geometry (irrelevant for vpype output, which emits none).
  • Consecutive arcs chain without re-anchoring: each AA continues from the previous arc's endpoint, so fit error compounds along a chain. Verified by simulating the emitted arcs back to points against the input: the worst radial residual against the fitted circle on the demo paths is 9.83 plotter units (0.246 mm) — inside TOL = 10, but near it. Reproduce with python3 examples/verify_fidelity.py (also run in CI). Tighten TOL and the chains tighten with it; an explicit PA re-anchor per arc would cost one extra move per chained arc, eroding the reduction.
  • Future: redundant PU;PU pairs between polylines are a free ~3-command win, left unoptimized to keep the committed evidence files stable.

Before / after

Physical before/after plot: coming after the next plotting session.

License

MIT

About

Refit HP-GL polyline chords into native arc commands — an ArcWelder for 1980s pen plotters. ~185 lines, stdlib-only.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages