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