Thu, 06 Apr 2017 17:18:48 +0200
Added tag Release1 for changeset 32de35694e56
14 | 1 | """ |
2 | STL related helpers | |
3 | """ | |
4 | ||
19 | 5 | import os, subprocess |
14 | 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 | """ | |
19 | 39 | import vtk |
9 | 40 | if os.path.isfile(output): |
41 | print "skipping existing file %s" % output | |
42 | return | |
43 | print "rendering %s" % output | |
44 | subprocess.check_call([ | |
45 | 'openscad', | |
46 | '-o', | |
47 | output, | |
48 | config.TMP_SCAD | |
49 | ]) | |
10 | 50 | # convert to binary |
51 | reader = vtk.vtkSTLReader() | |
52 | reader.SetFileName(output) | |
53 | reader.Update() | |
54 | write = vtk.vtkSTLWriter() | |
55 | write.SetFileTypeToBinary() | |
56 | write.SetInput(reader.GetOutput()) | |
57 | write.SetFileName(output) | |
14 | 58 | write.Write() |
9 | 59 | |
60 | def precompile_all_stl(): | |
14 | 61 | """ |
62 | Write .scad temp file and | |
63 | Invoke run_openscad for each object | |
64 | that needs to be created in stl directory | |
65 | """ | |
9 | 66 | base = os.path.abspath('stl') |
67 | hmin = 9999 | |
68 | rmax = 0 | |
69 | for cyl in data.CYLINDER.keys(): | |
70 | with open(config.TMP_SCAD, "w") as fn: | |
71 | fn.write(OPENSCAD_MODULES) | |
72 | fn.write("tank(0, %f, %f);" % ( | |
73 | data.CYLINDER[cyl][0] / 2 * config.SCALE3D, | |
74 | data.CYLINDER[cyl][1] * config.SCALE3D)) | |
14 | 75 | run_openscad(os.path.join(base, \ |
9 | 76 | 'cylinder_' + cyl + '.stl')) |
77 | if data.CYLINDER[cyl][1] < hmin: | |
78 | hmin = data.CYLINDER[cyl][1] | |
79 | if data.CYLINDER[cyl][0] > rmax: | |
80 | rmax = data.CYLINDER[cyl][0] | |
81 | ||
82 | for pipe in data.PIPES: | |
83 | with open(config.TMP_SCAD, "w") as fn: | |
84 | fn.write(OPENSCAD_MODULES) | |
85 | fn.write("spacer(0, %f, %f, %f);" % ( | |
86 | pipe[1] / 2 * config.SCALE3D, | |
87 | rmax / 2 * config.SCALE3D, | |
88 | hmin * config.SCALE3D)) | |
14 | 89 | run_openscad(os.path.join(base, \ |
9 | 90 | 'spacer_' + pipe[0] + '.stl')) |
91 |