cylindertransport.py

changeset 9
a01a3fd32073
parent 8
63b6f80e09ef
child 11
098335a1d510
equal deleted inserted replaced
8:63b6f80e09ef 9:a01a3fd32073
6 """ 6 """
7 from math import sqrt 7 from math import sqrt
8 from PIL import Image, ImageDraw, ImageFont 8 from PIL import Image, ImageDraw, ImageFont
9 import argparse, sys 9 import argparse, sys
10 from data import CYLINDER, PIPES 10 from data import CYLINDER, PIPES
11 from config import FONTBASE 11 from stl import OPENSCAD_MODULES, precompile_all_stl
12 from config import FONTBASE, SCALE3D
12 13
13 def offset(r_1, r_2): 14 def offset(r_1, r_2):
14 """ 15 """
15 Calculate horizontal center offset of two circles 16 Calculate horizontal center offset of two circles
16 so they tangent each other 17 so they tangent each other
28 self.font = FONTBASE + "arial.ttf" 29 self.font = FONTBASE + "arial.ttf"
29 self.width = 0 # will be calculated 30 self.width = 0 # will be calculated
30 self.scad = { 31 self.scad = {
31 "tmpl": """// Color support only in compile mode (F5) 32 "tmpl": """// Color support only in compile mode (F5)
32 // cylindertransport.py OpenSCAD script 33 // 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; 34 $fn = 10;
57 """, 35 """ + OPENSCAD_MODULES,
58 "spacer": "", 36 "spacer": "",
59 "cylinder": "" 37 "cylinder": ""
60 } 38 }
61 self.circles = [] 39 self.circles = []
62 self.spacings = [] 40 self.spacings = []
63 self.margin = 20 41 self.margin = 20
64 self.scale3d = 0.1
65 42
66 def calc_min(self, r_1, r_2): 43 def calc_min(self, r_1, r_2):
67 """ 44 """
68 stupider annaehreungsversuch, bis sich die beiden 45 stupider annaehreungsversuch, bis sich die beiden
69 Tauchflaschen r_1 und r_2 nicht mehr beruehren 46 Tauchflaschen r_1 und r_2 nicht mehr beruehren
98 r_2 = CYLINDER[self.cylinders[1]][0] / 2 75 r_2 = CYLINDER[self.cylinders[1]][0] / 2
99 r_1, r_2, r_3, label = self.calc_min(r_1, r_2) 76 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
100 posx = self.margin + r_2 # start offset x 77 posx = self.margin + r_2 # start offset x
101 self._circle(posx, r_2, label, 0.5) 78 self._circle(posx, r_2, label, 0.5)
102 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % ( 79 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
103 posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d, 80 posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
104 CYLINDER[self.cylinders[0]][1] * self.scale3d) 81 CYLINDER[self.cylinders[0]][1] * SCALE3D)
105 posx += offset(r_2, r_3) 82 posx += offset(r_2, r_3)
106 83
107 for i in range(0, len(self.cylinders) - 1): 84 for i in range(0, len(self.cylinders) - 1):
108 r_1 = CYLINDER[self.cylinders[i]][0] / 2 85 r_1 = CYLINDER[self.cylinders[i]][0] / 2
109 r_2 = CYLINDER[self.cylinders[i+1]][0] / 2 86 r_2 = CYLINDER[self.cylinders[i+1]][0] / 2
110 r_1, r_2, r_3, label = self.calc_min(r_1, r_2) 87 r_1, r_2, r_3, label = self.calc_min(r_1, r_2)
111 # draw cylinder 88 # draw cylinder
112 self._circle(posx, r_1, "Tank " + self.cylinders[i]) 89 self._circle(posx, r_1, "Tank " + self.cylinders[i])
113 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % ( 90 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
114 posx * self.scale3d, r_1 * self.scale3d, 91 posx * SCALE3D, r_1 * SCALE3D,
115 CYLINDER[self.cylinders[i]][1] * self.scale3d) 92 CYLINDER[self.cylinders[i]][1] * SCALE3D)
116 sx1 = posx + r_1 93 sx1 = posx + r_1
117 posx += offset(r_1, r_2) 94 posx += offset(r_1, r_2)
118 # draw right spacer 95 # draw right spacer
119 self._circle(posx, r_2, label, 0.5) 96 self._circle(posx, r_2, label, 0.5)
120 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % ( 97 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
121 posx * self.scale3d, r_2 * self.scale3d, r_1 * self.scale3d, 98 posx * SCALE3D, r_2 * SCALE3D, r_1 * SCALE3D,
122 CYLINDER[self.cylinders[i]][1] * self.scale3d) 99 CYLINDER[self.cylinders[i]][1] * SCALE3D)
123 posx += offset(r_2, r_3) 100 posx += offset(r_2, r_3)
124 sx2 = posx - r_3 101 sx2 = posx - r_3
125 if i == (len(self.cylinders) - 2): 102 if i == (len(self.cylinders) - 2):
126 # draw last bottle 103 # draw last bottle
127 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1]) 104 self._circle(posx, r_3, "Tank " + self.cylinders[i + 1])
128 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % ( 105 self.scad["cylinder"] += "tank(%f, %f, %f);\n" % (
129 posx * self.scale3d, r_3 * self.scale3d, 106 posx * SCALE3D, r_3 * SCALE3D,
130 CYLINDER[self.cylinders[i + 1]][1] * self.scale3d) 107 CYLINDER[self.cylinders[i + 1]][1] * SCALE3D)
131 posx += offset(r_2, r_3) 108 posx += offset(r_2, r_3)
132 109
133 self.spacings.append([sx1, sx2]) 110 self.spacings.append([sx1, sx2])
134 111
135 # last bottle spacer pipe 112 # last bottle spacer pipe
136 self._circle(posx, r_2, label, 0.5) 113 self._circle(posx, r_2, label, 0.5)
137 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % ( 114 self.scad["spacer"] += "spacer(%f, %f, %f, %f);\n" % (
138 posx * self.scale3d, r_2 * self.scale3d, r_3 * self.scale3d, 115 posx * SCALE3D, r_2 * SCALE3D, r_3 * SCALE3D,
139 CYLINDER[self.cylinders[-1]][1] * self.scale3d) 116 CYLINDER[self.cylinders[-1]][1] * SCALE3D)
140 return int(posx + r_2 + self.margin) 117 return int(posx + r_2 + self.margin)
141 118
142 def centertext(self, draw, posx, posy, txt, size): 119 def centertext(self, draw, posx, posy, txt, size):
143 """ 120 """
144 Centers text at position horizontally and vertically 121 Centers text at position horizontally and vertically
187 type=int, default=10, \ 164 type=int, default=10, \
188 help='minimum space between cylinders (mm)') 165 help='minimum space between cylinders (mm)')
189 parser.add_argument('--scad', dest='scad', \ 166 parser.add_argument('--scad', dest='scad', \
190 type=str, default="", metavar='filename', \ 167 type=str, default="", metavar='filename', \
191 help='Write OpenSCAD script file') 168 help='Write OpenSCAD script file')
169 parser.add_argument('--precompile', dest='precompile', \
170 default=False, action='store_true', \
171 help='Precompile STL objects for 3D View')
192 172
193 options = parser.parse_args() 173 options = parser.parse_args()
174
175 if options.precompile:
176 precompile_all_stl()
194 177
195 for test in options.cylinders: 178 for test in options.cylinders:
196 if not test in CYLINDER.keys(): 179 if not test in CYLINDER.keys():
197 print "Cylinder type '%s' is unknown" % test 180 print "Cylinder type '%s' is unknown" % test
198 sys.exit(1) 181 sys.exit(1)

mercurial