cylindertransport.py

Mon, 03 Apr 2017 05:21:17 +0200

author
mdd
date
Mon, 03 Apr 2017 05:21:17 +0200
changeset 6
57f17c62c137
parent 5
c2158ae1dc05
child 8
63b6f80e09ef
permissions
-rw-r--r--

finishing

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"
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
29 self.scad = "// Color support only in compile mode (F5)\n"
4
mdd
parents: 3
diff changeset
30 self.circles = []
mdd
parents: 3
diff changeset
31 self.spacings = []
mdd
parents: 3
diff changeset
32 self.margin = 20
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
33
4
mdd
parents: 3
diff changeset
34 def calc_min(self, r_1, r_2):
mdd
parents: 3
diff changeset
35 """
mdd
parents: 3
diff changeset
36 stupider annaehreungsversuch, bis sich die beiden
mdd
parents: 3
diff changeset
37 Tauchflaschen r_1 und r_2 nicht mehr beruehren
mdd
parents: 3
diff changeset
38 Rueckgabe: 3 Zylinderradien und das label der verwendeten Roehre
mdd
parents: 3
diff changeset
39 """
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
40 for pipe in PIPES:
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
41 i = pipe[1] / 2
4
mdd
parents: 3
diff changeset
42 x_1 = offset(r_1, i)
mdd
parents: 3
diff changeset
43 x_2 = offset(r_2, i)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
44 posx = (x_1 + x_2) - (r_1 + r_2)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
45 if posx >= self.space_min:
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
46 print "// %s Pipe (%.1fmm), Cylinder spacing: %imm" % (
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
47 pipe[0], pipe[1], posx)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
48 return [r_1, i, r_2, pipe[0]]
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
49 print "// Abort: no suitable pipe found"
4
mdd
parents: 3
diff changeset
50 sys.exit(1)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
51
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
52 def _circle(self, posx, radius, txt="", size=1.0):
4
mdd
parents: 3
diff changeset
53 """
mdd
parents: 3
diff changeset
54 Push the circle definition for later rendering
mdd
parents: 3
diff changeset
55 """
mdd
parents: 3
diff changeset
56 self.circles.append([
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
57 posx, radius, txt, size
4
mdd
parents: 3
diff changeset
58 ])
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
59
4
mdd
parents: 3
diff changeset
60 def _calculate(self):
mdd
parents: 3
diff changeset
61 """
mdd
parents: 3
diff changeset
62 Calculate all cylinder and spacer circles
mdd
parents: 3
diff changeset
63 """
mdd
parents: 3
diff changeset
64 # first bottle spacer
mdd
parents: 3
diff changeset
65 r_1 = CYLINDER[self.cylinders[0]][0] / 2
mdd
parents: 3
diff changeset
66 r_2 = CYLINDER[self.cylinders[1]][0] / 2
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
67 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
68 posx = self.margin + r_2 # start offset x
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
69 self._circle(posx, r_2, label, 0.5)
4
mdd
parents: 3
diff changeset
70 self.scad += "spacer(%i, %i, %i, %i);\n" % (
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
71 posx, r_2, r_3, CYLINDER[self.cylinders[0]][1])
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
72 posx += offset(r_2, r_3)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
73
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
74 for i in range(0, len(self.cylinders) - 1):
4
mdd
parents: 3
diff changeset
75 r_1 = CYLINDER[self.cylinders[i]][0] / 2
mdd
parents: 3
diff changeset
76 r_2 = CYLINDER[self.cylinders[i+1]][0] / 2
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
77 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
4
mdd
parents: 3
diff changeset
78 # draw cylinder
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
79 self._circle(posx, r_1, "Tank " + self.cylinders[i])
4
mdd
parents: 3
diff changeset
80 self.scad += "tank(%i, %i, %i);\n" % (
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
81 posx, r_1, CYLINDER[self.cylinders[i]][1])
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
82 sx1 = posx + r_1
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
83 posx += offset(r_1, r_2)
4
mdd
parents: 3
diff changeset
84 # draw right spacer
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
85 self._circle(posx, r_2, label, 0.5)
4
mdd
parents: 3
diff changeset
86 self.scad += "spacer(%i, %i, %i, %i);\n" % (
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
87 posx, r_2, r_1, CYLINDER[self.cylinders[i]][1])
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
88 posx += offset(r_2, r_3)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
89 sx2 = posx - r_3
4
mdd
parents: 3
diff changeset
90 if i == (len(self.cylinders) - 2):
mdd
parents: 3
diff changeset
91 # draw last bottle
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
92 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
4
mdd
parents: 3
diff changeset
93 self.scad += "tank(%i, %i, %i);\n" % (
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
94 posx, r_3, CYLINDER[self.cylinders[i + 1]][1])
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
95 posx += offset(r_2, r_3)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
96
4
mdd
parents: 3
diff changeset
97 self.spacings.append([sx1, sx2])
mdd
parents: 3
diff changeset
98
mdd
parents: 3
diff changeset
99 # last bottle spacer pipe
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
100 self._circle(posx, r_2, label, 0.5)
4
mdd
parents: 3
diff changeset
101 self.scad += "spacer(%i, %i, %i, %i);\n" % (
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
102 posx, r_2, r_3, CYLINDER[self.cylinders[-1]][1])
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
103 return int(posx + r_2 + self.margin)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
104
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
105 def centertext(self, draw, posx, posy, txt, size):
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
106 """
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
107 Centers text at position horizontally and vertically
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
108 """
4
mdd
parents: 3
diff changeset
109 font = ImageFont.truetype(self.font, int(24 * size))
mdd
parents: 3
diff changeset
110 tox, toy = draw.textsize(txt, font=font)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
111 draw.text((posx - tox / 2, posy - toy / 2), \
4
mdd
parents: 3
diff changeset
112 txt, font=font, fill='#ffffff')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
113
4
mdd
parents: 3
diff changeset
114 def render_image(self):
mdd
parents: 3
diff changeset
115 """
mdd
parents: 3
diff changeset
116 Start the calculation and return rendered PIL image object
mdd
parents: 3
diff changeset
117 """
mdd
parents: 3
diff changeset
118 width = self._calculate()
mdd
parents: 3
diff changeset
119 image = Image.new('1', (width, 250)) # create new image
mdd
parents: 3
diff changeset
120 draw = ImageDraw.Draw(image)
mdd
parents: 3
diff changeset
121 # draw calculated circles
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
122 for posx, radius, txt, size in self.circles:
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
123 draw.arc([posx - radius, self.margin, \
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
124 posx + radius, 2 * radius + self.margin], \
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
125 0, 360, 'white')
4
mdd
parents: 3
diff changeset
126 if txt != "":
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
127 self.centertext(draw, posx, radius + self.margin, txt, size)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
128
4
mdd
parents: 3
diff changeset
129 # draw the spacing between cylinders
mdd
parents: 3
diff changeset
130 spacer_y1 = 200
mdd
parents: 3
diff changeset
131 spacer_y2 = 220
mdd
parents: 3
diff changeset
132 for sx1, sx2 in self.spacings:
mdd
parents: 3
diff changeset
133 draw.line((sx1, spacer_y1, sx1, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
134 draw.line((sx2, spacer_y1, sx2, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
135 self.centertext(draw, sx1 + (sx2 - sx1) / 2, \
mdd
parents: 3
diff changeset
136 spacer_y2 + 10, "%imm" % (sx2 - sx1), 0.5)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
137
4
mdd
parents: 3
diff changeset
138 return image
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
139
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
140 def run():
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
141 """
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
142 Command line program invocation
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
143 """
4
mdd
parents: 3
diff changeset
144 parser = argparse.ArgumentParser(description=\
mdd
parents: 3
diff changeset
145 "Calculate spacer pipes for pressure cylinder transport\n" +\
mdd
parents: 3
diff changeset
146 "Known cylinder types:\n" + ", ".join(sorted(CYLINDER.keys())))
mdd
parents: 3
diff changeset
147 parser.add_argument('cylinders', metavar='cylinder', \
mdd
parents: 3
diff changeset
148 type=str, nargs='+', help='cylinder types')
mdd
parents: 3
diff changeset
149 parser.add_argument('--space', dest='space_min', \
mdd
parents: 3
diff changeset
150 type=int, default=10, \
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
151 help='minimum space between cylinders (mm)')
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
152 parser.add_argument('--scad', dest='scad', \
57f17c62c137 finishing
mdd
parents: 5
diff changeset
153 type=str, default="", metavar='filename', \
57f17c62c137 finishing
mdd
parents: 5
diff changeset
154 help='Write OpenSCAD script file')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
155
4
mdd
parents: 3
diff changeset
156 options = parser.parse_args()
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
157
4
mdd
parents: 3
diff changeset
158 for test in options.cylinders:
mdd
parents: 3
diff changeset
159 if not test in CYLINDER.keys():
mdd
parents: 3
diff changeset
160 print "Cylinder type '%s' is unknown" % test
mdd
parents: 3
diff changeset
161 sys.exit(1)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
162
4
mdd
parents: 3
diff changeset
163 worker = CylinderSpacerCalculator(
mdd
parents: 3
diff changeset
164 options.cylinders, options.space_min)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
165
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
166 img = worker.render_image()
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
167 img.show()
1
14a420653a5f output openscad script
mdd
parents: 0
diff changeset
168
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
169 if (options.scad != ""):
57f17c62c137 finishing
mdd
parents: 5
diff changeset
170 with open(options.scad, "w") as fd:
57f17c62c137 finishing
mdd
parents: 5
diff changeset
171 fd.write("""// cylindertransport.py OpenSCAD script
57f17c62c137 finishing
mdd
parents: 5
diff changeset
172 module tank(x, r, h) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
173 cap = r;
57f17c62c137 finishing
mdd
parents: 5
diff changeset
174 color("SteelBlue") render() rotate([90,0,0]) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
175 translate([x, r, cap]) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
176 sphere(r = r); // bottom
57f17c62c137 finishing
mdd
parents: 5
diff changeset
177 cylinder(h = h-2*cap, r = r);
57f17c62c137 finishing
mdd
parents: 5
diff changeset
178 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
179 translate([x, r, h-cap]) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
180 sphere(r = r); // top
57f17c62c137 finishing
mdd
parents: 5
diff changeset
181 cylinder(h = 30+cap, r = 25);
57f17c62c137 finishing
mdd
parents: 5
diff changeset
182 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
183 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
184 }
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
185
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
186 module spacer(x, r, rcylinder, h) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
187 cap = rcylinder;
57f17c62c137 finishing
mdd
parents: 5
diff changeset
188
57f17c62c137 finishing
mdd
parents: 5
diff changeset
189 color("DarkGrey") render() rotate([90,0,0])
57f17c62c137 finishing
mdd
parents: 5
diff changeset
190 translate([x, r, cap]) {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
191 difference() {
57f17c62c137 finishing
mdd
parents: 5
diff changeset
192 cylinder(h = h-2*cap, r = r);
57f17c62c137 finishing
mdd
parents: 5
diff changeset
193 cylinder(h = h-2*cap, r = r-5);
57f17c62c137 finishing
mdd
parents: 5
diff changeset
194 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
195 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
196 }
57f17c62c137 finishing
mdd
parents: 5
diff changeset
197 $fn = 20;\n""")
57f17c62c137 finishing
mdd
parents: 5
diff changeset
198 fd.write(worker.scad)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
199
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
200 if __name__ == "__main__":
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
201 run()

mercurial