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