finished cylindertransport.py

Mon, 03 Apr 2017 04:47:41 +0200

author
mdd
date
Mon, 03 Apr 2017 04:47:41 +0200
changeset 5
c2158ae1dc05
parent 4
f62562506053
child 6
57f17c62c137

finished cylindertransport.py

cylindertransport.py file | annotate | diff | comparison | revisions
--- a/cylindertransport.py	Mon Apr 03 04:19:45 2017 +0200
+++ b/cylindertransport.py	Mon Apr 03 04:47:41 2017 +0200
@@ -17,7 +17,7 @@
     """
     return 2 * sqrt(r_1 * r_2)
 
-class CylinderSpacerCalculator():
+class CylinderSpacerCalculator(object):
     """
     Class to calculate transport spacer pipes between
     Scuba cylinders
@@ -38,24 +38,24 @@
         Tauchflaschen r_1 und r_2 nicht mehr beruehren
         Rueckgabe: 3 Zylinderradien und das label der verwendeten Roehre
         """
-        for p in PIPES:
-            i = p[1] / 2
+        for pipe in PIPES:
+            i = pipe[1] / 2
             x_1 = offset(r_1, i)
             x_2 = offset(r_2, i)
-            x = (x_1 + x_2) - (r_1 + r_2)
-            if x >= self.space_min:
-                print "%s Pipe (%.1fmm), Cylinder spacing: %imm" % (
-                    p[0], p[1], x)
-                return [r_1, i, r_2, p[0]]
-        print "Abort: no suitable pipe found"
+            posx = (x_1 + x_2) - (r_1 + r_2)
+            if posx >= self.space_min:
+                print "// %s Pipe (%.1fmm), Cylinder spacing: %imm" % (
+                    pipe[0], pipe[1], posx)
+                return [r_1, i, r_2, pipe[0]]
+        print "// Abort: no suitable pipe found"
         sys.exit(1)
 
-    def _circle(self, x, r, txt="", size=1.0):
+    def _circle(self, posx, radius, txt="", size=1.0):
         """
         Push the circle definition for later rendering
         """
         self.circles.append([
-            x, r, txt, size
+            posx, radius, txt, size
             ])
 
     def _calculate(self):
@@ -65,48 +65,51 @@
         # first bottle spacer
         r_1 = CYLINDER[self.cylinders[0]][0] / 2
         r_2 = CYLINDER[self.cylinders[1]][0] / 2
-        r_1, r_2, r_3, dn = self.calc_min(r_1, r_2)
-        x = self.margin + r_2 # start offset x
-        self._circle(x, r_2, dn, 0.5)
+        r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
+        posx = self.margin + r_2 # start offset x
+        self._circle(posx, r_2, label, 0.5)
         self.scad += "spacer(%i, %i, %i, %i);\n" % (
-            x, r_2, r_3, CYLINDER[self.cylinders[0]][1])
-        x = x + offset(r_2, r_3)
+            posx, r_2, r_3, CYLINDER[self.cylinders[0]][1])
+        posx += offset(r_2, r_3)
 
-        for i in range(0, len(self.cylinders)-1):
+        for i in range(0, len(self.cylinders) - 1):
             r_1 = CYLINDER[self.cylinders[i]][0] / 2
             r_2 = CYLINDER[self.cylinders[i+1]][0] / 2
-            r_1, r_2, r_3, dn = self.calc_min(r_1, r_2)
+            r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
             # draw cylinder
-            self._circle(x, r_1, "Tank " + self.cylinders[i])
+            self._circle(posx, r_1, "Tank " + self.cylinders[i])
             self.scad += "tank(%i, %i, %i);\n" % (
-                x, r_1, CYLINDER[self.cylinders[i]][1])
-            sx1 = x + r_1
-            x = x + offset(r_1, r_2)
+                posx, r_1, CYLINDER[self.cylinders[i]][1])
+            sx1 = posx + r_1
+            posx += offset(r_1, r_2)
             # draw right spacer
-            self._circle(x, r_2, dn, 0.5)
+            self._circle(posx, r_2, label, 0.5)
             self.scad += "spacer(%i, %i, %i, %i);\n" % (
-                x, r_2, r_1, CYLINDER[self.cylinders[i]][1])
-            x = x + offset(r_2, r_3)
-            sx2 = x - r_3
+                posx, r_2, r_1, CYLINDER[self.cylinders[i]][1])
+            posx += offset(r_2, r_3)
+            sx2 = posx - r_3
             if i == (len(self.cylinders) - 2):
                 # draw last bottle
-                self._circle(x, r_3, "Tank " + self.cylinders[i + 1])
+                self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
                 self.scad += "tank(%i, %i, %i);\n" % (
-                    x, r_3, CYLINDER[self.cylinders[i + 1]][1])
-                x = x + offset(r_2, r_3)
+                    posx, r_3, CYLINDER[self.cylinders[i + 1]][1])
+                posx += offset(r_2, r_3)
 
             self.spacings.append([sx1, sx2])
 
         # last bottle spacer pipe
-        self._circle(x, r_2, dn, 0.5)
+        self._circle(posx, r_2, label, 0.5)
         self.scad += "spacer(%i, %i, %i, %i);\n" % (
-            x, r_2, r_3, CYLINDER[self.cylinders[len(self.cylinders)]][1])
-        return int(x + r_2 + self.margin)
+            posx, r_2, r_3, CYLINDER[self.cylinders[-1]][1])
+        return int(posx + r_2 + self.margin)
 
-    def centertext(self, draw, x, y, txt, size):
+    def centertext(self, draw, posx, posy, txt, size):
+        """
+        Centers text at position horizontally and vertically
+        """
         font = ImageFont.truetype(self.font, int(24 * size))
         tox, toy = draw.textsize(txt, font=font)
-        draw.text((x - tox / 2, y - toy / 2), \
+        draw.text((posx - tox / 2, posy - toy / 2), \
             txt, font=font, fill='#ffffff')
 
     def render_image(self):
@@ -117,11 +120,12 @@
         image = Image.new('1', (width, 250)) # create new image
         draw = ImageDraw.Draw(image)
         # draw calculated circles
-        for circle in self.circles:
-            x, r, txt, size = circle
-            draw.arc([x - r, 0, x + r, 2 * r], 0, 360, 'white')
+        for posx, radius, txt, size in self.circles:
+            draw.arc([posx - radius, self.margin, \
+                posx + radius, 2 * radius + self.margin], \
+                0, 360, 'white')
             if txt != "":
-                self.centertext(draw, x, r, txt, size)
+                self.centertext(draw, posx, radius + self.margin, txt, size)
 
         # draw the spacing between cylinders
         spacer_y1 = 200
@@ -134,7 +138,10 @@
 
         return image
 
-if __name__ == "__main__":
+def run():
+    """
+    Command line program invocation
+    """
     parser = argparse.ArgumentParser(description=\
         "Calculate spacer pipes for pressure cylinder transport\n" +\
         "Known cylinder types:\n" + ", ".join(sorted(CYLINDER.keys())))
@@ -154,8 +161,12 @@
     worker = CylinderSpacerCalculator(
         options.cylinders, options.space_min)
 
-    image = worker.render_image()
-    image.show()
+    img = worker.render_image()
+    img.show()
 
-    print "\n------------ START SCAD SCRIPT ------------"
-    print worker.scad + "------------ END SCAD SCRIPT ------------"
+    print "\n// ------------ START SCAD SCRIPT ------------"
+    print worker.scad + "// ------------ END SCAD SCRIPT ------------"
+
+
+if __name__ == "__main__":
+    run()

mercurial