stl.py

changeset 14
ba3d8c56e6f5
parent 10
d26669bf424e
child 19
32de35694e56
equal deleted inserted replaced
13:39fb313ba27c 14:ba3d8c56e6f5
1 import data, os, config, subprocess 1 """
2 import vtk 2 STL related helpers
3 """
4
5 import os, subprocess, vtk
6 import config, data
3 7
4 OPENSCAD_MODULES = """ 8 OPENSCAD_MODULES = """
5 module tank(x, r, h) { 9 module tank(x, r, h) {
6 color("SteelBlue") render() { //rotate([90,0,0]) { 10 color("SteelBlue") render() { //rotate([90,0,0]) {
7 translate([x, r, r]) { 11 translate([x, r, r]) {
27 } 31 }
28 $fn = 25; // high detail per default 32 $fn = 25; // high detail per default
29 """ 33 """
30 34
31 def run_openscad(output): 35 def run_openscad(output):
36 """
37 Run openSCAD process, then convert the ascii stl to binary by using vtk
38 """
32 if os.path.isfile(output): 39 if os.path.isfile(output):
33 print "skipping existing file %s" % output 40 print "skipping existing file %s" % output
34 return 41 return
35 print "rendering %s" % output 42 print "rendering %s" % output
36 subprocess.check_call([ 43 subprocess.check_call([
45 reader.Update() 52 reader.Update()
46 write = vtk.vtkSTLWriter() 53 write = vtk.vtkSTLWriter()
47 write.SetFileTypeToBinary() 54 write.SetFileTypeToBinary()
48 write.SetInput(reader.GetOutput()) 55 write.SetInput(reader.GetOutput())
49 write.SetFileName(output) 56 write.SetFileName(output)
50 write.Write() 57 write.Write()
51 58
52 def precompile_all_stl(): 59 def precompile_all_stl():
60 """
61 Write .scad temp file and
62 Invoke run_openscad for each object
63 that needs to be created in stl directory
64 """
53 base = os.path.abspath('stl') 65 base = os.path.abspath('stl')
54 hmin = 9999 66 hmin = 9999
55 rmax = 0 67 rmax = 0
56 for cyl in data.CYLINDER.keys(): 68 for cyl in data.CYLINDER.keys():
57 with open(config.TMP_SCAD, "w") as fn: 69 with open(config.TMP_SCAD, "w") as fn:
58 fn.write(OPENSCAD_MODULES) 70 fn.write(OPENSCAD_MODULES)
59 fn.write("tank(0, %f, %f);" % ( 71 fn.write("tank(0, %f, %f);" % (
60 data.CYLINDER[cyl][0] / 2 * config.SCALE3D, 72 data.CYLINDER[cyl][0] / 2 * config.SCALE3D,
61 data.CYLINDER[cyl][1] * config.SCALE3D)) 73 data.CYLINDER[cyl][1] * config.SCALE3D))
62 run_openscad(os.path.join(base, 74 run_openscad(os.path.join(base, \
63 'cylinder_' + cyl + '.stl')) 75 'cylinder_' + cyl + '.stl'))
64 if data.CYLINDER[cyl][1] < hmin: 76 if data.CYLINDER[cyl][1] < hmin:
65 hmin = data.CYLINDER[cyl][1] 77 hmin = data.CYLINDER[cyl][1]
66 if data.CYLINDER[cyl][0] > rmax: 78 if data.CYLINDER[cyl][0] > rmax:
67 rmax = data.CYLINDER[cyl][0] 79 rmax = data.CYLINDER[cyl][0]
71 fn.write(OPENSCAD_MODULES) 83 fn.write(OPENSCAD_MODULES)
72 fn.write("spacer(0, %f, %f, %f);" % ( 84 fn.write("spacer(0, %f, %f, %f);" % (
73 pipe[1] / 2 * config.SCALE3D, 85 pipe[1] / 2 * config.SCALE3D,
74 rmax / 2 * config.SCALE3D, 86 rmax / 2 * config.SCALE3D,
75 hmin * config.SCALE3D)) 87 hmin * config.SCALE3D))
76 run_openscad(os.path.join(base, 88 run_openscad(os.path.join(base, \
77 'spacer_' + pipe[0] + '.stl')) 89 'spacer_' + pipe[0] + '.stl'))
78 90

mercurial