/
conj_slfmaker.py
573 lines (443 loc) · 21.1 KB
/
conj_slfmaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/usr/bin/python
from math import sin, cos, pi, sqrt, atan, acos
from slfmaker import *
class ConjugateSLFMaker:
def __init__(self, teethCount, ts=5, period=2, holedistance=1.0, depth=0.1, tolerance = 0.001):
self.depth = depth
self.teethLoc = []
self.gapLoc = []
self.holedistance = holedistance
self.periodfactor = period
self.conjugateTeethLoc = []
self.teethCount = teethCount
self.cpitch = self.perimeter() / self.teethCount
self.toothSlices = ts
self.secondOffset = 1.0
print "est. circular pitch:", self.cpitch, "est. perimeter: ", self.perimeter()
self.tolerance = tolerance
def write(self, filename):
if not self.teethLoc:
self.calcPoints()
if not self.conjugateTeethLoc:
self.calcConjugatePoints()
f = open(filename, 'w')
self.preamble(f)
self.amble(f)
self.postamble(f)
f.close()
def amble(self, f):
# print self.teethLoc
# print self.conjugateTeethLoc
self.doShape(f, self.teethLoc, "a", "sBlu")
self.doShape(f, self.conjugateTeethLoc, "b", "sRed")
def doShape(self, f, teethLoc, gearLetter, color):
"""Takes the pointlist and produces the necessary dxf code for
the points and the line they're in"""
import string
#f.write("### GEAR "+gearLetter+" GEOMETRY ###\n")
#f.write("# points\n")
inc = 0.01
teethCount = len(teethLoc)
print "teeth completed: ",
n = 0
teeth_ends = []
inner_pts = []
for d in teethLoc:
rc = abs(d['rc'])
inverseInvoluteTableY = []
inverseInvoluteTableX = []
t = 0.0
while t < pi / 2.0:
i = involute(rc, t)
inverseInvoluteTableX.append((i[0],t))
inverseInvoluteTableY.append((i[1],t))
t += inc
# t parameter at pitch circle
td = findClosest(inverseInvoluteTableY,self.dedendumd)
# t parameter at top of tooth
tt = findClosest(inverseInvoluteTableY,self.dedendumd+self.adendumd)
# clip the involute sides to get rid of overlap
# new radius, and corresponding x and y
r = d['r']
x = r * cos(d['t'])
y = r * sin(d['t'])
dx = d['dx']
dy = d['dy']
# dx2 = d['dx2']
# dy2 = d['dy2']
dx, dy = normalize(dx,dy)
# tooth width = circular pitch / 2.0 - backlash
# offset = tooth width / 2.0 + x(td)
toothWidth = self.cpitch / 2.0
offset = (toothWidth / 2.0) + involute(rc, td)[0]
# make sure the involute won't intersect its symmetric partner
tx = findClosestDown(inverseInvoluteTableX, offset)
if (tt > tx): tt = tx
if (dy < 0):
rot = 3.0*pi/2.0+atan(dx/dy)
else:
rot = pi/2.0+atan(dx/dy)
t = 0.0
m = 0
#f.write("# tooth #"+`n`+"\n")
pts = []
while m < self.toothSlices:
ix, iy = involute(rc, t)
px = -offset+ix
py = iy
npx = px * cos(rot) + py * sin(rot)
npy = px * -sin(rot) + py * cos(rot)
arr = [(x + npx, y + npy,)]
# left side (front)
#f.write("point "+gearLetter+"fpt"+`n`+"_l"+`m`+" ("+ \
# `x+npx`+" " + \
# `y+npy`+" " + \
# `self.depth`+") endpoint\n")
# (back)
#f.write("point "+gearLetter+"bpt"+`n`+"_l"+`m`+" ("+ \
# `x+npx`+" " + \
# `y+npy`+" " + \
# "0.0) endpoint\n")
# right side
px = offset-ix # flip left-hand side
py = iy
npx = px * cos(rot) + py * sin(rot)
npy = px * -sin(rot) + py * cos(rot)
arr.append((x + npx, y + npy,))
pts.append(arr)
# (front)
#f.write("point "+gearLetter+"fpt"+`n`+"_r"+`m`+" ("+ \
# `x+npx`+" " + \
# `y+npy`+" " + \
# `self.depth`+") endpoint\n")
# (back)
#f.write("point "+gearLetter+"bpt"+`n`+"_r"+`m`+" ("+ \
# `x+npx`+" " + \
# `y+npy`+" " + \
# "0.0) endpoint\n")
t += tt / (self.toothSlices-1.0)
m += 1
for i in range(0, len(pts)):
f.write(" 0\nLINE\n")
f.write(" 8\n %s \n" % ('gear')) #layername
if(i == 0):
#for the first one, we don't have much of a line
f.write(" 10\n%f\n" % pts[i][0][0])
f.write(" 20\n%f\n" % pts[i][0][1])
else:
f.write(" 10\n%f\n" % pts[i-1][0][0])
f.write(" 20\n%f\n" % pts[i-1][0][1])
f.write(" 11\n%f\n" % pts[i][0][0])
f.write(" 21\n%f\n" % pts[i][0][1])
f.write(" 0\nLINE\n")
f.write(" 8\n %s \n" % ('gear'))
if(i == 0):
f.write(" 10\n%f\n" % pts[i][1][0])
f.write(" 20\n%f\n" % pts[i][1][1])
else:
f.write(" 10\n%f\n" % pts[i-1][1][0])
f.write(" 20\n%f\n" % pts[i-1][1][1])
f.write(" 11\n%f\n" % pts[i][1][0])
f.write(" 21\n%f\n" % pts[i][1][1])
#store the first two points to use to connect the teeth
teeth_ends.append((pts[0][0][0], pts[0][0][1], pts[0][1][0], pts[0][1][1],))
#connect the last two points of the tip of the spur
f.write(" 0\nLINE\n")
f.write(" 8\n %s \n" % ('gear'))
f.write(" 10\n%f\n" % pts[-1][0][0])
f.write(" 20\n%f\n" % pts[-1][0][1])
f.write(" 11\n%f\n" % pts[-1][1][0])
f.write(" 21\n%f\n" % pts[-1][1][1])
# inner point on inner axle
ri = self.innerradius(d['t'])
xi, yi = toCartesian(ri, d['t'])
inner_pts.append((xi,yi,))
# front
#f.write("point "+gearLetter+"fpi"+`n`+" ("+`xi`+" "+`yi`+" "+`self.depth`+" ) endpoint\n")
# back
#f.write("point "+gearLetter+"bpi"+`n`+" ("+`xi`+" "+`yi`+" 0.0 ) endpoint\n")
#n = n + 1
#n = 0
#for d in teethLoc:
# tooth face (front)
#f.write("face "+gearLetter+"fft"+`n`+" (")
#for z in range(self.toothSlices):
# f.write(" "+gearLetter+"fpt"+`n`+"_r"+`z`+" ")
#for z in range(self.toothSlices-1,-1,-1):
# f.write(" "+gearLetter+"fpt"+`n`+"_l"+`z`+" ")
#f.write(") endface\n")
# (back)
#f.write("face "+gearLetter+"bft"+`n`+" (")
#for z in range(self.toothSlices):
# f.write(" "+gearLetter+"bpt"+`n`+"_l"+`z`+" ")
#for z in range(self.toothSlices-1,-1,-1):
# f.write(" "+gearLetter+"bpt"+`n`+"_r"+`z`+" ")
#f.write(") endface\n")
# tooth sides
#for z in range(self.toothSlices-1):
# f.write("face "+gearLetter+"soft"+`n`+"_l"+`z`+" ( "+gearLetter+"bpt"+`n`+"_l"+`z`+" "+gearLetter+"fpt"+`n`+"_l"+`z`+" "+gearLetter+"fpt"+`n`+"_l"+`(z+1)`+" "+gearLetter+"bpt"+`n`+"_l"+`(z+1)`+") endface\n")
# f.write("face "+gearLetter+"soft"+`n`+"_r"+`z`+" ( "+gearLetter+"bpt"+`n`+"_r"+`z+1`+" "+gearLetter+"fpt"+`n`+"_r"+`z+1`+" "+gearLetter+"fpt"+`n`+"_r"+`z`+" "+gearLetter+"bpt"+`n`+"_r"+`z`+") endface\n")
# tooth side (end)
#f.write("face "+gearLetter+"soft"+`n`+"_c ( "+gearLetter+"bpt"+`n`+"_l"+`self.toothSlices-1`+" "+gearLetter+"fpt"+`n`+"_l"+`self.toothSlices-1`+" "+gearLetter+"fpt"+`n`+"_r"+`self.toothSlices-1`+" "+gearLetter+"bpt"+`n`+"_r"+`self.toothSlices-1`+") endface\n")
# in between teeth (gap), on the outside
#f.write("face "+gearLetter+"sofg"+`n`+" ( "+gearLetter+"fpt"+`(n+1)%teethCount`+"_r0 "+gearLetter+"fpt"+`n`+"_l0 "+gearLetter+"bpt"+`n`+"_l0 "+gearLetter+"bpt"+`(n+1)%teethCount`+"_r0) endface\n")
# inner radius faces
#f.write("face "+gearLetter+"sif"+`n`+" ("+gearLetter+"bpi"+`n`+" "+gearLetter+"fpi"+`n`+" "+gearLetter+"fpi"+`(n+1)%teethCount`+" "+gearLetter+"bpi"+`(n+1)%teethCount`+") endface\n")
# gap to inner radius face
# front
#f.write("face "+gearLetter+"ffbi"+`n`+" ( "+gearLetter+"fpi"+`(n+1)%teethCount`+" "+gearLetter+"fpi"+`n`+" "+gearLetter+"fpt"+`n`+"_l0 "+gearLetter+"fpt"+`(n+1)%teethCount`+"_r0) endface\n")
# back
#f.write("face "+gearLetter+"fbbi"+`n`+" ( "+gearLetter+"bpi"+`(n+1)%teethCount`+" "+gearLetter+"bpt"+`(n+1)%teethCount`+"_r0 "+gearLetter+"bpt"+`n`+"_l0 "+gearLetter+"bpi"+`n`+" ) endface\n")
# tooth to inner radius face
# front
#f.write("face "+gearLetter+"ffbt"+`n`+" ( "+gearLetter+"fpi"+`n`+" "+gearLetter+"fpt"+`n`+"_r0 "+gearLetter+"fpt"+`n`+"_l0) endface\n")
# back
#f.write("face "+gearLetter+"fbbt"+`n`+" ( "+gearLetter+"bpi"+`n`+" "+gearLetter+"bpt"+`n`+"_l0 "+gearLetter+"bpt"+`n`+"_r0) endface\n")
print n,
n += 1
#TODO this is the same process repeated twice, put it in a method and test it
#connect the inner radius
for i in range(0, len(inner_pts)):
f.write(" 0\nLINE\n")
f.write(" 8\n %s \n" % ('gear')) #layername
if(i == 0):
f.write(" 10\n%f\n" % inner_pts[-1][0])
f.write(" 20\n%f\n" % inner_pts[-1][1])
else:
f.write(" 10\n%f\n" % inner_pts[i-1][0])
f.write(" 20\n%f\n" % inner_pts[i-1][1])
f.write(" 11\n%f\n" % inner_pts[i][0])
f.write(" 21\n%f\n" % inner_pts[i][1])
#connect the teeth
for i in range(0, len(teeth_ends)):
f.write(" 0\nLINE\n")
f.write(" 8\n %s \n" % ('gear')) #layername
if(i == 0):
f.write(" 10\n%f\n" % teeth_ends[-1][0])
f.write(" 20\n%f\n" % teeth_ends[-1][1])
else:
f.write(" 10\n%f\n" % teeth_ends[i-1][0])
f.write(" 20\n%f\n" % teeth_ends[i-1][1])
f.write(" 11\n%f\n" % teeth_ends[i][2])
f.write(" 21\n%f\n" % teeth_ends[i][3])
#f.write("# object\n")
#print "\nassembling polygons into gear"+gearLetter+" object"
#f.write("object gear"+gearLetter+" (")
#for n in range(len(teethLoc)):
# f.write(" "+gearLetter+"fft"+`n`+" "+gearLetter+"ffbi"+`n`+" "+gearLetter+"ffbt"+`n`+" "+gearLetter+"bft"+`n`+" "+gearLetter+"fbbi"+`n`+" "+gearLetter+"fbbt"+`n`+
# " "+gearLetter+"sif"+`n`+" "+gearLetter+"soft"+`n`+"_c "+gearLetter+"sofg"+`n`+" ")
# for z in range(self.toothSlices-1):
# f.write(" "+gearLetter+"soft"+`n`+"_l"+`z`+" "+gearLetter+"soft"+`n`+"_r"+`z`+" ")
#f.write(")\n")
#f.write(" surface "+color+"\n")
#f.write("endobject\n")
#f.write("### END GEAR "+gearLetter+" GEOMETRY ###\n")
def preamble(self,f):
f.write(" 999\nDXF created from gearsgen.py phill baker\n")
f.write(" 0\nSECTION\n")
f.write(" 2\nENTITIES\n")
def postamble(self,f):
f.write(" 0\nENDSEC\n")
f.write(" 0\nEOF\n")
def radiusOfCurvature(self, t):
dx = self.dx(t)
dy = self.dy(t)
dx2 = self.dx2(t)
dy2 = self.dy2(t)
return ((dx*dx+dy*dy)**1.5) / (dx*dy2-dx2*dy)
def calcPoints(self):
refinement = 1
halfteethCount = self.teethCount * 2
halfpitch = self.cpitch / 2.0
while 1:
module = halfpitch * 2.0 / pi
self.dedendumd = module * 1.25
self.adendumd = module
ccd = 0.0
theta = 0.0
ro = self.outerradius(0) - self.dedendumd
fx = lx = ro * cos(0)
fy = ly = ro * sin(0)
actualTeeth = 1
while theta < 2.0 * pi:
theta += self.tolerance
ro = self.outerradius(theta) - self.dedendumd
x = ro * cos(theta)
y = ro * sin(theta)
# add to distance from last point
# by adding cartesian distance
ccd += sqrt((x - lx)**2 + (y-ly)**2)
if (ccd >= halfpitch and actualTeeth < halfteethCount):
# add the new set of points
actualTeeth += 1
ccd = 0.0
lx = x
ly = y
print "refinement", refinement, ",", "half-teeth =", actualTeeth, "extra = ", ccd, "c pitch = ", halfpitch * 2.0
#if pastvalues.has_key((actualTeeth, ccd)):
if halfteethCount == actualTeeth and \
(abs(ccd - halfpitch) < self.tolerance * 5):
break
# pastvalues[(actualTeeth, ccd)] = self.cpitch
refinement += 1
halfpitch = (halfpitch*actualTeeth + ccd) / (halfteethCount + 1)
self.cpitch = halfpitch * 2.0
print "new circular pitch =",self.cpitch
print "new perimeter =",self.cpitch*self.teethCount
module = self.cpitch / pi
self.dedendumd = module * 1.25
self.adendumd = module
print "dedendum distance =", self.dedendumd
print "adendum distance =", self.adendumd
print "tooth height =", self.dedendumd+self.adendumd
# compute final refinement and store data
ccd = 0.0
theta = 0.0
ro = self.outerradius(theta) - self.dedendumd
lx = ro * cos(theta)
ly = ro * sin(theta)
rc = self.radiusOfCurvature(theta)
self.teethLoc = [{'x':lx, 'y':ly, 'r':ro, 't':0.0, 'rc':rc, 'dx' : self.dx(0.0), 'dy' : self.dy(0.0), 'dx2' : self.dx2(0.0), 'dy2' : self.dy2(0.0)}]
self.gapLoc = []
gapFlag = 1
print "computing points for final refinement"
actualTeeth = 1
while actualTeeth < halfteethCount:
theta += self.tolerance
ro = self.outerradius(theta) - self.dedendumd
x = ro * cos(theta)
y = ro * sin(theta)
# add to distance from last point
# by adding cartesian distance
ccd = sqrt((x - lx)**2 + (y-ly)**2)
if (ccd >= halfpitch):
# add the new set of points
rc = self.radiusOfCurvature(theta)
if gapFlag:
self.gapLoc.append({'x':x, 'y':y, 'r':ro, 't':theta, 'rc':rc, 'dx' : self.dx(theta), 'dy' : self.dy(theta), 'dx2' : self.dx2(theta), 'dy2' : self.dy2(theta)})
else:
self.teethLoc.append({'x':x, 'y':y, 'r':ro, 't':theta, 'rc':rc, 'dx' : self.dx(theta), 'dy' : self.dy(theta), 'dx2' : self.dx2(theta), 'dy2' : self.dy2(theta)})
gapFlag = not gapFlag
lx = x
ly = y
actualTeeth += 1
def calcConjugatePoints(self):
self.secondOffset = 1.2*self.holedistance / 2.0
print "calculating conjugate points"
while 1:
p = self.gapLoc[0]
r = self.holedistance - (p['r']+2.0*self.dedendumd)
t = pi - p['t']
x,y = toCartesian(r,t)
dx = -sin(t)
dy = cos(t)
self.conjugateTeethLoc = [{'x':x,
'y':y,
'r':r,
't':t,
'rc':p['rc'],
'dx':dx,
'dy':dy,
'dx2':p['dx2'],
'dy2':p['dy2']}]
n = 1
while (n < len(self.gapLoc)*self.periodfactor+1):
p = self.gapLoc[n % self.teethCount]
rp1 = self.holedistance - (p['r']+2.0*self.dedendumd)
rp2 = self.conjugateTeethLoc[n-1]['r']
r1 = self.gapLoc[n % self.teethCount]['r']
r2 = self.gapLoc[(n-1) % self.teethCount]['r']
dt = self.gapLoc[n % self.teethCount]['t'] - \
self.gapLoc[(n-1) % self.teethCount]['t']
dtp = acos((r1**2.0+r2**2.0-2.0*r1*r2*cos(dt)-rp1**2.0-rp2**2.0) / \
(-2.0*rp1*rp2))
t = self.conjugateTeethLoc[n-1]['t'] + dtp
if (t < 0): t += 2.0*pi
if (t > 2.0*pi): t -= 2.0*pi
x = cos(t) * rp1
y = sin(t) * rp1
dx = -sin(t)
dy = cos(t)
self.conjugateTeethLoc.append({'x':x,
'y':y,
'r':rp1,
't':t,
'rc':p['rc'],
'dx':dx,
'dy':dy,
'dx2':p['dx2'],
'dy2':p['dy2']})
n = n + 1
if (n < self.teethCount * self.periodfactor+1):
self.holedistance += self.tolerance
elif (n > self.teethCount * self.periodfactor+1):
self.holedistance -= self.tolerance
else: # must be equal
gap = sqrt( \
(self.conjugateTeethLoc[-1]['x']-\
self.conjugateTeethLoc[0]['x'])**2 + \
(self.conjugateTeethLoc[-1]['y']-\
self.conjugateTeethLoc[0]['y'])**2)
if (gap >= self.tolerance * 10):
if (t >= 2.0 * pi):
self.holedistance += self.tolerance
else:
self.holedistance -= self.tolerance
else:
self.conjugateTeethLoc = self.conjugateTeethLoc[:-1]
break
print "using hole distance of "+`self.holedistance`
for n in range(0,len(self.conjugateTeethLoc)):
before = n - 1
after = n + 1
if before < 0: before += len(self.conjugateTeethLoc)
if after >= len(self.conjugateTeethLoc): after -= len(self.conjugateTeethLoc)
dt = self.conjugateTeethLoc[after]['t'] - \
self.conjugateTeethLoc[before]['t']
if (dt < 0): dt += 2.0 * pi
dx = self.conjugateTeethLoc[after]['x'] - \
self.conjugateTeethLoc[before]['x']
dy = self.conjugateTeethLoc[after]['y'] - \
self.conjugateTeethLoc[before]['y']
self.conjugateTeethLoc[n]['dx'] = dx
self.conjugateTeethLoc[n]['dy'] = dy
def teethCmp(x,y):
if (x['t'] == y['t']): return 0
elif (x['t'] < y['t']): return -1
else: return 1
if (__name__ == "__main__"):
s = SLFMaker()
print "### ConjugateSLFMake self-test"
class ConjugateSLFMakerSphereTeeth(ConjugateSLFMaker):
def __init__(self, teethCount, depth=0.1, tolerance = 0.001):
ConjugateSLFMaker.__init__(self, teethCount, 0, depth, tolerance)
def doShape(self, f, points, gearLetter, color):
import string
#f.write("### GEAR GEOMETRY ###\n")
#f.write("### BASE GEOMETRY ###\n")
#f.write("### "+`len(self.teethLoc)`+" actual slices\n")
#f.write("# points\n")
n = 0
#for d in points:
# f.write("sphere "+gearLetter+"t"+`n`+"\n")
# f.write(" radius "+`self.cpitch/4.0`+"\n") #"+`d['rc']/5.0`+"\n")
# f.write(" radius "+`d['rc']/10.0`+"\n")
# f.write("endsphere\n")
# n += 1
#n = 0
#f.write("group gear"+gearLetter+"\n")
#for d in points:
# f.write("instance "+gearLetter+"t"+`n`+"\n")
# r = d['r'] # + self.dedendumd
# x = r * cos(d['t'])
# y = r * sin(d['t'])
# f.write("translate ("+`x`+" " + `y` + " 0)\n")
# f.write("endinstance\n")
# n += 1
#f.write("endgroup\n")
#f.write("# faces\n")
#f.write("# object\n")
#f.write("### END BASE GEOMETRY ###\n")
#f.write("### TEETH GEOMETRY ###\n")
#f.write("### END TEETH GEOMETRY ###\n")
#f.write("### END GEAR GEOMETRY ###\n")