stl.py

Tue, 04 Apr 2017 00:47:22 +0200

author
mdd
date
Tue, 04 Apr 2017 00:47:22 +0200
changeset 11
098335a1d510
parent 10
d26669bf424e
child 14
ba3d8c56e6f5
permissions
-rw-r--r--

web viewer 3d finished

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

mercurial