# OpenNest A Windows desktop application for CNC nesting — imports DXF drawings, arranges parts on material plates, and exports layouts as DXF or G-code for cutting.

OpenNest - parts nested on a 36x36 plate OpenNest - 44 parts nested on a 60x120 plate

OpenNest takes your part drawings, lets you define your sheet (plate) sizes, and arranges the parts to make efficient use of material. The result can be exported as DXF files or post-processed into G-code that your CNC cutting machine understands. ## Features ### Import & Export | Feature | Description | |---------|-------------| | **DXF/DWG Import** | Load part drawings from AutoCAD DXF or DWG files via ACadSharp | | **DXF Export** | Export completed nest layouts back to DXF for downstream tools | | **BOM Import** | Batch-import part lists with quantities from Excel spreadsheets | | **Bend Line Detection** | Import bend lines from DXF via pluggable detectors (SolidWorks flat pattern built in) | | **Built-in Shapes** | 12 parametric shapes (circles, rectangles, L/T/flange, etc.) for quick parts | ### Nesting | Feature | Description | |---------|-------------| | **Pluggable Engines** | Default multi-phase, Vertical Remnant, Horizontal Remnant, plus custom plugin DLLs | | **Fill Strategies** | Linear grid, interlocking pairs, rectangle best-fit, and extents-based tiling | | **Best-Fit Pair Nesting** | NFP-based pair evaluation finds tight interlocking orientations between parts | | **Gravity Compaction** | Geometry-based directional push that preserves part spacing, including rotated parts and near passes between outlines | | **Part Rotation** | Automatic angle sweep to find better fits across allowed orientations | | **Multi-Plate Support** | Manage multiple plates of different sizes and materials in one nest | ### Plate Operations | Feature | Description | |---------|-------------| | **Sheet Cut-Offs** | Auto-generated trim cuts with geometry-aware clearance around placed parts | | **Drawing Splitting** | Split oversized parts with straight cuts, weld-gap tabs, or spike-groove joints | | **Interactive Editing** | Zoom, pan, select, clone, rotate, push, and manually arrange parts | ### CNC Output | Feature | Description | |---------|-------------| | **Lead-Ins, Lead-Outs & Tabs** | Configurable approach/exit paths and holding tabs with snap placement | | **Contour & Program Editing** | Inline G-code editor with contour reordering and cut-direction reversal | | **User-Defined Variables** | Named G-code variables (`$name`) emitted as machine variables (`#200+`) at post time | | **Post-Processors** | Plugin-based G-code generation; Cincinnati CL-707/800/900/940/CLX included | ## Prerequisites - **Windows 10 or later** - [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) ## Getting Started ### Build ```bash git clone https://github.com/ajisaacs/OpenNest.git cd OpenNest dotnet build OpenNest.sln ``` ### Cross-platform engine contract tests ```bash dotnet test OpenNest.Engine.Tests/OpenNest.Engine.Tests.csproj ``` `OpenNest.Engine.Tests` targets `net8.0` and runs on Linux, macOS, and Windows without the desktop project or local DXF fixtures. The existing `OpenNest.Tests` suite still requires Windows. The new whole-job contracts in `OpenNest.Engine/Jobs` (`namespace OpenNest`) use owned immutable geometry/settings, explicit part IDs and positive demand, finite or unlimited stock (`null` means unlimited; zero means unavailable), and result ID/pose values rather than mutable desktop models. Rotation is in radians about the geometry origin, followed by translation into the plate quadrant frame. Strategy factories belong to each runner, not the global registry. **Current scope (minimum runnable slice, not release-ready):** `NestJobRunner.Solve` allocates one stock entry across multiple physical sheets, respecting finite inventory (`null` is unlimited), positive remaining demand, and `MaxPlates`. Empty parts complete without consuming stock; empty/unavailable stock returns `Incomplete/StockExhausted`. A zero-placement candidate stops with `NoPlacementFound` and consumes no sheet. Nonempty jobs with multiple stock entries explicitly throw `NotSupportedException`; mixed-stock selection and optional optimizations are not implemented. `DrawingJobMapper` snapshots caller drawings/items under explicit requirement IDs. `LegacyPlateNesterAdapter` creates fresh private legacy drawings, items, and plates for each trial and maps returned drawings **by reference**, never by name. Mutable legacy quantities never drive the fulfillment ledger. `PlateNesterFactory` resolves the built-in strategies (`Default`, `Strip`, `Vertical Remnant`, `Horizontal Remnant`) to private engine factories; it neither reads nor changes the process-global `NestEngineRegistry`, and unknown keys reject. Quantity deduction in the engine paths the runner reaches (base-class fill/pack, strip deduction, remnant-fill ledger, shrink-leftover counting) is keyed by drawing reference, not display name, so same-name drawings and repeated requirements stay independent. `NestResultMaterializer` returns a detached domain nest and `DrawingsByPartId` identity map. Each output plate represents one physical sheet (`Quantity = 1`), and each placement is attached exactly once so domain quantity events do not double count. ```csharp var job = new NestJob( new[] { DrawingJobMapper.FromDrawing("requirement-1", drawing, quantity: 3) }, new[] { DrawingJobMapper.FromPlate("stock-1", plateTemplate, quantity: 3) }); var result = new NestJobRunner(LegacyPlateNesterAdapter.Create).Solve(job); var domainResult = NestResultMaterializer.Materialize(job, result); // result contains fulfillment/unplaced counts and physical stock usage; // domainResult.Nest and domainResult.DrawingsByPartId are detached from caller objects. ``` **Unfinished safety boundary:** basic validation rejects invalid dimensions/settings, nonfinite geometry, unknown candidate IDs, nonfinite poses, and overproduction. Cancellation throws initially and immediately after the engine returns; no partial result is returned. Full contour validity, plate containment, allowed-rotation enforcement, inter-part overlap/clearance validation, and broader cancellation coverage remain task 5 work. A committed candidate is therefore **not yet certified safe for cutting**, even if the legacy engine reports success. Geometry snapshots preserve flat CNC rapid/line/arc programs, including origin and hole contours, without approximation; other instructions are explicitly rejected. Existing desktop, API, CLI, and MCP nesting paths are unchanged. ### Run ```bash dotnet run --project OpenNest/OpenNest.csproj ``` Or open `OpenNest.sln` in Visual Studio and run the `OpenNest` project. ### Quick Walkthrough 1. **Create a nest** — File > New Nest 2. **Add drawings** — Import DXF files via the CAD Converter (handles bend detection, layer filtering, and color/linetype exclusion) or create built-in shapes 3. **Set up a plate** — Define the plate size, material, quadrant, and spacing 4. **Fill the plate** — The nesting engine arranges parts automatically using the active fill strategy 5. **Add cut-offs** — Optionally add horizontal/vertical cut-off lines to trim unused plate material 6. **Export** — Save as a `.nest` file, export to DXF, or post-process to G-code ### CAD Converter The CAD Converter turns DXF/DWG files into nest-ready drawings. Detected bend notes replace their original CAD annotations in the preview, avoiding duplicate labels without hiding unrelated text. Toggle layers, colors, and linetypes to exclude construction geometry; review detected bend lines; and preview the generated cut program with contour ordering before accepting the drawing into the nest.

