precompile STL files with openscad

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

author
mdd
date
Mon, 03 Apr 2017 19:40:06 +0200
changeset 9
a01a3fd32073
parent 8
63b6f80e09ef
child 10
d26669bf424e

precompile STL files with openscad

.hgignore file | annotate | diff | comparison | revisions
config.py file | annotate | diff | comparison | revisions
cylindertransport.py file | annotate | diff | comparison | revisions
data.py file | annotate | diff | comparison | revisions
stl.py file | annotate | diff | comparison | revisions
stl/README file | annotate | diff | comparison | revisions
--- a/.hgignore	Mon Apr 03 09:10:41 2017 +0200
+++ b/.hgignore	Mon Apr 03 19:40:06 2017 +0200
@@ -1,2 +1,3 @@
 syntax: glob
 *.pyc
+stl/*.stl
--- a/config.py	Mon Apr 03 09:10:41 2017 +0200
+++ b/config.py	Mon Apr 03 19:40:06 2017 +0200
@@ -1,2 +1,5 @@
 # pfad zu den TTF files (arial.ttf)
 FONTBASE = "/usr/share/fonts/truetype/msttcorefonts/"
+
+SCALE3D = 0.1
+TMP_SCAD = "/tmp/render.scad"
--- a/cylindertransport.py	Mon Apr 03 09:10:41 2017 +0200
+++ b/cylindertransport.py	Mon Apr 03 19:40:06 2017 +0200
@@ -8,7 +8,8 @@
 from PIL import Image, ImageDraw, ImageFont
 import argparse, sys
 from data import CYLINDER, PIPES
-from config import FONTBASE
+from stl import OPENSCAD_MODULES, precompile_all_stl
+from config import FONTBASE, SCALE3D
 
 def offset(r_1, r_2):
     """
@@ -30,38 +31,14 @@
         self.scad = {
             "tmpl": """// Color support only in compile mode (F5)
 // cylindertransport.py OpenSCAD script
-module tank(x, r, h) {
-  color("SteelBlue") render() { //rotate([90,0,0]) {
-    translate([x, r, r]) {
-        sphere(r = r); // bottom
-        cylinder(h = h-2*r, r = r);
-    }
-    translate([x, r, h-r]) {
-        sphere(r = r); // top
-        cylinder(h = r*1.4, r = r*0.25);
-    }
-  }
-}
-
-module spacer(x, r, rcyl, h) {
-  color("DarkGrey") render() { //rotate([90,0,0]) {
-    translate([x, r, rcyl]) {
-        difference() {
-            cylinder(h = h-2*rcyl, r = r);
-            cylinder(h = h-2*rcyl, r = r*0.8);
-        }
-    }
-  }
-}
 $fn = 10;
-""",
+""" + OPENSCAD_MODULES,
             "spacer": "",
             "cylinder": ""
             }
         self.circles = []
         self.spacings = []
         self.margin = 20
-        self.scale3d = 0.1
 
     def calc_min(self, r_1, r_2):
         """
@@ -100,8 +77,8 @@
         posx = self.margin + r_2 # start offset x
         self._circle(posx, r_2, label, 0.5)
         self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
-            posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d,
-            CYLINDER[self.cylinders[0]][1] * self.scale3d)
+            posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
+            CYLINDER[self.cylinders[0]][1] * SCALE3D)
         posx += offset(r_2, r_3)
 
         for i in range(0, len(self.cylinders) - 1):
@@ -111,23 +88,23 @@
             # draw cylinder
             self._circle(posx, r_1, "Tank " + self.cylinders[i])
             self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
-                posx * self.scale3d, r_1 * self.scale3d,
-                CYLINDER[self.cylinders[i]][1] * self.scale3d)
+                posx * SCALE3D, r_1 * SCALE3D,
+                CYLINDER[self.cylinders[i]][1] * SCALE3D)
             sx1 = posx + r_1
             posx += offset(r_1, r_2)
             # draw right spacer
             self._circle(posx, r_2, label, 0.5)
             self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
-                posx * self.scale3d, r_2 * self.scale3d, r_1 * self.scale3d,
-                CYLINDER[self.cylinders[i]][1] * self.scale3d)
+                posx * SCALE3D, r_2 * SCALE3D, r_1 * SCALE3D,
+                CYLINDER[self.cylinders[i]][1] * SCALE3D)
             posx += offset(r_2, r_3)
             sx2 = posx - r_3
             if i == (len(self.cylinders) - 2):
                 # draw last bottle
                 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
                 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
-                    posx * self.scale3d, r_3 * self.scale3d,
-                    CYLINDER[self.cylinders[i + 1]][1] * self.scale3d)
+                    posx * SCALE3D, r_3 * SCALE3D,
+                    CYLINDER[self.cylinders[i + 1]][1] * SCALE3D)
                 posx += offset(r_2, r_3)
 
             self.spacings.append([sx1, sx2])
@@ -135,8 +112,8 @@
         # last bottle spacer pipe
         self._circle(posx, r_2, label, 0.5)
         self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
-            posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d,
-            CYLINDER[self.cylinders[-1]][1] * self.scale3d)
+            posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
+            CYLINDER[self.cylinders[-1]][1] * SCALE3D)
         return int(posx + r_2 + self.margin)
 
     def centertext(self, draw, posx, posy, txt, size):
@@ -189,9 +166,15 @@
     parser.add_argument('--scad', dest='scad', \
         type=str, default="", metavar='filename', \
         help='Write OpenSCAD script file')
+    parser.add_argument('--precompile', dest='precompile', \
+        default=False, action='store_true', \
+        help='Precompile STL objects for 3D View')
 
     options = parser.parse_args()
 
+    if options.precompile:
+        precompile_all_stl()
+
     for test in options.cylinders:
         if not test in CYLINDER.keys():
             print "Cylinder type '%s' is unknown" % test
--- a/data.py	Mon Apr 03 09:10:41 2017 +0200
+++ b/data.py	Mon Apr 03 19:40:06 2017 +0200
@@ -25,4 +25,4 @@
 	["DN100", 114.3],
 	["DN125", 139.7],
 	["DN150", 168.3],
-]
\ No newline at end of file
+]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stl.py	Mon Apr 03 19:40:06 2017 +0200
@@ -0,0 +1,68 @@
+import data, os, config, subprocess
+
+OPENSCAD_MODULES = """
+module tank(x, r, h) {
+  color("SteelBlue") render() { //rotate([90,0,0]) {
+    translate([x, r, r]) {
+        sphere(r = r); // bottom
+        cylinder(h = h-2*r, r = r);
+    }
+    translate([x, r, h-r]) {
+        sphere(r = r); // top
+        cylinder(h = r*1.4, r = r*0.25);
+    }
+  }
+}
+
+module spacer(x, r, rcyl, h) {
+  color("DarkGrey") render() { //rotate([90,0,0]) {
+    translate([x, r, rcyl]) {
+        difference() {
+            cylinder(h = h-2*rcyl, r = r);
+            cylinder(h = h-2*rcyl, r = r*0.8);
+        }
+    }
+  }
+}
+$fn = 25; // high detail per default
+"""
+
+def run_openscad(output):
+    if os.path.isfile(output):
+        print "skipping existing file %s" % output
+        return
+    print "rendering %s" % output
+    subprocess.check_call([
+        'openscad',
+        '-o',
+        output,
+        config.TMP_SCAD
+        ])
+
+def precompile_all_stl():
+    base = os.path.abspath('stl')
+    hmin = 9999
+    rmax = 0
+    for cyl in data.CYLINDER.keys():
+        with open(config.TMP_SCAD, "w") as fn:
+            fn.write(OPENSCAD_MODULES)
+            fn.write("tank(0, %f, %f);" % (
+                data.CYLINDER[cyl][0] / 2 * config.SCALE3D,
+                data.CYLINDER[cyl][1] * config.SCALE3D))
+        run_openscad(os.path.join(base,
+            'cylinder_' + cyl + '.stl'))
+        if data.CYLINDER[cyl][1] < hmin:
+            hmin = data.CYLINDER[cyl][1]
+        if data.CYLINDER[cyl][0] > rmax:
+            rmax = data.CYLINDER[cyl][0]
+
+    for pipe in data.PIPES:
+        with open(config.TMP_SCAD, "w") as fn:
+            fn.write(OPENSCAD_MODULES)
+            fn.write("spacer(0, %f, %f, %f);" % (
+                pipe[1] / 2 * config.SCALE3D,
+                rmax / 2 * config.SCALE3D,
+                hmin * config.SCALE3D))
+        run_openscad(os.path.join(base,
+            'spacer_' + pipe[0] + '.stl'))
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stl/README	Mon Apr 03 19:40:06 2017 +0200
@@ -0,0 +1,2 @@
+Precompiled STL files, generate with 
+cylindertransport.py --precompile dummy
\ No newline at end of file

mercurial