#! /usr/bin/gawk -f # Last edited on 2019-11-22 13:47:21 by jstolfi BEGIN{ skosh = 1; # Half of nominal saw blade width (mm). width = 2200; # Max dimension of sheet. # Initialize global state: omat = ""; # Current material. odz = -1; # Current sheet thickness. 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 < 4) { printf "** BUG\n"; exit 1 } mat = $1; dx = $2; dy = $3; dz = $4; # Rest of line is label: $4 = ""; $3 = ""; $2 = ""; $1 = ""; lab = $0; gsub(/^[ ]+/, "", lab) gsub(/[ ]+$/, "", lab) if ((mat != omat) || (dz != odz)) { # New sheet: finish_row() finish_sheet() start_sheet(mat, dz) } if ((dy != ody) || (px + dx + skosh > width)) { # New row: finish_row() start_row(dy) } # Append piece to current row: printf "%s %6.1f %6.1f %6.1f %6.1f %6.1f %s\n", mat, dz, px,py, dx,dy, 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) { sheet = 0; # For now. omat = mat; odz = dz; 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; }