CAD Converter — layer, color, and linetype filtering CAD Converter — contour list and G-code preview

## Command-Line Interface OpenNest includes a CLI for batch nesting without the GUI — useful for automation, scripting, and CI pipelines. ```bash dotnet run --project OpenNest.Console/OpenNest.Console.csproj -- [options] ``` **Import DXF files and nest onto a plate:** ```bash # Import a DXF and fill a 60x120 plate dotnet run --project OpenNest.Console/OpenNest.Console.csproj -- part.dxf --size 60x120 # Import multiple DXFs with mixed-part auto-nesting (experimental) dotnet run --project OpenNest.Console/OpenNest.Console.csproj -- part1.dxf part2.dxf --size 60x120 --autonest ``` **Work with existing nest files:** ```bash # Re-fill an existing nest file dotnet run --project OpenNest.Console/OpenNest.Console.csproj -- project.zip # Add a new DXF to an existing nest and auto-nest dotnet run --project OpenNest.Console/OpenNest.Console.csproj -- project.zip extra-part.dxf --autonest ``` **Options:** | Option | Description | |--------|-------------| | `--size ` | Plate size (e.g. `60x120`). Required for DXF-only mode. | | `--autonest` | Use mixed-part nesting instead of linear fill (experimental) | | `--drawing ` | Select which drawing to fill with (default: first) | | `--quantity ` | Max parts to place (default: unlimited) | | `--spacing ` | Override part spacing | | `--template ` | Load plate defaults (thickness, quadrant, material, spacing) from a nest file | | `--output ` | Output file path (default: `-result.zip`) | | `--keep-parts` | Keep existing parts instead of clearing before fill | | `--check-overlaps` | Run overlap detection after fill (exits with code 1 if found) | | `--engine ` | Select a registered nesting engine | | `--post ` | Post-process the result with the named post-processor plugin | | `--no-save` | Skip saving the output file | | `--no-log` | Skip writing the debug log | ## Project Structure ``` OpenNest.sln ├── OpenNest/ # WinForms desktop application (UI) ├── OpenNest.Core/ # Domain model, geometry, and CNC primitives ├── OpenNest.Engine/ # Nesting algorithms and whole-job contracts ├── OpenNest.Engine.Tests/ # Cross-platform whole-job contract tests (net8.0) ├── OpenNest.IO/ # File I/O — DXF import/export, nest file format ├── OpenNest.Console/ # Command-line interface for batch nesting ├── OpenNest.Api/ # Programmatic nesting API (NestRunner pipeline) ├── OpenNest.Data/ # Machine configuration and cutting parameters ├── OpenNest.Gpu/ # GPU-accelerated pair evaluation (ILGPU) ├── OpenNest.Training/ # ML training data collection (SQLite + EF Core) ├── OpenNest.Mcp/ # MCP server for AI tool integration ├── OpenNest.Posts.Cincinnati/ # Cincinnati CL-707 laser post-processor plugin └── OpenNest.Tests/ # Unit tests (xUnit) ``` | Project | What it does | |---------|-------------| | **OpenNest** | The app you run. WinForms MDI interface with plate viewer, drawing list, CAD converter, and dialogs. | | **OpenNest.Console** | Command-line interface for batch nesting, scripting, and automation. | | **OpenNest.Core** | The building blocks — parts, plates, drawings, geometry, G-code representation, bend lines, cut-offs, and drawing splitting. | | **OpenNest.Engine** | The brains — fill strategies (linear, pairs, rect best-fit, extents), NFP-based pair evaluation, gravity compaction, and a pluggable engine registry. | | **OpenNest.IO** | Reads and writes files — DXF/DWG (via ACadSharp), G-code, the `.nest` ZIP format, BOM spreadsheets (via ClosedXML), and bend detection from CAD files. | | **OpenNest.Api** | High-level API for running the full nesting pipeline programmatically (import, nest, export). | | **OpenNest.Data** | Machine configuration data layer — stores machine profiles, material/thickness parameters, lead-in/lead-out settings, and cut-off defaults. JSON-based local storage with an `IDataProvider` interface. | | **OpenNest.Gpu** | GPU-accelerated bitmap overlap detection for best-fit pair evaluation using ILGPU. | | **OpenNest.Posts.Cincinnati** | Post-processor plugin for Cincinnati CL-707/800/900/940/CLX laser cutting machines. Outputs Cincinnati-format G-code with material library, kerf compensation, and pierce logic. | | **OpenNest.Mcp** | MCP (Model Context Protocol) server exposing nesting operations as tools for AI assistants. | | **OpenNest.Tests** | 89 test files covering core geometry, fill strategies, splitting, bending, BOM import, post-processing, and the API. | ## Nesting Engines OpenNest uses a pluggable engine architecture. The active engine can be selected at runtime. | Engine | Description | |--------|-------------| | **Default** | Multi-phase strategy: linear fill, pair fill, rect best-fit, then remainder. Balances density and speed. | | **Vertical Remnant** | Optimizes for a clean vertical drop on the right side of the plate. | | **Horizontal Remnant** | Optimizes for a clean horizontal drop on the top of the plate. | Custom engines can be built by subclassing `NestEngineBase` and registering via `NestEngineRegistry` or dropping a plugin DLL in the `Engines/` directory. ### Fill Strategies Each engine composes from a set of fill strategies: | Strategy | Description | |----------|-------------| | **Linear** | Grid-based fill with geometry-aware copy distance and 4-config rotation/axis optimization | | **Pairs** | NFP-based interlocking pair evaluation — finds tight-fitting orientations between two parts | | **Rect Best-Fit** | Greedy rectangle bin-packing with horizontal and vertical orientation trials | | **Extents** | Extents-based pair tiling for simple rectangular arrangements | ## Drawing Splitting Oversized parts that don't fit on a single plate can be split into smaller pieces: - **Straight Split** — Clean cut with no joining features - **Weld-Gap Tabs** — Rectangular tab spacers on one side for weld alignment - **Spike-Groove** — Interlocking V-shaped spike and groove pairs for self-aligning joints The split system supports fit-to-plate (auto-calculates split lines) and split-by-count modes, with an interactive UI for adjusting split positions and feature parameters. **Cutout-aware clipping.** Split lines are trimmed against interior cutouts so cut paths never travel through a hole. Lines are Liang-Barsky clipped at region boundaries and arcs/circles are iteratively split at their intersections with the region box, so a cutout that straddles a split correctly contributes material to both sides. When a cutout fully spans the region between two splits, the material breaks into physically disconnected strips — the splitter detects the connected components via endpoint connectivity, nests any remaining holes inside their outer loops by bounding-box and point-in-polygon containment, and emits one drawing per strip. ## Post-Processors Post-processors convert nested layouts into machine-specific G-code. They are loaded as plugin DLLs from the `Posts/` directory at runtime. **Included:** - **Cincinnati** — Full post-processor for Cincinnati CL-707/800/900/940/CLX laser cutting machines with variable declarations, material library resolution, speed classification, kerf compensation, and optional part sub-programs (M98). Custom post-processors implement the `IPostProcessor` interface and are auto-discovered from DLLs in the `Posts/` directory. ## Keyboard Shortcuts | Key | Action | |-----|--------| | `Ctrl+F` | Fill the area around the cursor with the selected drawing | | `F` | Zoom to fit the plate view | | `Shift` + Mouse Wheel | Rotate parts when a drawing is selected | | `Shift` + Left Click | Push the selected group of parts to the bottom-left most point | | Middle Mouse Click | Rotate selected parts 90 degrees | | `X` | Push selected parts left (negative X) | | `Shift+X` | Push selected parts right (positive X) | | `Y` | Push selected parts down (negative Y) | | `Shift+Y` | Push selected parts up (positive Y) | | Arrow Keys | Nudge selected parts by an increment | | `Shift` + Arrow Keys | Push selected parts in that direction | ## Supported Formats | Format | Import | Export | |--------|--------|--------| | DXF (AutoCAD Drawing Exchange) | Yes | Yes | | DWG (AutoCAD Drawing) | Yes | No | | Excel BOM (Bill of Materials) | Yes | No | | G-code | No | Yes (via post-processors) | | `.nest` (ZIP-based project format) | Yes | Yes | ## Nest File Format Nest files (`.nest`) are ZIP archives containing: - `nest.json` — JSON metadata: nest info (name, customer, units, material, thickness, assist gas, salvage rate), plate defaults, plate options (alternative sizes with cost), drawings (with bend lines, material, source path, rotation constraints), and plates (size, quadrant, grain angle, parts with manual lead-in flags, cut-offs) - `programs/program-N` — G-code text for drawing N's cut program (may include variable definitions and `$name` references) - `programs/program-N-subs` — Sub-program definitions for drawing N (M98/G65-callable blocks for repeated features like holes) - `entities/entities-N` — Original source entities for drawing N (preserved from DXF import with per-entity suppression state for round-trip editing) - `bestfits/bestfit-N` — Cached best-fit pair evaluation results for drawing N, keyed by plate size and spacing (optional) ## Status OpenNest is under active development. The core nesting workflows function end-to-end — from DXF import through filling, splitting, cut-offs, and G-code post-processing. Contributions and feedback are welcome. ## License This project is licensed under the [MIT License](LICENSE).