1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#!/bin/bash
#set -x
OPENSCAD=${OPENSCAD:-"openscad"}
RUN=y
INTERACTIVE=
PRINT_VARS=
USER_OUTPUT_FILENAME=
SCAD_FILE=
SHELL_SKEL=
help()
{
cat <<EOF
$0: parse and execute OpenSCAD files, allowing default variables from input scad
to be overridden with values supplied from the shell. (The parsing of scad
files is done from the shell mostly with sed, and is very primitive)
Usage: $0 [-h] [-n] [-p] [-i] [-o OUTPUT-FILENAME] INPUT.scad
-h, --help display this help
-p, --print-vars print variables parsed from INPUT.scad
-i, --interactive show output command and query for execution
-n, --no-act show output command, do not execute
-s, --shell-skel output a skeleton shell script for generating models
-o, --output-filename Specify an output filename. The following variables are
available to you:
\$VALUES - all values from INPUT.scad. This is the
default (-o '\${VALUES}.stl'):
cube-15-10-5.stl
\$ALL - all variable names and values from INPUT.scad
(-o '\${ALL}.stl'):
shape=cube,width=15,height=10,depth=5.stl
Additionally, all variables parsed from INPUT.scad
are available in the associatve array $SCAD, e.g.:
-o '\${SCAD[part]}.stl'
Usage examples:
1) Run $OPENSCAD with default values from input scad file:
$ $0 input.scad
2) You can override variables in the input file with variables passed in
through the shell; this will override "part" and "cubby_width"
paramaters from input scad file. (Note that if the part is a string,
you need to include quotes.):
$ cubby_width=200 part='"bottom"' $0 input.scad
3) Generate multiple models:
$ for w in 250 300 350 400; do
cubby_width="$w" part="bottom" $0 cubbies.scad
done
EOF
}
parse_options()
{
OPTS=$(getopt \
--options 'hpinso:' \
--longoptions 'help,print-vars,interactive,no-act,shell-skel,output-filename:' \
-- "$@")
eval set -- "$OPTS"
unset OPTS
if [ $# = 0 ]; then
help
exit
fi
while true; do
case "$1" in
-h | --help ) help; exit;;
-p | --print-vars ) PRINT_VARS=y;;
-i | --interactive ) INTERACTIVE=y;;
-n | --no-act ) RUN=;;
-s | --shell-skel ) SHELL_SKEL=y;;
-o | --output-filename ) shift; USER_OUTPUT_FILENAME=$1;;
-- ) shift; break;;
esac
shift
done
SCAD_FILE="$*";
[ -f "$SCAD_FILE" ] || die "$0: Needs an input scad file"
}
die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
warn() { printf 'Warning: %s\n' "$*" >&2; }
strip_multiline_comments () {
# https://stackoverflow.com/questions/13061785/remove-multi-line-comments
# https://stackoverflow.com/users/751863/steve
sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba'
}
strip_trailing_semicolon () {
sed -r 's/;$//'
}
strip_leading_spaces () {
sed -r 's/^ +//'
}
strip_trailing_spaces () {
sed -r 's/ +$//'
}
strip_nonassignments () {
grep -e '^[A-Za-z0-9_]\+\s*='
}
chomp () {
strip_leading_spaces |strip_trailing_spaces
}
parse_variable () {
#sed -r 's/^([a-zA-Z0-9_]+).*/\1/'
sed -r 's/^([^=]+)=.*/\1/'
}
parse_value () {
#sed -r 's/^.*?=\s*([^;]+);.*$/\1/'
#sed -r 's/^[^=]+\s*=\s*([^;]+);.*$/\1/'
sed -r 's/.*=\s*([^;]+?).*/\1/'
}
val_to_filename () {
echo "$1" |sed -r 's/"//g' |sed -r 's/ /_/g'
}
strip_after_hidden () {
sed -n '/\/\* \[Hidden\] \*\//q;p' "$1"
}
strip_trailing () {
trailing="$1"
input="$2"
echo ${input%${trailing}}
}
main()
{
parse_options "$@"
output_params=""
default_output_filename=""
ALL=""
VALUES=""
val_sep="-"
var_sep=","
declare -A SCAD
declare -a scad_var_order
while IFS= read -r line; do
[ "$line" ] || continue;
clean="$(echo -n "$line" |chomp)"
var=$(echo -n "$clean" |parse_variable |chomp)
# use value provided on command line preferentially to any values in the .scad file
val=""
if [[ -n "${!var}" ]]; then
val=${!var}
else
val=$(echo -n "$clean" |parse_value |chomp)
fi
if [ "$var" ] && [ "$val" ]; then
scad_var_order+=($var);
SCAD[$var]="$val"
output_params+="-D '$var=$val' "
VALUES+="$(val_to_filename "$val")${val_sep}"
ALL+="${var}=$(val_to_filename "$val")${var_sep}"
fi;
done < <(strip_after_hidden $SCAD_FILE | \
strip_multiline_comments |strip_nonassignments |strip_trailing_semicolon)
if [ "$PRINT_VARS" ]; then
for k in "${scad_var_order[@]}"; do
# Make sure quotes surround openscad strings
case "${SCAD[$k]:0:1}" in
\" ) line="${k}='${SCAD[$k]}'";;
\' ) line=${k}="\"${SCAD[$k]}\"";;
* ) line="${k}=${SCAD[$k]}";;
esac
echo $line
done
exit
fi
if [ "$SHELL_SKEL" ]; then
all_vars=$($0 $SCAD_FILE -p |sed -e 's/^/# /')
cat <<EOF
#!/bin/bash
shopenscad_cmd="$0 $SCAD_FILE "
export \$(echo \$(\$shopenscad_cmd -p))
${all_vars}
\$shopenscad_cmd
EOF
exit;
fi;
ALL=$(strip_trailing "$var_sep" "$ALL")
VALUES=$(strip_trailing "$val_sep" "$VALUES")
default_output_filename="${VALUES}.stl"
output_filename="$default_output_filename"
if [ "$USER_OUTPUT_FILENAME" ]; then
output_filename=$(echo $(eval "echo $USER_OUTPUT_FILENAME"))
fi
openscad_str="$OPENSCAD '$SCAD_FILE' "$output_params" -o '$output_filename'"
echo "${openscad_str}"
[ ! "$RUN" ] && [ ! "$INTERACTIVE" ] && exit
if [ "$INTERACTIVE" ]; then
eval_prompt="Evaulate openscad command? (type y for yes, anything else to exit): "
read -p "$eval_prompt" -n1 yes_eval_read
echo
case "$yes_eval_read" in
[Yy]) RUN=y ;; # still true
*) RUN=;;
esac
fi
[ "$RUN" ] && eval "$(echo "$openscad_str")"
}
main "$@"
#parse_options "$@"
|