stl.py

Wed, 05 Apr 2017 00:59:45 +0200

author
mdd
date
Wed, 05 Apr 2017 00:59:45 +0200
changeset 14
ba3d8c56e6f5
parent 10
d26669bf424e
child 19
32de35694e56
permissions
-rw-r--r--

code cleanup

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

mercurial