cylindertransport.py

Thu, 06 Apr 2017 17:18:24 +0200

author
mdd
date
Thu, 06 Apr 2017 17:18:24 +0200
changeset 19
32de35694e56
parent 14
ba3d8c56e6f5
permissions
-rw-r--r--

finishing first release

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

mercurial