Deepnest Documentation

repository·master·Indexed 22 days ago

https://github.com/jack000/deepnest

A high-performance nesting tool for CNC and laser cutting applications designed to optimize material usage. Based on SVGNest, Deepnest features a C-based nesting engine, line merging, DXF support, and path approximation. It utilizes a Genetic Algorithm and No Fit Polygon (NFP) strategy to arrange irregular vector shapes efficiently.

Tokens
1.8K
Snippets
3
Records
8
Agent score
78%

What's inside Deepnest

  1. Overview of Deepnest

    master

    Deepnest is a desktop application designed for fast and robust nesting, specifically optimized for laser cutters and other CNC tools. It is based on SVGNest but features a high-performance nesting engine with speed-critical code written in C.

    Key features include:

    • High Performance: A new nesting engine using C for speed.
    • Line Merging: Automatically merges common lines to optimize laser cuts.
    • DXF Support: Supports DXF files through conversion.
    • Path Approximation: Includes a path approximation feature to handle highly complex parts efficiently.
  2. How SVGNest nesting works (Algorithm & Optimization)

    master

    SVGNest uses an orbital approach combined with a genetic algorithm (GA) to solve the irregular shape nesting problem.

    1. Placement Strategy (No Fit Polygon)

    Instead of simple bounding boxes, SVGNest uses the No Fit Polygon (NFP) concept.

    • NFP: An 'orbit' created by moving polygon B around polygon A such that they touch but do not intersect. The NFP represents all possible valid placement positions for B relative to A.
    • Inner Fit Polygon: Similar to NFP, but ensures the orbiting polygon stays inside the bin boundaries.
    • Union of NFPs: When multiple parts are already placed, the tool takes the union of their NFPs to find valid empty spaces.

    2. Optimization Strategy (Genetic Algorithm)

    To find the best order of parts and their rotations, SVGNest uses a Genetic Algorithm where the 'gene' consists of the insertion order and part rotation.

    Heuristics used:

    • First-fit-decreasing: Larger parts are placed first, and smaller parts are placed last (acting as 'sand' to fill gaps).
    • Fitness Function: The algorithm evaluates 'fitness' by attempting to:
      1. Minimize unplaceable parts.
      2. Minimize the number of bins used.
      3. Minimize the width of all placed parts (to avoid long slivers of unused material).
  3. How to use SVGNest for vector nesting

    master

    SVGNest is a browser-based tool used to pack irregular vector shapes (parts) into a container (bin) to minimize material waste.

    Workflow

    1. Prepare SVG: Ensure all parts in your SVG file have been converted to outlines and that no outlines overlap.
    2. Upload: Upload your SVG file to the tool.
    3. Select Bin: Select one of the outlines in the uploaded file to serve as the bin (the container/material).
    4. Process: All other outlines in the file are automatically treated as parts to be nested within the selected bin.

    Note: Running the nesting process is CPU intensive. Mobile devices may struggle with performance.

  4. Configure SVGNest nesting parameters

    master

    You can tune the nesting algorithm using the following configuration parameters:

    | Parameter | Description | | :--- | : | | Space between parts | The minimum distance to maintain between parts (useful for accounting for laser kerf or CNC offsets). | | Curve tolerance | The maximum error allowed for linear approximations of Bezier paths and arcs (in SVG units/pixels). Decrease this if curved parts appear to overlap. | | Part rotations | The number of possible rotations to evaluate for each part (e.g., 4 for cardinal directions). Higher values improve results but increase computation time. | | GA population | The population size for the Genetic Algorithm. | | GA mutation rate | The probability of mutation for each gene or part placement (values between 1 and 50). | | Part in part | When enabled, allows the tool to place parts inside the holes of other parts. This is off by default due to high resource intensity. | | Explore concave areas | When enabled, specifically solves for concave edge cases at the cost of performance and placement robustness. |

  5. Manage background worker windows

    master

    Deepnest utilizes hidden BrowserWindow instances as background workers to perform tasks without blocking the main UI.

    • Initialization: Background windows are created via createBackgroundWindows(). They load the content from ./main/background.html.
    • Lifecycle: The application maintains an array backgroundWindows to track these instances. A winCount variable tracks how many background windows have been initialized.
    • Concurrency: The system checks a custom property isBusy on the BrowserWindow instance to determine if a background window is currently processing a task before assigning a new one.
  6. Inter-Process Communication (IPC) via ipcMain

    master

    The application uses Electron's ipcMain to facilitate communication between the main process and the renderer processes (the main window and background windows).

    Background Task Orchestration

    To manage heavy computations or background tasks, the main process listens for specific events to route messages between the UI (mainWindow) and worker windows (backgroundWindows):

    • background-start: Receives a payload and sends it to the first available (not busy) background window using webContents.send('background-start', payload). The background window is marked as isBusy = true.
    • background-response: When a background window responds, the main process forwards the payload to the mainWindow via webContents.send('background-response', payload) and marks the background window as isBusy = false.
    • background-progress: Forwards progress updates from background windows directly to the mainWindow via webContents.send('background-progress', payload).
    • background-stop: Destroys all current background windows and restarts the background window creation cycle.

    Application State Events

    • login-success: Forwards login payloads from the renderer to the mainWindow.
    • purchase-success: Forwards a purchase confirmation signal to the mainWindow.
    // Example of how the main process handles background task starts
    ipcMain.on('background-start', function(event, payload){
      for(var i=0; i<backgroundWindows.length; i++){
        if(backgroundWindows[i] && !backgroundWindows[i].isBusy){
          backgroundWindows[i].isBusy = true;
          backgroundWindows[i].webContents.send('background-start', payload);
          break;
        }
      }
    });
  7. Cleanup nfpcache on application quit

    master

    The application performs a cleanup of the ./nfpcache directory immediately before the process terminates. It listens to the before-quit event and synchronously unlinks (deletes) all files found within that directory.

    app.on('before-quit', function(){
      var p = path.join(__dirname, './nfpcache')
      if( fs.existsSync(p) ) {
        fs.readdirSync(p).forEach(function(file,index){
          var curPath = p + "/" + file;
          fs.unlinkSync(curPath);
        });
      }
    });