Skip to content

feat: nesting (auto-arrange) to keep the largest reusable offcut - #15

Open
inzuno wants to merge 2 commits into
SJLS27:mainfrom
inzuno:auto-nesting
Open

inzuno wants to merge 2 commits into
SJLS27:mainfrom
inzuno:auto-nesting

Conversation

@inzuno

@inzuno inzuno commented Dec 2, 2026

Copy link
Copy Markdown

Builds on #14. This branch starts from click-selection-option, so its first commit (b5f0e62) is #14. Merging #14 first leaves only the two nesting commits here (e81d3e1, 1245c8b).

Problem

Laser parts are usually laid out by hand on a board. With many irregular parts (petals, leaves, rings with holes…) it is slow, and the offcut left over is rarely a shape that can be reused. This is the "Nesting / auto-arrange" item of the P4 roadmap in TODO.md.

Change

New Arrange → Nesting… dialog:

  • Sheet: size and position on the bed, shown in a live preview.
  • Spacing between parts and margin to the sheet edge.
  • Parts: the selection or every object. Each top-level object (usually a group) is one rigid part.
  • Rotation: free (5° steps) or a fixed step (90°, 45°, …, or none).
  • Mirror: optional, with "Never mirror parts with text".
  • Offcut: centred, kept as the largest square in a chosen corner, or as a full-width strip along a chosen side.
  • Time limit: the best layout so far is previewed with the sheet usage and the largest offcut. Apply moves the parts in a single undo step. Parts that do not fit are lined up next to the sheet.

Settings are remembered in localStorage (openburn.nesting).

How it works (src/nesting/, no Fabric dependency)

  • Extraction (extract.ts) reads Fabric's own geometry (path commands, points, rect/ellipse sizes) through calcTransformMatrix(), so it matches what is drawn. The cut layer is the layer of the part's largest closed contour. Its inner contours are through holes, and smaller parts can be nested inside them. Closed shapes on other layers (engravings) and text are filled, so nothing lands on them.
  • Grid engine (engines/grid.ts): each orientation is rasterised conservatively on a fine grid (0.2 mm minimum, at most 1200 cells per side), dilated by half the spacing, so non-overlapping masks always respect the spacing. A greedy pass places the largest parts first. A late-acceptance hill climbing then permutes the order, angles and mirroring, and resumes from checkpoints so the unchanged prefix of a layout is not placed again.
  • Offcut modes: the engine always packs toward the top-left of an internal frame. frame.ts maps that frame to the requested corner or side with flips and a transpose. These never mirror a part on their own.
  • Workers: the search runs in several Web Workers, each with its own seed and initial ordering, and the best result is kept. It falls back to the UI thread if workers are unavailable. vite.config.ts sets worker.format: 'es'.
  • Pluggable engines: NestEngine + registry.ts. The dialog shows an engine picker as soon as more than one engine is registered.
  • Reserved for later, not exposed in the UI: common-line cutting (NestOptions.commonLine, NestResult.sharedEdges), irregular scrap sheets (NestSheet.obstacles) and quantities (NestPart.quantity).

docs/ARCHITECTURE.md (new §4b) and TODO.md are updated.

Testing

  • npm run typecheck, npm run build and the full unit suite (506 tests) pass. New tests cover:
    • geometry and frames;
    • rasterisation;
    • the engine: spacing, margin, parts that do not fit, rotation, a part nested inside a ring, every offcut side and corner, no mirroring of protected parts, monotonic progress and abort;
    • extraction from Fabric objects;
    • settings;
    • the dialog.
  • New e2e e2e/nesting.spec.ts runs the whole flow in Electron. It checks that the search runs in the nesting worker, that the parts stay inside the margin with the requested spacing, and that one undo restores the layout. The rest of the e2e suite passes except nodes-snap.spec.ts › node mode deletes only the selected node, which already fails on main here.
  • Real project: 44 grouped parts (rose kit, 3 mm plywood) on a 300 × 300 sheet with 1 mm spacing and a 30 s search, using about 18 % of the sheet. Side mode leaves a 212 × 300 mm free strip, and corner mode leaves a 246 × 246 mm free square.
  • Tested on Windows with a build of this branch.

🤖 Generated with Claude Code

inzuno and others added 2 commits December 2, 2026 14:36
Arrange → Nesting… packs the selected parts (or every object) on a board:
sheet size and position, spacing between parts, sheet margin, free or
stepped rotation, optional mirroring (never for parts with text if asked)
and where the offcut should stay: centred, in a corner or along a side.

- src/nesting: Fabric-free core. Parts are rasterised conservatively on a
  fine grid (dilated by half the spacing), placed greedily, then improved
  with late-acceptance hill climbing over order, angle and mirror, resuming
  from checkpoints. Holes in ring parts receive smaller parts.
- Runs in Web Workers (one seed/strategy each) with a live preview; falls
  back to the UI thread when workers are unavailable.
- Engines plug in through NestEngine + registry; common-line cutting,
  scrap obstacles and quantities are reserved in the types for later.
- Apply moves the objects in a single undo step; parts that do not fit are
  lined up next to the sheet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runs Arrange → Nesting on six rectangles in the desktop app: checks the
search runs in the nesting worker, every part lands inside the sheet margin
with the requested spacing, and a single undo restores the layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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