#! /usr/bin/gawk -f # Last edited on 2019-11-22 13:47:21 by jstolfi BEGIN{ # Reads a list of rectangular seet pieces as produced # by {write_pieces.inc}. Packs them into sheets # of standard size. skosh = 1; # Half of nominal saw blade width (mm). width = 2200; # Width of sheet. height = 1600; # Height of sheet. # Initialize global state: omat = ""; # Current material. odz = -1; # Current sheet thickness. sheet = 0; # Current sheet index. ody = -1; # Curent piece height. xmax = -1; # Max X extent in sheet including {skosh} on both sides. ymax = -1; # Max Y extent in sheet including {skosh} on both sides. py = -1; # Low Y coordinate for next row or pieces. px = -1; # Low X coord of next piece in same row. } /^[ ]*([\#]|$)/ { next; } // { if (NF < 5) { printf "** BUG\n"; exit 1 } mat = $1; tagg = $2; dx = $3; dy = $4; dz = $5; # Rest of line is label: $5 = ""; $4 = ""; $3 = ""; $2 = ""; $1 = ""; lab = $0; gsub(/^[ ]+/, "", lab) gsub(/[ ]+$/, "", lab) if( \ (mat != omat) || (dz != odz) || (dy != ody) || \ ((px > skosh) && (px + dx + skosh > width)) \ ) { # Can't place in current row: finish_row() if( \ (mat != omat) || (dz != odz) || \ ((py > skosh) && (py + dy + skosh > height)) \ ) { # New sheet: finish_sheet() start_sheet(mat, dz) } start_row(dy) } # Append piece to current row: printf "%3d %-7s %6.1f %6.1f %6.1f %6.1f %6.1f %-20s %s\n", sheet, mat, dz, px,py, dx,dy, tagg, lab px = px + dx + 2*skosh next; } END { finish_row() finish_sheet() } function finish_sheet() { if ((mat != "") && (odz != -1)) { ymax = py - skosh; printf "# used sheet area = %.1f x %.1f\n", xmax, ymax; } } function start_sheet(mat, dz) { omat = mat; odz = dz; sheet++; py = skosh; xmax = 0; ody = -1; px = -1; } function finish_row() { if (ody > 0) { if (px - skosh > xmax) { xmax = px - skosh; } py = py + ody + 2*skosh; } ody = -1; px = -1; } function start_row(dy) { ody = dy; px = skosh; }