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