Mon, 03 Apr 2017 19:40:06 +0200
precompile STL files with openscad
9 | 1 | import data, os, config, subprocess |
2 | ||
3 | OPENSCAD_MODULES = """ | |
4 | module tank(x, r, h) { | |
5 | color("SteelBlue") render() { //rotate([90,0,0]) { | |
6 | translate([x, r, r]) { | |
7 | sphere(r = r); // bottom | |
8 | cylinder(h = h-2*r, r = r); | |
9 | } | |
10 | translate([x, r, h-r]) { | |
11 | sphere(r = r); // top | |
12 | cylinder(h = r*1.4, r = r*0.25); | |
13 | } | |
14 | } | |
15 | } | |
16 | ||
17 | module spacer(x, r, rcyl, h) { | |
18 | color("DarkGrey") render() { //rotate([90,0,0]) { | |
19 | translate([x, r, rcyl]) { | |
20 | difference() { | |
21 | cylinder(h = h-2*rcyl, r = r); | |
22 | cylinder(h = h-2*rcyl, r = r*0.8); | |
23 | } | |
24 | } | |
25 | } | |
26 | } | |
27 | $fn = 25; // high detail per default | |
28 | """ | |
29 | ||
30 | def run_openscad(output): | |
31 | if os.path.isfile(output): | |
32 | print "skipping existing file %s" % output | |
33 | return | |
34 | print "rendering %s" % output | |
35 | subprocess.check_call([ | |
36 | 'openscad', | |
37 | '-o', | |
38 | output, | |
39 | config.TMP_SCAD | |
40 | ]) | |
41 | ||
42 | def precompile_all_stl(): | |
43 | base = os.path.abspath('stl') | |
44 | hmin = 9999 | |
45 | rmax = 0 | |
46 | for cyl in data.CYLINDER.keys(): | |
47 | with open(config.TMP_SCAD, "w") as fn: | |
48 | fn.write(OPENSCAD_MODULES) | |
49 | fn.write("tank(0, %f, %f);" % ( | |
50 | data.CYLINDER[cyl][0] / 2 * config.SCALE3D, | |
51 | data.CYLINDER[cyl][1] * config.SCALE3D)) | |
52 | run_openscad(os.path.join(base, | |
53 | 'cylinder_' + cyl + '.stl')) | |
54 | if data.CYLINDER[cyl][1] < hmin: | |
55 | hmin = data.CYLINDER[cyl][1] | |
56 | if data.CYLINDER[cyl][0] > rmax: | |
57 | rmax = data.CYLINDER[cyl][0] | |
58 | ||
59 | for pipe in data.PIPES: | |
60 | with open(config.TMP_SCAD, "w") as fn: | |
61 | fn.write(OPENSCAD_MODULES) | |
62 | fn.write("spacer(0, %f, %f, %f);" % ( | |
63 | pipe[1] / 2 * config.SCALE3D, | |
64 | rmax / 2 * config.SCALE3D, | |
65 | hmin * config.SCALE3D)) | |
66 | run_openscad(os.path.join(base, | |
67 | 'spacer_' + pipe[0] + '.stl')) | |
68 |