#! /bin/bash
# Last edited on 2019-12-08 15:17:01 by jstolfi

# Usage: draw_pieces.sh {OUTPREFIX} < {INFILE}

# Reads from standard input a file produced by the povray macro "write_pieces".
# Arranges those pieces in standard sheets of material to cut out.
# For each sheet with sequential index {ix}, produces an EPS file 
# "{prefix}_{ix}.eps" with a drawing of the sheet and how the pieces 
# are to be cut out.  Also produces a text description "{prefix}_layout.txt".

prefix="$1"; shift

cat \
  | sort -b -k1,1 -k5,5g -k4,4g -k2,2 \
  > ${prefix}_plates.txt
  
cat ${prefix}_plates.txt \
  | gawk '/[^\#]*[ ][0-9]/{ print $2; }' \
  > ${prefix}_grouping.txt
  
choose_sheet_cuts \
  -sheetSize 2200 1600 \
  -scrapMargin 10 \
  -cutWidth 3 \
  -inPrefix ${prefix} \
  -outPrefix ${prefix}
