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
permissions
-rw-r--r--

finished cylindertransport.py

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

mercurial