code cleanup cylindertransport_v2

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

author
mdd
date
Wed, 05 Apr 2017 00:59:45 +0200
changeset 14
ba3d8c56e6f5
parent 13
39fb313ba27c
child 15
0760f2016167

code cleanup

cylindertransport-web.py file | annotate | diff | comparison | revisions
cylindertransport.py file | annotate | diff | comparison | revisions
stl.py file | annotate | diff | comparison | revisions
stlviewer.html file | annotate | diff | comparison | revisions
--- a/cylindertransport-web.py	Wed Apr 05 00:39:31 2017 +0200
+++ b/cylindertransport-web.py	Wed Apr 05 00:59:45 2017 +0200
@@ -1,16 +1,22 @@
 #!/usr/bin/env python
 # -*- coding: UTF-8 -*-
+"""
+Web CGI script for 3D View
+"""
 
 import cgi, data, json, config
 from cylindertransport import CylinderSpacerCalculator
 
 # enable debugging
-import cgitb, sys
-sys.stderr = sys.stdout
+#import sys
+#sys.stderr = sys.stdout
+#import cgitb
 #cgitb.enable()
 
-
 def do_action(args):
+    """
+    process some actions (like JSON requests)
+    """
     if args["action"].value == "calculate":
         cylinders = args.getlist("cylinders[]")
         calc = CylinderSpacerCalculator(cylinders)
@@ -22,24 +28,26 @@
     else:
         print "unknown Action %s" % args["action"].value
 
-
-
-
-
-
-print("Content-Type: text/html;charset=utf-8")
-print("")
+def run():
+    """
+    Main program, without action displays the html content
+    """
+    print "Content-Type: text/html;charset=utf-8"
+    print ""
 
-args = cgi.FieldStorage()
-if "action" in args:
-    do_action(args)
-else:
-    # display the html content
-    content = open("stlviewer.html", "r").read()
-    cyls = ""
-    for cyl in sorted(data.CYLINDER.keys()):
-        cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
-            cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]);
-    content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
+    args = cgi.FieldStorage()
+    if "action" in args:
+        do_action(args)
+    else:
+        # display the html content
+        content = open("stlviewer.html", "r").read()
+        cyls = ""
+        for cyl in sorted(data.CYLINDER.keys()):
+            cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
+                cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4])
+        content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
 
-    print content
+        print content
+
+if __name__ == "__main__":
+    run()
--- a/cylindertransport.py	Wed Apr 05 00:39:31 2017 +0200
+++ b/cylindertransport.py	Wed Apr 05 00:59:45 2017 +0200
@@ -58,7 +58,7 @@
         print "// Abort: no suitable pipe found"
         sys.exit(1)
 
-    def _circle(self, posx, radius, data, cylinder = ""):
+    def _circle(self, posx, radius, data, cylinder=""):
         """
         Push the circle definition for later rendering
         """
@@ -92,7 +92,7 @@
             r_2 = CYLINDER[self.cylinders[i+1]][0] / 2
             r_1, r_2, r_3, data = self.calc_min(r_1, r_2)
             # draw cylinder
-            self._circle(posx, r_1,
+            self._circle(posx, r_1, \
                 CYLINDER[self.cylinders[i]], self.cylinders[i])
             self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
                 posx * SCALE3D, r_1 * SCALE3D,
@@ -108,7 +108,7 @@
             sx2 = posx - r_3
             if i == (len(self.cylinders) - 2):
                 # draw last bottle
-                self._circle(posx, r_3,
+                self._circle(posx, r_3, \
                     CYLINDER[self.cylinders[i + 1]], self.cylinders[i + 1])
                 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
                     posx * SCALE3D, r_3 * SCALE3D,
@@ -194,11 +194,12 @@
     img = worker.render_image()
     img.show()
 
-    if (options.scad != ""):
+    if options.scad != "":
         with open(options.scad, "w") as fd:
             fd.write(worker.scad["tmpl"])
+            # center the object
             fd.write("translate([%f,0,0]) {\n" % (
-                ((worker.width - 2 * worker.margin) / -2) * SCALE3D)) # center the object
+                ((worker.width - 2 * worker.margin) / -2) * SCALE3D))
             fd.write(worker.scad["cylinder"])
             fd.write(worker.scad["spacer"])
             fd.write("}\n")
--- a/stl.py	Wed Apr 05 00:39:31 2017 +0200
+++ b/stl.py	Wed Apr 05 00:59:45 2017 +0200
@@ -1,5 +1,9 @@
-import data, os, config, subprocess
-import vtk
+"""
+STL related helpers
+"""
+
+import os, subprocess, vtk
+import config, data
 
 OPENSCAD_MODULES = """
 module tank(x, r, h) {
@@ -29,6 +33,9 @@
 """
 
 def run_openscad(output):
+    """
+    Run openSCAD process, then convert the ascii stl to binary by using vtk
+    """
     if os.path.isfile(output):
         print "skipping existing file %s" % output
         return
@@ -47,9 +54,14 @@
     write.SetFileTypeToBinary()
     write.SetInput(reader.GetOutput())
     write.SetFileName(output)
-    write.Write() 
+    write.Write()
 
 def precompile_all_stl():
+    """
+    Write .scad temp file and
+    Invoke run_openscad for each object
+    that needs to be created in stl directory
+    """
     base = os.path.abspath('stl')
     hmin = 9999
     rmax = 0
@@ -59,7 +71,7 @@
             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,
+        run_openscad(os.path.join(base, \
             'cylinder_' + cyl + '.stl'))
         if data.CYLINDER[cyl][1] < hmin:
             hmin = data.CYLINDER[cyl][1]
@@ -73,6 +85,6 @@
                 pipe[1] / 2 * config.SCALE3D,
                 rmax / 2 * config.SCALE3D,
                 hmin * config.SCALE3D))
-        run_openscad(os.path.join(base,
+        run_openscad(os.path.join(base, \
             'spacer_' + pipe[0] + '.stl'))
 
--- a/stlviewer.html	Wed Apr 05 00:39:31 2017 +0200
+++ b/stlviewer.html	Wed Apr 05 00:59:45 2017 +0200
@@ -60,7 +60,7 @@
   			init();
 			animate();
 
-		</script><div><canvas style="width: 1589px; height: 711px;" height="711" width="1589"></canvas><div style="position: fixed; top: 0px; left: 0px; cursor: pointer; opacity: 0.9; z-index: 10000;"><canvas style="width: 80px; height: 48px; display: block;" height="48" width="80"></canvas><canvas style="width: 80px; height: 48px; display: none;" height="48" width="80"></canvas></div></div>
+		</script>
 	
 
 </body></html>
\ No newline at end of file

mercurial