#! /bin/bash
# Last edited on 2022-04-17 22:01:11 by stolfi

# Usage: draw_pieces.sh {OUTDIR} < {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 
# "{outdir}/{ix}.eps" with a drawing of the sheet and how the pieces 
# are to be cut out.  Also produces a text description "{outdir}/layout.txt".

outdir="$1"; shift
prefix="${outdir}/dp"

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