Skip to content

Add fit-to-area scaling and a machine bed-size check - #166

Open
HaikuZen wants to merge 1 commit into
sameer:mainfrom
HaikuZen:fit-to-area-and-bed-check
Open

HaikuZen wants to merge 1 commit into
sameer:mainfrom
HaikuZen:fit-to-area-and-bed-check

Conversation

@HaikuZen

Copy link
Copy Markdown

Adds two options for getting drawings onto a plotter safely: scaling a drawing to fit a given area, and checking that it fits on the machine's bed. Both are available in the CLI, the web settings and JSON settings files. Both are off by default, so existing conversions are unchanged, and settings files without the new fields load as before.

Fit to area

ConversionConfig::fit_to_area: [Option<f64>; 2], CLI --fit-to-area W,H

  • Uniformly scales the drawing so it fits within W × H mm, keeping its aspect ratio, and places its corner at origin. An empty origin axis is treated as 0.
  • Either axis can be left empty (200, or ,150) to leave it unconstrained.
  • Measures the tight bounding box of strokes only, since fills and images aren't plotted. Without this, a fill-only background could make the plotted lines come out smaller than the area. PreprocessTurtle now tracks a strokes-only bounding box for this, and falls back to the full bounding box when there are no strokes. The existing origin and clip path behavior is unchanged.
  • fit_shrink_only (--fit-shrink-only true) only shrinks drawings that are too large, and never enlarges small ones.

Bed-size check

MachineConfig::bed_size: [Option<f64>; 2] and MachineConfig::bed_check: BedCheck, CLI --bed-size W,H and --bed-check error|warn

  • After conversion, checks the bounding box of every stroke against a bed from (0,0) to W × H mm. An axis left empty isn't checked.
  • With error (the default), no g-code is written and the CLI exits with code 1. The error says how far the drawing goes past each edge:
    drawing spans X 54.706..318.491 mm, Y 13.641..325.712 mm, which does not fit the 300 x 200 mm bed (18.491 mm past the right edge, 125.712 mm past the top edge). No g-code was written. Try --fit-to-area or --origin to move the drawing onto the bed, or --bed-check warn to write it anyway.
    
  • With warn, the g-code is written and the message is logged as a warning.
  • Library: GCodeTurtle records the bounds of what it draws. New svg_to_gcode_with_bounds and check_bed_bounds functions are exported, and svg_to_gcode is unchanged.
  • Web: drawings that don't fit are skipped with an alert explaining why, unless "Only warn" is ticked.

Testing

  • New tests: fit to area (scaling up, scaling down with an offset origin, one axis only, each with and without shrink-only) and the bed check (inside, outside on several edges, unchecked axes).
  • cargo test --workspace passes, nightly cargo fmt --check passes, and nightly clippy passes with -D warnings for every feature combination of svg2star and svg2gcode, plus the CLI and web crates.
  • The web app builds for wasm32-unknown-unknown and was tested deployed.

🤖 Generated with Claude Code

Fit to area (`fit_to_area`, `--fit-to-area W,H`):
- Uniformly scales the drawing so its plotted strokes fit within W x H mm,
  preserving aspect ratio, and places its corner at `origin`.
- Either axis may be left empty to leave it unconstrained.
- Measures strokes only, since fills and images are not plotted.
- `fit_shrink_only` / `--fit-shrink-only true` never enlarges drawings.

Bed-size check (`machine.bed_size`, `--bed-size W,H`):
- After conversion, checks every stroke against a bed from (0,0) to W x H mm.
- `bed_check` is `error` by default (no g-code is written, exit code 1);
  `warn` / `--bed-check warn` writes g-code and prints a warning.
- Error messages report how far the drawing overshoots each edge.
- New `svg_to_gcode_with_bounds` and `check_bed_bounds` in the library;
  `svg_to_gcode` is unchanged.

Web UI: new settings fields for fit area, shrink-only, bed size and
warn-only; out-of-bed drawings show an alert and are skipped unless
warn-only is enabled.

Settings without the new fields load unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant