Wed, 05 Apr 2017 00:59:45 +0200
code cleanup
14 | 1 | """ |
2 | STL related helpers | |
3 | """ | |
4 | ||
5 | import os, subprocess, vtk | |
6 | import config, data | |
9 | 7 | |
8 | OPENSCAD_MODULES = """ | |
9 | module tank(x, r, h) { | |
10 | color("SteelBlue") render() { //rotate([90,0,0]) { | |
11 | translate([x, r, r]) { | |
12 | sphere(r = r); // bottom | |
13 | cylinder(h = h-2*r, r = r); | |
14 | } | |
15 | translate([x, r, h-r]) { | |
16 | sphere(r = r); // top | |
17 | cylinder(h = r*1.4, r = r*0.25); | |
18 | } | |
19 | } | |
20 | } | |
21 | ||
22 | module spacer(x, r, rcyl, h) { | |
23 | color("DarkGrey") render() { //rotate([90,0,0]) { | |
24 | translate([x, r, rcyl]) { | |
25 | difference() { | |
26 | cylinder(h = h-2*rcyl, r = r); | |
27 | cylinder(h = h-2*rcyl, r = r*0.8); | |
28 | } | |
29 | } | |
30 | } | |
31 | } | |
32 | $fn = 25; // high detail per default | |
33 | """ | |
34 | ||
35 | def run_openscad(output): | |
14 | 36 | """ |
37 | Run openSCAD process, then convert the ascii stl to binary by using vtk | |
38 | """ | |
9 | 39 | if os.path.isfile(output): |
40 | print "skipping existing file %s" % output | |
41 | return | |
42 | print "rendering %s" % output | |
43 | subprocess.check_call([ | |
44 | 'openscad', | |
45 | '-o', | |
46 | output, | |
47 | config.TMP_SCAD | |
48 | ]) | |
10 | 49 | # convert to binary |
50 | reader = vtk.vtkSTLReader() | |
51 | reader.SetFileName(output) | |
52 | reader.Update() | |
53 | write = vtk.vtkSTLWriter() | |
54 | write.SetFileTypeToBinary() | |
55 | write.SetInput(reader.GetOutput()) | |
56 | write.SetFileName(output) | |
14 | 57 | write.Write() |
9 | 58 | |
59 | def precompile_all_stl(): | |
14 | 60 | """ |
61 | Write .scad temp file and | |
62 | Invoke run_openscad for each object | |
63 | that needs to be created in stl directory | |
64 | """ | |
9 | 65 | base = os.path.abspath('stl') |
66 | hmin = 9999 | |
67 | rmax = 0 | |
68 | for cyl in data.CYLINDER.keys(): | |
69 | with open(config.TMP_SCAD, "w") as fn: | |
70 | fn.write(OPENSCAD_MODULES) | |
71 | fn.write("tank(0, %f, %f);" % ( | |
72 | data.CYLINDER[cyl][0] / 2 * config.SCALE3D, | |
73 | data.CYLINDER[cyl][1] * config.SCALE3D)) | |
14 | 74 | run_openscad(os.path.join(base, \ |
9 | 75 | 'cylinder_' + cyl + '.stl')) |
76 | if data.CYLINDER[cyl][1] < hmin: | |
77 | hmin = data.CYLINDER[cyl][1] | |
78 | if data.CYLINDER[cyl][0] > rmax: | |
79 | rmax = data.CYLINDER[cyl][0] | |
80 | ||
81 | for pipe in data.PIPES: | |
82 | with open(config.TMP_SCAD, "w") as fn: | |
83 | fn.write(OPENSCAD_MODULES) | |
84 | fn.write("spacer(0, %f, %f, %f);" % ( | |
85 | pipe[1] / 2 * config.SCALE3D, | |
86 | rmax / 2 * config.SCALE3D, | |
87 | hmin * config.SCALE3D)) | |
14 | 88 | run_openscad(os.path.join(base, \ |
9 | 89 | 'spacer_' + pipe[0] + '.stl')) |
90 |