Mon, 03 Apr 2017 04:47:41 +0200
finished cylindertransport.py
4 | 1 | """ |
2 | Calculation of spacer pipes for Scuba cylinder transportation | |
3 | 2017 by NeoSoft, mdd | |
4 | Input: see commandline help | |
5 | Output: 2D schematic & 3D OpenSCAD script | |
6 | """ | |
0 | 7 | from math import sqrt |
8 | from PIL import Image, ImageDraw, ImageFont | |
9 | import argparse, sys | |
2 | 10 | from data import CYLINDER, PIPES |
11 | from config import FONTBASE | |
1 | 12 | |
4 | 13 | def offset(r_1, r_2): |
14 | """ | |
15 | Calculate horizontal center offset of two circles | |
16 | so they tangent each other | |
17 | """ | |
18 | return 2 * sqrt(r_1 * r_2) | |
0 | 19 | |
5 | 20 | class CylinderSpacerCalculator(object): |
4 | 21 | """ |
22 | Class to calculate transport spacer pipes between | |
23 | Scuba cylinders | |
24 | """ | |
25 | def __init__(self, cylinders, space_min=10): | |
26 | self.cylinders = cylinders | |
27 | self.space_min = space_min | |
28 | self.font = FONTBASE + "arial.ttf" | |
29 | self.scad = "// Color support only in compile mode (F5)\n" +\ | |
30 | "include <cylindertransport.scad>\n" | |
31 | self.circles = [] | |
32 | self.spacings = [] | |
33 | self.margin = 20 | |
0 | 34 | |
4 | 35 | def calc_min(self, r_1, r_2): |
36 | """ | |
37 | stupider annaehreungsversuch, bis sich die beiden | |
38 | Tauchflaschen r_1 und r_2 nicht mehr beruehren | |
39 | Rueckgabe: 3 Zylinderradien und das label der verwendeten Roehre | |
40 | """ | |
5 | 41 | for pipe in PIPES: |
42 | i = pipe[1] / 2 | |
4 | 43 | x_1 = offset(r_1, i) |
44 | x_2 = offset(r_2, i) | |
5 | 45 | posx = (x_1 + x_2) - (r_1 + r_2) |
46 | if posx >= self.space_min: | |
47 | print "// %s Pipe (%.1fmm), Cylinder spacing: %imm" % ( | |
48 | pipe[0], pipe[1], posx) | |
49 | return [r_1, i, r_2, pipe[0]] | |
50 | print "// Abort: no suitable pipe found" | |
4 | 51 | sys.exit(1) |
0 | 52 | |
5 | 53 | def _circle(self, posx, radius, txt="", size=1.0): |
4 | 54 | """ |
55 | Push the circle definition for later rendering | |
56 | """ | |
57 | self.circles.append([ | |
5 | 58 | posx, radius, txt, size |
4 | 59 | ]) |
3 | 60 | |
4 | 61 | def _calculate(self): |
62 | """ | |
63 | Calculate all cylinder and spacer circles | |
64 | """ | |
65 | # first bottle spacer | |
66 | r_1 = CYLINDER[self.cylinders[0]][0] / 2 | |
67 | r_2 = CYLINDER[self.cylinders[1]][0] / 2 | |
5 | 68 | r_1, r_2, r_3, label = self.calc_min(r_1, r_2) |
69 | posx = self.margin + r_2 # start offset x | |
70 | self._circle(posx, r_2, label, 0.5) | |
4 | 71 | self.scad += "spacer(%i, %i, %i, %i);\n" % ( |
5 | 72 | posx, r_2, r_3, CYLINDER[self.cylinders[0]][1]) |
73 | posx += offset(r_2, r_3) | |
0 | 74 | |
5 | 75 | for i in range(0, len(self.cylinders) - 1): |
4 | 76 | r_1 = CYLINDER[self.cylinders[i]][0] / 2 |
77 | r_2 = CYLINDER[self.cylinders[i+1]][0] / 2 | |
5 | 78 | r_1, r_2, r_3, label = self.calc_min(r_1, r_2) |
4 | 79 | # draw cylinder |
5 | 80 | self._circle(posx, r_1, "Tank " + self.cylinders[i]) |
4 | 81 | self.scad += "tank(%i, %i, %i);\n" % ( |
5 | 82 | posx, r_1, CYLINDER[self.cylinders[i]][1]) |
83 | sx1 = posx + r_1 | |
84 | posx += offset(r_1, r_2) | |
4 | 85 | # draw right spacer |
5 | 86 | self._circle(posx, r_2, label, 0.5) |
4 | 87 | self.scad += "spacer(%i, %i, %i, %i);\n" % ( |
5 | 88 | posx, r_2, r_1, CYLINDER[self.cylinders[i]][1]) |
89 | posx += offset(r_2, r_3) | |
90 | sx2 = posx - r_3 | |
4 | 91 | if i == (len(self.cylinders) - 2): |
92 | # draw last bottle | |
5 | 93 | self._circle(posx, r_3, "Tank " + self.cylinders[i + 1]) |
4 | 94 | self.scad += "tank(%i, %i, %i);\n" % ( |
5 | 95 | posx, r_3, CYLINDER[self.cylinders[i + 1]][1]) |
96 | posx += offset(r_2, r_3) | |
3 | 97 | |
4 | 98 | self.spacings.append([sx1, sx2]) |
99 | ||
100 | # last bottle spacer pipe | |
5 | 101 | self._circle(posx, r_2, label, 0.5) |
4 | 102 | self.scad += "spacer(%i, %i, %i, %i);\n" % ( |
5 | 103 | posx, r_2, r_3, CYLINDER[self.cylinders[-1]][1]) |
104 | return int(posx + r_2 + self.margin) | |
3 | 105 | |
5 | 106 | def centertext(self, draw, posx, posy, txt, size): |
107 | """ | |
108 | Centers text at position horizontally and vertically | |
109 | """ | |
4 | 110 | font = ImageFont.truetype(self.font, int(24 * size)) |
111 | tox, toy = draw.textsize(txt, font=font) | |
5 | 112 | draw.text((posx - tox / 2, posy - toy / 2), \ |
4 | 113 | txt, font=font, fill='#ffffff') |
0 | 114 | |
4 | 115 | def render_image(self): |
116 | """ | |
117 | Start the calculation and return rendered PIL image object | |
118 | """ | |
119 | width = self._calculate() | |
120 | image = Image.new('1', (width, 250)) # create new image | |
121 | draw = ImageDraw.Draw(image) | |
122 | # draw calculated circles | |
5 | 123 | for posx, radius, txt, size in self.circles: |
124 | draw.arc([posx - radius, self.margin, \ | |
125 | posx + radius, 2 * radius + self.margin], \ | |
126 | 0, 360, 'white') | |
4 | 127 | if txt != "": |
5 | 128 | self.centertext(draw, posx, radius + self.margin, txt, size) |
0 | 129 | |
4 | 130 | # draw the spacing between cylinders |
131 | spacer_y1 = 200 | |
132 | spacer_y2 = 220 | |
133 | for sx1, sx2 in self.spacings: | |
134 | draw.line((sx1, spacer_y1, sx1, spacer_y2), fill='#ffffff') | |
135 | draw.line((sx2, spacer_y1, sx2, spacer_y2), fill='#ffffff') | |
136 | self.centertext(draw, sx1 + (sx2 - sx1) / 2, \ | |
137 | spacer_y2 + 10, "%imm" % (sx2 - sx1), 0.5) | |
3 | 138 | |
4 | 139 | return image |
0 | 140 | |
5 | 141 | def run(): |
142 | """ | |
143 | Command line program invocation | |
144 | """ | |
4 | 145 | parser = argparse.ArgumentParser(description=\ |
146 | "Calculate spacer pipes for pressure cylinder transport\n" +\ | |
147 | "Known cylinder types:\n" + ", ".join(sorted(CYLINDER.keys()))) | |
148 | parser.add_argument('cylinders', metavar='cylinder', \ | |
149 | type=str, nargs='+', help='cylinder types') | |
150 | parser.add_argument('--space', dest='space_min', \ | |
151 | type=int, default=10, \ | |
3 | 152 | help='minimum space between cylinders (mm)') |
0 | 153 | |
4 | 154 | options = parser.parse_args() |
0 | 155 | |
4 | 156 | for test in options.cylinders: |
157 | if not test in CYLINDER.keys(): | |
158 | print "Cylinder type '%s' is unknown" % test | |
159 | sys.exit(1) | |
0 | 160 | |
4 | 161 | worker = CylinderSpacerCalculator( |
162 | options.cylinders, options.space_min) | |
3 | 163 | |
5 | 164 | img = worker.render_image() |
165 | img.show() | |
1 | 166 | |
5 | 167 | print "\n// ------------ START SCAD SCRIPT ------------" |
168 | print worker.scad + "// ------------ END SCAD SCRIPT ------------" | |
169 | ||
170 | ||
171 | if __name__ == "__main__": | |
172 | run() |