stl.py

Mon, 03 Apr 2017 19:40:06 +0200

author
mdd
date
Mon, 03 Apr 2017 19:40:06 +0200
changeset 9
a01a3fd32073
child 10
d26669bf424e
permissions
-rw-r--r--

precompile STL files with openscad

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

mercurial