|
11
|
1 #!/bin/sh
|
|
|
2 USAGE="$0 [-o <dir>] [-p <parts>] [<preset> ...]
|
|
|
3
|
|
|
4 export the printable parts of the presets in main.json as stl files. the
|
|
|
5 file names come from the model, with hinged_box replaced by the preset
|
|
|
6 name, spaces turned into underscores. requires jq to read the preset
|
|
|
7 names.
|
|
|
8
|
|
|
9 options:
|
|
|
10 -o, --output <dir> output directory, default .
|
|
|
11 -p, --parts <parts> space separated list of parts to export,
|
|
|
12 default \"body lid latch\" or \$PARTS
|
|
|
13
|
|
|
14 arguments:
|
|
|
15 <preset> preset to export, default all presets
|
|
|
16
|
|
|
17 environment:
|
|
|
18 OPENSCAD openscad binary, default the one in
|
|
|
19 /Applications/OpenSCAD.app
|
|
|
20 "
|
|
|
21
|
|
|
22 : "${OPENSCAD:=/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD}"
|
|
|
23 : "${PARTS:=body lid latch}"
|
|
|
24 OUTDIR="."
|
|
|
25 PRESETS=""
|
|
|
26
|
|
|
27 MODEL_DIR=$(dirname "$0")
|
|
|
28 MAIN="$MODEL_DIR/main.scad"
|
|
|
29 JSON="$MODEL_DIR/main.json"
|
|
|
30
|
|
|
31 # export_part PRESET PART -- export one part of one preset to OUTDIR.
|
|
|
32 # expects MAIN, JSON, OPENSCAD and TMP_DIR. prints the file name. on
|
|
|
33 # failure prints the openscad output to stderr and returns 1.
|
|
|
34 export_part() {
|
|
|
35 ECHO_FILE="$TMP_DIR/name.echo"
|
|
|
36 LOG_FILE="$TMP_DIR/openscad.log"
|
|
|
37 PREFIX=$(echo "$1" | tr ' ' '_')
|
|
|
38
|
|
|
39 # the model echoes the file name, starting with hinged_box_
|
|
|
40 "$OPENSCAD" "$MAIN" -p "$JSON" -P "$1" -D "part=\"$2\"" \
|
|
|
41 -o "$ECHO_FILE" 2>"$LOG_FILE" &&
|
|
|
42 NAME=$(sed -n 's/^ECHO: "hinged_box_\([^"]*\)"$/\1/p' "$ECHO_FILE") &&
|
|
|
43 [ -n "$NAME" ] &&
|
|
|
44 FILE="$OUTDIR/${PREFIX}_$NAME.stl" &&
|
|
|
45 "$OPENSCAD" "$MAIN" -p "$JSON" -P "$1" -D "part=\"$2\"" \
|
|
|
46 -o "$FILE" --export-format asciistl 2>"$LOG_FILE" &&
|
|
|
47 echo "$FILE" || {
|
|
|
48 echo "export of $2 for '$1' failed" >&2
|
|
|
49 cat "$LOG_FILE" >&2
|
|
|
50 return 1
|
|
|
51 }
|
|
|
52 }
|
|
|
53
|
|
|
54 # export_all -- export every part in PARTS for every preset in PRESETS.
|
|
|
55 # stops at the first failure and returns 1.
|
|
|
56 export_all() {
|
|
|
57 OLD_IFS="$IFS"
|
|
|
58 IFS="
|
|
|
59 "
|
|
|
60 for PRESET in $PRESETS; do
|
|
|
61 IFS="$OLD_IFS"
|
|
|
62 # PARTS is a word list, expanded unquoted
|
|
|
63 for PART in $PARTS; do
|
|
|
64 export_part "$PRESET" "$PART" || return 1
|
|
|
65 done
|
|
|
66 done
|
|
|
67 IFS="$OLD_IFS"
|
|
|
68 }
|
|
|
69
|
|
|
70 while [ "$#" != 0 ]
|
|
|
71 do
|
|
|
72 case "$1" in
|
|
|
73 -h|--help) echo "$USAGE"; exit 0 ;;
|
|
|
74 -o|--output) shift; OUTDIR="$1" ;;
|
|
|
75 -p|--parts) shift; PARTS="$1" ;;
|
|
|
76 -*) echo "unknown option '$1'" >&2; exit 1 ;;
|
|
|
77 *)
|
|
|
78 # presets are kept one per line, since their names may
|
|
|
79 # contain spaces
|
|
|
80 if [ -z "$PRESETS" ]; then
|
|
|
81 PRESETS="$1"
|
|
|
82 else
|
|
|
83 PRESETS="$PRESETS
|
|
|
84 $1"
|
|
|
85 fi
|
|
|
86 ;;
|
|
|
87 esac
|
|
|
88 shift
|
|
|
89 done
|
|
|
90
|
|
|
91 if [ ! -x "$OPENSCAD" ]; then
|
|
|
92 echo "openscad not found at '$OPENSCAD'" >&2
|
|
|
93 exit 1
|
|
|
94 fi
|
|
|
95 if [ ! -d "$OUTDIR" ]; then
|
|
|
96 echo "output directory '$OUTDIR' not found" >&2
|
|
|
97 exit 1
|
|
|
98 fi
|
|
|
99 if [ -z "$PARTS" ]; then echo "no parts to export" >&2; exit 1; fi
|
|
|
100
|
|
|
101 ALL_PRESETS=$(jq -r '.parameterSets | keys_unsorted[]' "$JSON") || exit 1
|
|
|
102 if [ -z "$PRESETS" ]; then PRESETS="$ALL_PRESETS"; fi
|
|
|
103
|
|
|
104 # openscad falls back to the defaults for an unknown preset, so check
|
|
|
105 # the names here
|
|
|
106 OLD_IFS="$IFS"
|
|
|
107 IFS="
|
|
|
108 "
|
|
|
109 for PRESET in $PRESETS; do
|
|
|
110 if ! printf '%s\n' "$ALL_PRESETS" | grep -qxF "$PRESET"; then
|
|
|
111 echo "preset '$PRESET' not found in $JSON" >&2
|
|
|
112 exit 1
|
|
|
113 fi
|
|
|
114 done
|
|
|
115 IFS="$OLD_IFS"
|
|
|
116
|
|
|
117 TMP_DIR=$(mktemp -d) || exit 1
|
|
|
118 export_all
|
|
|
119 STATUS="$?"
|
|
|
120 rm -rf "$TMP_DIR"
|
|
|
121 exit "$STATUS"
|