stl.py

Thu, 06 Apr 2017 17:18:24 +0200

author
mdd
date
Thu, 06 Apr 2017 17:18:24 +0200
changeset 19
32de35694e56
parent 14
ba3d8c56e6f5
permissions
-rw-r--r--

finishing first release

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

mercurial