cylindertransport.py

Mon, 03 Apr 2017 04:19:45 +0200

author
mdd
date
Mon, 03 Apr 2017 04:19:45 +0200
changeset 4
f62562506053
parent 3
d8745f771267
child 5
c2158ae1dc05
permissions
-rw-r--r--

pylint

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
4
mdd
parents: 3
diff changeset
20 class CylinderSpacerCalculator():
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 """
mdd
parents: 3
diff changeset
41 for p in PIPES:
mdd
parents: 3
diff changeset
42 i = p[1] / 2
mdd
parents: 3
diff changeset
43 x_1 = offset(r_1, i)
mdd
parents: 3
diff changeset
44 x_2 = offset(r_2, i)
mdd
parents: 3
diff changeset
45 x = (x_1 + x_2) - (r_1 + r_2)
mdd
parents: 3
diff changeset
46 if x >= self.space_min:
mdd
parents: 3
diff changeset
47 print "%s Pipe (%.1fmm), Cylinder spacing: %imm" % (
mdd
parents: 3
diff changeset
48 p[0], p[1], x)
mdd
parents: 3
diff changeset
49 return [r_1, i, r_2, p[0]]
mdd
parents: 3
diff changeset
50 print "Abort: no suitable pipe found"
mdd
parents: 3
diff changeset
51 sys.exit(1)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
52
4
mdd
parents: 3
diff changeset
53 def _circle(self, x, r, txt="", size=1.0):
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([
mdd
parents: 3
diff changeset
58 x, r, txt, size
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
mdd
parents: 3
diff changeset
68 r_1, r_2, r_3, dn = self.calc_min(r_1, r_2)
mdd
parents: 3
diff changeset
69 x = self.margin + r_2 # start offset x
mdd
parents: 3
diff changeset
70 self._circle(x, r_2, dn, 0.5)
mdd
parents: 3
diff changeset
71 self.scad += "spacer(%i, %i, %i, %i);\n" % (
mdd
parents: 3
diff changeset
72 x, r_2, r_3, CYLINDER[self.cylinders[0]][1])
mdd
parents: 3
diff changeset
73 x = x + offset(r_2, r_3)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
74
4
mdd
parents: 3
diff changeset
75 for i in range(0, len(self.cylinders)-1):
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
mdd
parents: 3
diff changeset
78 r_1, r_2, r_3, dn = self.calc_min(r_1, r_2)
mdd
parents: 3
diff changeset
79 # draw cylinder
mdd
parents: 3
diff changeset
80 self._circle(x, r_1, "Tank " + self.cylinders[i])
mdd
parents: 3
diff changeset
81 self.scad += "tank(%i, %i, %i);\n" % (
mdd
parents: 3
diff changeset
82 x, r_1, CYLINDER[self.cylinders[i]][1])
mdd
parents: 3
diff changeset
83 sx1 = x + r_1
mdd
parents: 3
diff changeset
84 x = x + offset(r_1, r_2)
mdd
parents: 3
diff changeset
85 # draw right spacer
mdd
parents: 3
diff changeset
86 self._circle(x, r_2, dn, 0.5)
mdd
parents: 3
diff changeset
87 self.scad += "spacer(%i, %i, %i, %i);\n" % (
mdd
parents: 3
diff changeset
88 x, r_2, r_1, CYLINDER[self.cylinders[i]][1])
mdd
parents: 3
diff changeset
89 x = x + offset(r_2, r_3)
mdd
parents: 3
diff changeset
90 sx2 = x - r_3
mdd
parents: 3
diff changeset
91 if i == (len(self.cylinders) - 2):
mdd
parents: 3
diff changeset
92 # draw last bottle
mdd
parents: 3
diff changeset
93 self._circle(x, r_3, "Tank " + self.cylinders[i + 1])
mdd
parents: 3
diff changeset
94 self.scad += "tank(%i, %i, %i);\n" % (
mdd
parents: 3
diff changeset
95 x, r_3, CYLINDER[self.cylinders[i + 1]][1])
mdd
parents: 3
diff changeset
96 x = x + 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
mdd
parents: 3
diff changeset
101 self._circle(x, r_2, dn, 0.5)
mdd
parents: 3
diff changeset
102 self.scad += "spacer(%i, %i, %i, %i);\n" % (
mdd
parents: 3
diff changeset
103 x, r_2, r_3, CYLINDER[self.cylinders[len(self.cylinders)]][1])
mdd
parents: 3
diff changeset
104 return int(x + r_2 + self.margin)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
105
4
mdd
parents: 3
diff changeset
106 def centertext(self, draw, x, y, txt, size):
mdd
parents: 3
diff changeset
107 font = ImageFont.truetype(self.font, int(24 * size))
mdd
parents: 3
diff changeset
108 tox, toy = draw.textsize(txt, font=font)
mdd
parents: 3
diff changeset
109 draw.text((x - tox / 2, y - toy / 2), \
mdd
parents: 3
diff changeset
110 txt, font=font, fill='#ffffff')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
111
4
mdd
parents: 3
diff changeset
112 def render_image(self):
mdd
parents: 3
diff changeset
113 """
mdd
parents: 3
diff changeset
114 Start the calculation and return rendered PIL image object
mdd
parents: 3
diff changeset
115 """
mdd
parents: 3
diff changeset
116 width = self._calculate()
mdd
parents: 3
diff changeset
117 image = Image.new('1', (width, 250)) # create new image
mdd
parents: 3
diff changeset
118 draw = ImageDraw.Draw(image)
mdd
parents: 3
diff changeset
119 # draw calculated circles
mdd
parents: 3
diff changeset
120 for circle in self.circles:
mdd
parents: 3
diff changeset
121 x, r, txt, size = circle
mdd
parents: 3
diff changeset
122 draw.arc([x - r, 0, x + r, 2 * r], 0, 360, 'white')
mdd
parents: 3
diff changeset
123 if txt != "":
mdd
parents: 3
diff changeset
124 self.centertext(draw, x, r, txt, size)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
125
4
mdd
parents: 3
diff changeset
126 # draw the spacing between cylinders
mdd
parents: 3
diff changeset
127 spacer_y1 = 200
mdd
parents: 3
diff changeset
128 spacer_y2 = 220
mdd
parents: 3
diff changeset
129 for sx1, sx2 in self.spacings:
mdd
parents: 3
diff changeset
130 draw.line((sx1, spacer_y1, sx1, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
131 draw.line((sx2, spacer_y1, sx2, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
132 self.centertext(draw, sx1 + (sx2 - sx1) / 2, \
mdd
parents: 3
diff changeset
133 spacer_y2 + 10, "%imm" % (sx2 - sx1), 0.5)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
134
4
mdd
parents: 3
diff changeset
135 return image
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
136
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
137 if __name__ == "__main__":
4
mdd
parents: 3
diff changeset
138 parser = argparse.ArgumentParser(description=\
mdd
parents: 3
diff changeset
139 "Calculate spacer pipes for pressure cylinder transport\n" +\
mdd
parents: 3
diff changeset
140 "Known cylinder types:\n" + ", ".join(sorted(CYLINDER.keys())))
mdd
parents: 3
diff changeset
141 parser.add_argument('cylinders', metavar='cylinder', \
mdd
parents: 3
diff changeset
142 type=str, nargs='+', help='cylinder types')
mdd
parents: 3
diff changeset
143 parser.add_argument('--space', dest='space_min', \
mdd
parents: 3
diff changeset
144 type=int, default=10, \
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
145 help='minimum space between cylinders (mm)')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
146
4
mdd
parents: 3
diff changeset
147 options = parser.parse_args()
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
148
4
mdd
parents: 3
diff changeset
149 for test in options.cylinders:
mdd
parents: 3
diff changeset
150 if not test in CYLINDER.keys():
mdd
parents: 3
diff changeset
151 print "Cylinder type '%s' is unknown" % test
mdd
parents: 3
diff changeset
152 sys.exit(1)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
153
4
mdd
parents: 3
diff changeset
154 worker = CylinderSpacerCalculator(
mdd
parents: 3
diff changeset
155 options.cylinders, options.space_min)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
156
4
mdd
parents: 3
diff changeset
157 image = worker.render_image()
mdd
parents: 3
diff changeset
158 image.show()
1
14a420653a5f output openscad script
mdd
parents: 0
diff changeset
159
4
mdd
parents: 3
diff changeset
160 print "\n------------ START SCAD SCRIPT ------------"
mdd
parents: 3
diff changeset
161 print worker.scad + "------------ END SCAD SCRIPT ------------"

mercurial