cylindertransport.py

Mon, 03 Apr 2017 19:40:06 +0200

author
mdd
date
Mon, 03 Apr 2017 19:40:06 +0200
changeset 9
a01a3fd32073
parent 8
63b6f80e09ef
child 11
098335a1d510
permissions
-rw-r--r--

precompile STL files with openscad

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:
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
55 print "// %s Pipe (%.1fmm), Cylinder spacing: %imm" % (
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
56 pipe[0], pipe[1], posx)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
57 return [r_1, i, r_2, pipe[0]]
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
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
61 def _circle(self, posx, radius, txt="", size=1.0):
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 """
mdd
parents: 3
diff changeset
65 self.circles.append([
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
66 posx, radius, txt, size
4
mdd
parents: 3
diff changeset
67 ])
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
68
4
mdd
parents: 3
diff changeset
69 def _calculate(self):
mdd
parents: 3
diff changeset
70 """
mdd
parents: 3
diff changeset
71 Calculate all cylinder and spacer circles
mdd
parents: 3
diff changeset
72 """
mdd
parents: 3
diff changeset
73 # first bottle spacer
mdd
parents: 3
diff changeset
74 r_1 = CYLINDER[self.cylinders[0]][0] / 2
mdd
parents: 3
diff changeset
75 r_2 = CYLINDER[self.cylinders[1]][0] / 2
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
76 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
77 posx = self.margin + r_2 # start offset x
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
78 self._circle(posx, r_2, label, 0.5)
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
79 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
80 posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
81 CYLINDER[self.cylinders[0]][1] * SCALE3D)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
82 posx += offset(r_2, r_3)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
83
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
84 for i in range(0, len(self.cylinders) - 1):
4
mdd
parents: 3
diff changeset
85 r_1 = CYLINDER[self.cylinders[i]][0] / 2
mdd
parents: 3
diff changeset
86 r_2 = CYLINDER[self.cylinders[i+1]][0] / 2
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
87 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
4
mdd
parents: 3
diff changeset
88 # draw cylinder
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
89 self._circle(posx, r_1, "Tank " + self.cylinders[i])
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
90 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
91 posx * SCALE3D, r_1 * SCALE3D,
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
92 CYLINDER[self.cylinders[i]][1] * SCALE3D)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
93 sx1 = posx + r_1
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
94 posx += offset(r_1, r_2)
4
mdd
parents: 3
diff changeset
95 # draw right spacer
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
96 self._circle(posx, r_2, label, 0.5)
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
97 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
98 posx * SCALE3D, r_2 * 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 posx += offset(r_2, r_3)
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
101 sx2 = posx - r_3
4
mdd
parents: 3
diff changeset
102 if i == (len(self.cylinders) - 2):
mdd
parents: 3
diff changeset
103 # draw last bottle
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
104 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
105 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
106 posx * SCALE3D, r_3 * SCALE3D,
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
107 CYLINDER[self.cylinders[i + 1]][1] * SCALE3D)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
108 posx += offset(r_2, r_3)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
109
4
mdd
parents: 3
diff changeset
110 self.spacings.append([sx1, sx2])
mdd
parents: 3
diff changeset
111
mdd
parents: 3
diff changeset
112 # last bottle spacer pipe
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
113 self._circle(posx, r_2, label, 0.5)
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
114 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
115 posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
116 CYLINDER[self.cylinders[-1]][1] * SCALE3D)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
117 return int(posx + r_2 + self.margin)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
118
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
119 def centertext(self, draw, posx, posy, txt, size):
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
120 """
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
121 Centers text at position horizontally and vertically
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
122 """
4
mdd
parents: 3
diff changeset
123 font = ImageFont.truetype(self.font, int(24 * size))
mdd
parents: 3
diff changeset
124 tox, toy = draw.textsize(txt, font=font)
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
125 draw.text((posx - tox / 2, posy - toy / 2), \
4
mdd
parents: 3
diff changeset
126 txt, font=font, fill='#ffffff')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
127
4
mdd
parents: 3
diff changeset
128 def render_image(self):
mdd
parents: 3
diff changeset
129 """
mdd
parents: 3
diff changeset
130 Start the calculation and return rendered PIL image object
mdd
parents: 3
diff changeset
131 """
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
132 self.width = self._calculate()
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
133 image = Image.new('1', (self.width, 250)) # create new image
4
mdd
parents: 3
diff changeset
134 draw = ImageDraw.Draw(image)
mdd
parents: 3
diff changeset
135 # draw calculated circles
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
136 for posx, radius, txt, size in self.circles:
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
137 draw.arc([posx - radius, self.margin, \
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
138 posx + radius, 2 * radius + self.margin], \
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
139 0, 360, 'white')
4
mdd
parents: 3
diff changeset
140 if txt != "":
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
141 self.centertext(draw, posx, radius + self.margin, txt, size)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
142
4
mdd
parents: 3
diff changeset
143 # draw the spacing between cylinders
mdd
parents: 3
diff changeset
144 spacer_y1 = 200
mdd
parents: 3
diff changeset
145 spacer_y2 = 220
mdd
parents: 3
diff changeset
146 for sx1, sx2 in self.spacings:
mdd
parents: 3
diff changeset
147 draw.line((sx1, spacer_y1, sx1, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
148 draw.line((sx2, spacer_y1, sx2, spacer_y2), fill='#ffffff')
mdd
parents: 3
diff changeset
149 self.centertext(draw, sx1 + (sx2 - sx1) / 2, \
mdd
parents: 3
diff changeset
150 spacer_y2 + 10, "%imm" % (sx2 - sx1), 0.5)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
151
4
mdd
parents: 3
diff changeset
152 return image
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
153
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
154 def run():
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
155 """
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
156 Command line program invocation
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
157 """
4
mdd
parents: 3
diff changeset
158 parser = argparse.ArgumentParser(description=\
mdd
parents: 3
diff changeset
159 "Calculate spacer pipes for pressure cylinder transport\n" +\
mdd
parents: 3
diff changeset
160 "Known cylinder types:\n" + ", ".join(sorted(CYLINDER.keys())))
mdd
parents: 3
diff changeset
161 parser.add_argument('cylinders', metavar='cylinder', \
mdd
parents: 3
diff changeset
162 type=str, nargs='+', help='cylinder types')
mdd
parents: 3
diff changeset
163 parser.add_argument('--space', dest='space_min', \
mdd
parents: 3
diff changeset
164 type=int, default=10, \
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
165 help='minimum space between cylinders (mm)')
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
166 parser.add_argument('--scad', dest='scad', \
57f17c62c137 finishing
mdd
parents: 5
diff changeset
167 type=str, default="", metavar='filename', \
57f17c62c137 finishing
mdd
parents: 5
diff changeset
168 help='Write OpenSCAD script file')
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
169 parser.add_argument('--precompile', dest='precompile', \
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
170 default=False, action='store_true', \
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
171 help='Precompile STL objects for 3D View')
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
172
4
mdd
parents: 3
diff changeset
173 options = parser.parse_args()
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
174
9
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
175 if options.precompile:
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
176 precompile_all_stl()
a01a3fd32073 precompile STL files with openscad
mdd
parents: 8
diff changeset
177
4
mdd
parents: 3
diff changeset
178 for test in options.cylinders:
mdd
parents: 3
diff changeset
179 if not test in CYLINDER.keys():
mdd
parents: 3
diff changeset
180 print "Cylinder type '%s' is unknown" % test
mdd
parents: 3
diff changeset
181 sys.exit(1)
0
007bbb709982 cylinder transport spacer calculation
mdd
parents:
diff changeset
182
4
mdd
parents: 3
diff changeset
183 worker = CylinderSpacerCalculator(
mdd
parents: 3
diff changeset
184 options.cylinders, options.space_min)
3
d8745f771267 objectoriz0r with separate calculation and rendering
mdd
parents: 2
diff changeset
185
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
186 img = worker.render_image()
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
187 img.show()
1
14a420653a5f output openscad script
mdd
parents: 0
diff changeset
188
6
57f17c62c137 finishing
mdd
parents: 5
diff changeset
189 if (options.scad != ""):
57f17c62c137 finishing
mdd
parents: 5
diff changeset
190 with open(options.scad, "w") as fd:
8
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
191 fd.write(worker.scad["tmpl"])
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
192 fd.write("translate([%f,0,0]) {\n" % (((worker.width - 2 * worker.margin) / -2)*worker.scale3d)) # center the object
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
193 fd.write(worker.scad["cylinder"])
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
194 fd.write(worker.scad["spacer"])
63b6f80e09ef added threejs stl viewer html testing
mdd
parents: 6
diff changeset
195 fd.write("}\n")
5
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
196
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
197 if __name__ == "__main__":
c2158ae1dc05 finished cylindertransport.py
mdd
parents: 4
diff changeset
198 run()

mercurial