diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-05-08 19:47:06 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-05-08 19:47:06 -0400 |
commit | 630ec04413210cc69b07f3b5f2bb3eff8d3bc87b (patch) | |
tree | 7813b088b2f593aca718092e35dc360fe7a1e6ae | |
parent | d2c0f052c1bf723eec4dace6d7a8931d32e0a9d1 (diff) |
add shopenscad.sh for parsing parameters from scad files into -D parameters to openscad
-rwxr-xr-x | shopenscad.sh | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/shopenscad.sh b/shopenscad.sh new file mode 100755 index 0000000..b044ded --- /dev/null +++ b/shopenscad.sh | |||
@@ -0,0 +1,91 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | # parse a .scad file and generate an openscad command with all its var=value's | ||
4 | # parameterized into -D options | ||
5 | |||
6 | |||
7 | openscad="openscad" | ||
8 | |||
9 | scad_file="$1" | ||
10 | |||
11 | function strip_comments () { | ||
12 | strip_multiline_comments | ||
13 | } | ||
14 | |||
15 | function strip_multiline_comments () { | ||
16 | # https://stackoverflow.com/questions/13061785/remove-multi-line-comments | ||
17 | # https://stackoverflow.com/users/751863/steve | ||
18 | sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' | ||
19 | } | ||
20 | |||
21 | function strip_spaces () { | ||
22 | sed -r 's/ //g' | ||
23 | } | ||
24 | |||
25 | function strip_semicolon () { | ||
26 | sed -r 's/;$//' | ||
27 | } | ||
28 | |||
29 | function strip_leading_spaces () { | ||
30 | sed -r 's/^ +//' | ||
31 | } | ||
32 | |||
33 | function strip_trailing_spaces () { | ||
34 | sed -r 's/ +$//' | ||
35 | } | ||
36 | |||
37 | function strip_nonassignments () { | ||
38 | grep -e '^[A-Za-z0-9_]\+\s*=' | ||
39 | } | ||
40 | |||
41 | function chomp () { | ||
42 | strip_leading_spaces |strip_trailing_spaces | ||
43 | } | ||
44 | |||
45 | |||
46 | function parse_variable () { | ||
47 | #sed -r 's/^([a-zA-Z0-9_]+).*/\1/' | ||
48 | sed -r 's/^([^=]+)=.*/\1/' | ||
49 | } | ||
50 | |||
51 | function parse_value () { | ||
52 | #sed -r 's/^.*?=\s*([^;]+);.*$/\1/' | ||
53 | #sed -r 's/^[^=]+\s*=\s*([^;]+);.*$/\1/' | ||
54 | sed -r 's/.*=\s*([^;]+?).*/\1/' | ||
55 | } | ||
56 | |||
57 | |||
58 | function strip_empty_lines () { | ||
59 | sed -r '/^$/d' | ||
60 | } | ||
61 | |||
62 | function val_to_filename () { | ||
63 | echo "$1" |sed -r 's/"//g' |sed -r 's/ /_/g' | ||
64 | } | ||
65 | |||
66 | function strip_last_char () { | ||
67 | echo "$1" |sed -r 's/.$//' | ||
68 | } | ||
69 | |||
70 | function shopenscad () { | ||
71 | input_file="$1" | ||
72 | output_params="" | ||
73 | output_filename="" | ||
74 | while IFS= read -r line; do | ||
75 | clean="$(echo -n "$line" |chomp)" | ||
76 | var=$(echo -n "$clean" |parse_variable |chomp) | ||
77 | val=$(echo -n "$clean" |parse_value |chomp) | ||
78 | if [ "$var" ] && [ "$val" ]; then | ||
79 | output_params+="-D $var=$val " | ||
80 | output_filename+="$(val_to_filename "$val")-" | ||
81 | fi; | ||
82 | done < <(echo "$(cat "$input_file")" |strip_comments |strip_nonassignments |strip_empty_lines |strip_semicolon) | ||
83 | |||
84 | echo "$openscad $(strip_last_char "$output_params") -o ${output_filename}.stl" | ||
85 | |||
86 | } | ||
87 | |||
88 | shopenscad "$scad_file" | ||
89 | |||
90 | |||
91 | |||