summaryrefslogtreecommitdiff
path: root/shopenscad.sh
blob: 996d405e2a6ce5a6137fd8f494a264436a0b0347 (plain)
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
#!/bin/bash
# shopenscad.sh: parse a .scad file and generate an openscad command with all
# its var=value's parameterized into -D options, which can be overriden with
# shell variables.
#
# Usage examples:
#
# $ ./shopenscad.sh cubbies.scad
# openscad 'cubbies.scad' -D 'part="divider"' -D 'cubby_width=50' -D 'cubby_depth=180' -D 'cubby_divider_height=90' -D 'thickness=5' -D 'strut_thickness=5' -D 'tab_width=10' -D 'tab_padding=5' -D 'tab_tolerance=0.5'  -o 'divider=50=180=90=5=5=10=5=0.5.stl'
# [openscad output omitted]
#
# (the openscad command is printed and then evaluated, outputing to the file
# indicated after the -o flag)
#
# You can also overwrite the variables parsed from your scad file by passing a
# shell variable with the same name:
#
# $ cubby_width=200 part=\"bottom\" ./shopenscad.sh cubbies.scad
# openscad 'cubbies.scad' -D 'part="bottom"' -D 'cubby_width=200' -D 'cubby_depth=180' -D 'cubby_divider_height=90' -D 'thickness=5' -D 'strut_thickness=5' -D 'tab_width=10' -D 'tab_padding=5' -D 'tab_tolerance=0.5'  -o 'bottom=200=180=90=5=5=10=5=0.5.stl'
# [openscad output omitted]
#
# Now we can easily generate multiple models:
#
# $ for w in 250 300 350 400; do
# >     for p in '"divider"' '"bottom"'; do
# >         cubby_width="$w" part="$p" ./shopenscad.sh  cubbies.scad
# >     done
# > done
# [openscad output omitted]

openscad=${OPENSCAD:-"openscad"}

# yes_eval=false
# no_eval=false

interactive=false
run=true
print_vars=false
user_output_filename=

while getopts pino: opt; do
    case $opt in
        p) print_vars=true ;;   # print variables that were parsed
        i) interactive=true ;;  # suppress interactive output -- do eval cmd
        n) run=false ;;         # do not eval cmd
        o) user_output_filename="$OPTARG" ;; # override default output filename
    esac
done
shift $(($OPTIND - 1))
scad_file="$*"

if [ ! "$scad_file" ]; then
    echo "$0: needs an input scad file"
    exit 1
fi;

function strip_comments () {
    strip_multiline_comments
}

function 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'
}

function strip_trailing_semicolon () {
    sed -r 's/;$//'
}

function strip_leading_spaces () {
    sed -r 's/^ +//'
}

function strip_trailing_spaces () {
    sed -r 's/ +$//'
}

function strip_nonassignments () {
    grep -e '^[A-Za-z0-9_]\+\s*='
}

function chomp () {
    strip_leading_spaces |strip_trailing_spaces
}

function parse_variable () {
    #sed -r 's/^([a-zA-Z0-9_]+).*/\1/'
    sed -r 's/^([^=]+)=.*/\1/'
}

function parse_value () {
    #sed -r 's/^.*?=\s*([^;]+);.*$/\1/'
    #sed -r 's/^[^=]+\s*=\s*([^;]+);.*$/\1/'
    sed -r 's/.*=\s*([^;]+?).*/\1/'
}

function val_to_filename () {
    echo "$1" |sed -r 's/"//g' |sed -r 's/ /_/g'
}

function strip_last_char () {
    echo "$1" |sed -r 's/.$//'
}

function strip_after_hidden () {
    echo "$1" |sed -n '/\/\* \[Hidden\] \*\//q;p' cubbies.scad
}

function strip_trailing () {
    trailing="$1"
    input="$2"
    echo ${input%${trailing}}
}

output_params=""
default_output_filename=""
all=""
values_only=""
val_sep="="
var_sep=","

declare -A scad

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]="$val"
        output_params+="-D '$var=$val' "
        values_only+="$(val_to_filename "$val")${val_sep}"
        all+="${var}${val_sep}$(val_to_filename "$val")${var_sep}"
    fi;
done < <(echo "$(cat "$scad_file")" |
             strip_comments |strip_after_hidden |strip_nonassignments |strip_trailing_semicolon)


if $print_vars; then
    echo The following variables have been parsed from ${scad_file}:
    for k in "${!scad[@]}"; do
        echo ${k}=${scad[$k]}
    done; echo
fi

all=$(strip_trailing "$val_sep" "$all")
values_only=$(strip_trailing "$val_sep" "$values_only")

default_output_filename="$(strip_trailing "-" "${values_only}").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 $(strip_trailing "-" "${values_only}").stl"
openscad_str="$openscad '$scad_file' "$output_params" -o '$output_filename'"
#openscad_str="$openscad '$scad_file' "$output_params" -o '${all}.stl'"

# ${open_filename//'/'\\''}


echo "${openscad_str}"
$print_vars || ! $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=true ;;       # still true
        *) run=false ;;
    esac
fi
#sed -nel <<< "$openscad_str"
$run &&  eval "$openscad_str"