-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHelicalGearPlus.py
executable file
·1664 lines (1414 loc) · 76.1 KB
/
HelicalGearPlus.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Author Nico Schlueter 2020
#
# Released under the MIT license. See License.txt for full license information.
#
# Description-Generates straight, helical and herringbone external, internal and rack gears
# as well as non-enveloping worms and worm gears
#
# Parts (mostly helical gear calculatiom) was taken from Ross Korsky's Helical gear generator
# Parts (mostly some of the Involute code) was taken from AutoDesks' Fusion 360 SpurGear sample script.
# The primary source used to produce this add-in was http://qtcgears.com/tools/catalogs/PDF_Q420/Tech.pdf
import adsk.core, adsk.fusion, traceback
import math
# Global set of event _handlers to keep them referenced for the duration of the command
_handlers = []
# Caches last gear for
lastGear = None
lastInput = ""
COMMANDID = "helicalGearPlus"
COMMANDNAME = "Helical Gear+"
COMMANDTOOLTIP = "Generates Helical Gears"
TOOLBARPANELS = ["SolidCreatePanel"]
# Initial persistence Dict
pers = {
'DDType': "External Gear",
'DDStandard': "Normal",
'VIHelixAngle': 0.5235987755982988,
'VIPressureAngle': 0.3490658503988659,
'VIModule': 0.3,
'ISTeeth': 16,
'VIBacklash': 0.0,
'VIWidth': 1.0,
'VIHeight': 0.8,
'VILength': 10.0,
'VIDiameter': 8.0,
'BVHerringbone': False,
'BVPreview': False,
'VIAddendum': 1,
'VIDedendum': 1.25}
class Involute:
def __init__(self, gear):
self.gear = gear
def draw(self, sketch, zShift=0, rotation=0, involutePointCount=10):
# Calculate points along the involute curve.
originPoint = adsk.core.Point3D.create(0, 0, zShift)
involutePoints = []
keyPoints = []
if self.gear.baseDiameter >= self.gear.rootDiameter:
involuteFromRad = self.gear.baseDiameter / 2.0
else:
involuteFromRad = self.gear.rootDiameter / 2
radiusStep = (self.gear.outsideDiameter / 2 - involuteFromRad) / (involutePointCount - 1)
involuteIntersectionRadius = involuteFromRad
for i in range(0, involutePointCount):
newPoint = self.InvolutePoint(self.gear.baseDiameter / 2.0, involuteIntersectionRadius, zShift)
involutePoints.append(newPoint)
involuteIntersectionRadius = involuteIntersectionRadius + radiusStep
# Determine the angle between the X axis and a line between the origin of the curve
# and the intersection point between the involute and the pitch diameter circle.
pitchInvolutePoint = self.InvolutePoint(self.gear.baseDiameter / 2.0, self.gear.pitchDiameter / 2.0,
zShift)
pitchPointAngle = math.atan2(pitchInvolutePoint.y, pitchInvolutePoint.x)
# Rotate the involute so the intersection point lies on the x axis.
rotateAngle = -((self.gear.toothArcAngle / 4) + pitchPointAngle - (self.gear.backlashAngle / 4))
cosAngle = math.cos(rotateAngle)
sinAngle = math.sin(rotateAngle)
for i in range(0, involutePointCount):
x = involutePoints[i].x
y = involutePoints[i].y
involutePoints[i].x = x * cosAngle - y * sinAngle
involutePoints[i].y = x * sinAngle + y * cosAngle
# Create a new set of points with a negated y. This effectively mirrors the original
# points about the X axis.
involute2Points = []
for i in range(0, involutePointCount):
involute2Points.append(adsk.core.Point3D.create(involutePoints[i].x, -involutePoints[i].y, zShift))
# Rotate involute
if rotation:
cosAngle = math.cos(rotation)
sinAngle = math.sin(rotation)
for i in range(0, involutePointCount):
x = involutePoints[i].x
y = involutePoints[i].y
involutePoints[i].x = x * cosAngle - y * sinAngle
involutePoints[i].y = x * sinAngle + y * cosAngle
x = involute2Points[i].x
y = involute2Points[i].y
involute2Points[i].x = x * cosAngle - y * sinAngle
involute2Points[i].y = x * sinAngle + y * cosAngle
curve1Angle = math.atan2(involutePoints[0].y, involutePoints[0].x)
curve2Angle = math.atan2(involute2Points[0].y, involute2Points[0].x)
if curve2Angle < curve1Angle:
curve2Angle += math.pi * 2
# Create and load an object collection with the points.
# Add the involute points for the second spline to an ObjectCollection.
pointSet1 = adsk.core.ObjectCollection.create()
pointSet2 = adsk.core.ObjectCollection.create()
for i in range(0, involutePointCount):
pointSet1.add(involutePoints[i])
pointSet2.add(involute2Points[i])
midIndex = int(pointSet1.count / 2)
keyPoints.append(pointSet1.item(0))
keyPoints.append(pointSet2.item(0))
keyPoints.append(pointSet1.item(midIndex))
keyPoints.append(pointSet2.item(midIndex))
# Create splines.
spline1 = sketch.sketchCurves.sketchFittedSplines.add(pointSet1)
spline2 = sketch.sketchCurves.sketchFittedSplines.add(pointSet2)
oc = adsk.core.ObjectCollection.create()
oc.add(spline2)
(_, _, crossPoints) = spline1.intersections(oc)
assert len(crossPoints) == 0 or len(crossPoints) == 1, 'Failed to compute a valid involute profile!'
if len(crossPoints) == 1:
# involute splines cross, clip the tooth
# clip = spline1.endSketchPoint.geometry.copy()
# spline1 = spline1.trim(spline2.endSketchPoint.geometry).item(0)
# spline2 = spline2.trim(clip).item(0)
keyPoints.append(crossPoints[0])
else:
# Draw the tip of the tooth - connect the splines
if self.gear.toothCount >= 100:
sketch.sketchCurves.sketchLines.addByTwoPoints(spline1.endSketchPoint, spline2.endSketchPoint)
keyPoints.append(spline1.endSketchPoint.geometry)
keyPoints.append(spline2.endSketchPoint.geometry)
else:
tipCurve1Angle = math.atan2(involutePoints[-1].y, involutePoints[-1].x)
tipCurve2Angle = math.atan2(involute2Points[-1].y, involute2Points[-1].x)
if tipCurve2Angle < tipCurve1Angle:
tipCurve2Angle += math.pi * 2
tipRad = originPoint.distanceTo(involutePoints[-1])
tipArc = sketch.sketchCurves.sketchArcs.addByCenterStartSweep(
originPoint,
adsk.core.Point3D.create(math.cos(tipCurve1Angle) * tipRad,
math.sin(tipCurve1Angle) * tipRad,
zShift),
tipCurve2Angle - tipCurve1Angle)
keyPoints.append(tipArc.startSketchPoint.geometry)
keyPoints.append(adsk.core.Point3D.create(tipRad, 0, zShift))
keyPoints.append(tipArc.endSketchPoint.geometry)
# Draw root circle
# rootCircle = sketch.sketchCurves.sketchCircles.addByCenterRadius(originPoint, self.gear.rootDiameter/2)
rootArc = sketch.sketchCurves.sketchArcs.addByCenterStartSweep(
originPoint,
adsk.core.Point3D.create(math.cos(curve1Angle) * (self.gear.rootDiameter / 2 - 0.01),
math.sin(curve1Angle) * (self.gear.rootDiameter / 2 - 0.01),
zShift),
curve2Angle - curve1Angle)
# if the offset tooth profile crosses the offset circle then trim it, else connect the offset tooth to the circle
oc = adsk.core.ObjectCollection.create()
oc.add(spline1)
if True:
if rootArc.intersections(oc)[1].count > 0:
spline1 = spline1.trim(originPoint).item(0)
spline2 = spline2.trim(originPoint).item(0)
rootArc.trim(rootArc.startSketchPoint.geometry)
rootArc.trim(rootArc.endSketchPoint.geometry)
else:
sketch.sketchCurves.sketchLines.addByTwoPoints(originPoint, spline1.startSketchPoint).trim(
originPoint)
sketch.sketchCurves.sketchLines.addByTwoPoints(originPoint, spline2.startSketchPoint).trim(
originPoint)
else:
if rootArc.intersections(oc)[1].count > 0:
spline1 = spline1.trim(originPoint).item(0)
spline2 = spline2.trim(originPoint).item(0)
rootArc.deleteMe()
sketch.sketchCurves.sketchLines.addByTwoPoints(originPoint, spline1.startSketchPoint)
sketch.sketchCurves.sketchLines.addByTwoPoints(originPoint, spline2.startSketchPoint)
# Calculate points along an involute curve.
def InvolutePoint(self, baseCircleRadius, distFromCenterToInvolutePoint, zShift):
l = math.sqrt(
distFromCenterToInvolutePoint * distFromCenterToInvolutePoint - baseCircleRadius * baseCircleRadius)
alpha = l / baseCircleRadius
theta = alpha - math.acos(baseCircleRadius / distFromCenterToInvolutePoint)
x = distFromCenterToInvolutePoint * math.cos(theta)
y = distFromCenterToInvolutePoint * math.sin(theta)
return adsk.core.Point3D.create(x, y, zShift)
class HelicalGear:
def __init__(self):
pass
@property
def isUndercutRequried(self):
return self.virtualTeeth < self.critcalVirtualToothCount
@property
def backlashAngle(self):
"""The backlash is split between both sides of this and (an assumed) mateing gear - each side of a tooth will be narrowed by 1/4 this value."""
return 2 * self.backlash / self.pitchDiameter if self.pitchDiameter > 0 else 0
@property
def toothArcAngle(self):
"""Arc angle of a single tooth."""
return 2 * math.pi / self.toothCount if self.toothCount > 0 else 0
@property
def tipPressureAngle(self):
"""Pressure angle at the tip of the tooth."""
return math.acos(self.baseDiameter / self.outsideDiameter)
@property
def involuteA(self):
"""Involute at nominal pressure angle."""
return math.tan(self.pressureAngle) - self.pressureAngle
@property
def involuteAa(self):
"""Involute at tip pressure angle."""
return math.tan(self.tipPressureAngle) - self.tipPressureAngle
@property
def profileShiftCoefficient(self):
"""Profile shift coefficient without undercut."""
return 1 - (self.toothCount / 2) * math.pow(math.sin(self.pressureAngle), 2)
@property
def topLandAngle(self):
"""Top land is the (sometimes flat) surface of the top of a gear tooth.
DOES NOT APPEAR TO PRODUCE THE CORRECT VALUE."""
return (math.pi / (2 * self.toothCount)) + (
(2 * self.profileShiftCoefficient * math.tan(self.pressureAngle)) / self.toothCount) + (
self.involuteA - self.involuteAa)
@property
def topLandThickness(self):
"""Top land is the (sometimes flat) surface of the top of a gear tooth.
DOES NOT APPEAR TO PRODUCE THE CORRECT VALUE."""
return math.radians(self.topLandAngle) * self.outsideDiameter
@property
def critcalVirtualToothCount(self):
q = math.pow(math.sin(self.normalPressureAngle), 2)
return 2 / q if q != 0 else float('inf')
@property
def isInvalid(self):
if (self.width <= 0):
return "Width too low"
if (math.radians(-90) > self.helixAngle):
return "Helix angle too low"
if (math.radians(90) < self.helixAngle):
return "Helix angle too high"
if (self.module <= 0):
return "Module to low"
if (self.addendum <= 0):
return "Addendum too low"
if (self.wholeDepth <= 0):
return "Dedendum too low"
if (self.pressureAngle < 0):
return "Pressure angle too low"
if (self.pressureAngle > math.radians(80)):
return "Pressure angle too high"
if (self.normalPressureAngle < 0):
return "Pressure angle too low"
if (self.normalPressureAngle > math.radians(80)):
return "Pressure angle too high"
if (self.toothCount <= 0):
return "Too few teeth"
if (abs(self.backlashAngle) / 4 >= self.toothArcAngle / 8):
return "Backlash too high"
if (self.internalOutsideDiameter):
if (self.internalOutsideDiameter <= self.outsideDiameter):
return "Outside diameter too low"
if (self.circularPitch <= 0):
return "Invalid: circularPitch"
if (self.baseDiameter <= 0):
return "Invalid Gear"
if (self.pitchDiameter <= 0):
return "Invalid Gear"
if (self.rootDiameter <= 0.03):
return "Invalid Gear"
if (self.outsideDiameter <= 0):
return "Invalid Gear"
return False
@property
def verticalLoopSeperation(self):
return math.tan(math.radians(90) + self.helixAngle) * self.pitchDiameter * math.pi
# returns the number of turns for a given distance
def tFor(self, displacement):
return displacement / (math.tan(math.radians(90) + self.helixAngle) * (self.pitchDiameter / 2))
def __str__(self):
str = ''
str += '\n'
str += 'root diameter..............: {0:.3f} mm\n'.format(self.rootDiameter * 10)
str += 'base diameter.............: {0:.3f} mm\n'.format(self.baseDiameter * 10)
str += 'pitch diameter............: {0:.3f} mm\n'.format(self.pitchDiameter * 10)
str += 'outside diameter.........: {0:.3f} mm\n'.format(self.outsideDiameter * 10)
str += '\n'
str += 'module.......................: {0:.3f} mm\n'.format(self.module * 10)
str += 'normal module...........: {0:.3f} mm\n'.format(self.normalModule * 10)
str += 'pressure angle............: {0:.3f} deg\n'.format(math.degrees(self.pressureAngle))
str += 'normal pressure angle: {0:.3f} deg\n'.format(math.degrees(self.normalPressureAngle))
str += '\n'
if (self.helixAngle != 0):
str += 'length per revolution..: {0:.3f} mm\n'.format(abs(self.verticalLoopSeperation) * 10)
str += '\n'
return str
@staticmethod
def createInNormalSystem(toothCount, normalModule, normalPressureAngle, helixAngle, backlash=0, addendum=1,
dedendum=1.25, width=1, herringbone=False, internalOutsideDiameter=None):
toothCount = toothCount if toothCount > 0 else 1
# normalModule = normalModule if normalModule > 0 else 1e-10
# normalPressureAngle = normalPressureAngle if 0 <= normalPressureAngle < math.radians(90) else 0
# helixAngle = helixAngle if math.radians(-90) < helixAngle < math.radians(90) else 0
gear = HelicalGear()
gear.backlash = backlash
gear.helixAngle = helixAngle
gear.toothCount = toothCount
gear.width = width
gear.herringbone = herringbone
gear.internalOutsideDiameter = internalOutsideDiameter
gear.normalModule = normalModule
gear.normalPressureAngle = normalPressureAngle
gear.normalCircularPitch = gear.normalModule * math.pi
cosHelixAngle = math.cos(helixAngle)
gear.virtualTeeth = gear.toothCount / math.pow(cosHelixAngle, 3)
# Radial / Transverse figures
gear.module = gear.normalModule / cosHelixAngle
gear.pressureAngle = math.atan2(math.tan(gear.normalPressureAngle), cosHelixAngle)
gear.pitchDiameter = gear.module * gear.toothCount
gear.baseDiameter = gear.pitchDiameter * math.cos(gear.pressureAngle)
gear.addendum = addendum * gear.normalModule
gear.wholeDepth = (addendum + dedendum) * gear.normalModule
gear.outsideDiameter = gear.pitchDiameter + 2 * gear.addendum
gear.rootDiameter = gear.outsideDiameter - 2 * gear.wholeDepth
gear.circularPitch = gear.module * math.pi
return gear
@staticmethod
def createInRadialSystem(toothCount, radialModule, radialPressureAngle, helixAngle, backlash=0, addendum=1,
dedendum=1.25, width=1, herringbone=False, internalOutsideDiameter=None):
toothCount = toothCount if toothCount > 0 else 1
radialModule = radialModule if radialModule > 0 else 1e-10
radialPressureAngle = radialPressureAngle if 0 <= radialPressureAngle < math.radians(90) else 0
helixAngle = helixAngle if math.radians(-90) < helixAngle < math.radians(90) else 0
gear = HelicalGear()
gear.backlash = backlash
gear.helixAngle = helixAngle
gear.toothCount = toothCount
gear.width = width
gear.herringbone = herringbone
gear.internalOutsideDiameter = internalOutsideDiameter
gear.normalModule = radialModule * math.cos(gear.helixAngle)
gear.normalPressureAngle = math.atan(math.tan(radialPressureAngle) * math.cos(gear.helixAngle))
gear.normalCircularPitch = gear.normalModule * math.pi
cosHelixAngle = math.cos(helixAngle)
gear.virtualTeeth = gear.toothCount / math.pow(cosHelixAngle, 3)
# Radial / Transverse figures
gear.module = radialModule
gear.pressureAngle = radialPressureAngle
gear.pitchDiameter = gear.module * gear.toothCount
gear.baseDiameter = gear.pitchDiameter * math.cos(gear.pressureAngle)
gear.addendum = addendum * gear.normalModule
gear.wholeDepth = (addendum + dedendum) * gear.normalModule
gear.outsideDiameter = gear.pitchDiameter + 2 * gear.addendum
gear.rootDiameter = gear.outsideDiameter - 2 * gear.wholeDepth
gear.circularPitch = gear.module * math.pi
return gear
def modelGear(self, parentComponent, sameAsLast=False):
# Storres a copy of the last gear generated to speed up regeneation of the same gear
global lastGear
# The temporaryBRep manager is a tool for creating 3d geometry without the use of features
# The word temporary referrs to the geometry being created being virtual, but It can easily be converted to actual geometry
tbm = adsk.fusion.TemporaryBRepManager.get()
# Create new component
occurrence = parentComponent.occurrences.addNewComponent(adsk.core.Matrix3D.create())
component = occurrence.component
component.name = 'Helical Gear ({0}{1}@{2:.2f} m={3})'.format(
self.toothCount,
'L' if self.helixAngle < 0 else 'R',
abs(math.degrees(self.helixAngle)),
round(self.normalModule * 10, 4))
# Creates BaseFeature if DesignType is parametric
if (parentComponent.parentDesign.designType):
baseFeature = component.features.baseFeatures.add()
baseFeature.startEdit()
else:
baseFeature = None
if (not (sameAsLast and lastGear)):
# Creates sketch and draws tooth profile
involute = Involute(self)
# Creates profile on z=0 if herringbone and on bottom if not
if (not self.herringbone):
plane = adsk.core.Plane.create(adsk.core.Point3D.create(0, 0, -self.width / 2),
adsk.core.Vector3D.create(0, 0, 1))
# Creates an object responsible for passing all required data to create a construction plane
planeInput = component.constructionPlanes.createInput()
# Sets the plane input by plane
planeInput.setByPlane(plane)
# Adds plain input to construction planes
cPlane = component.constructionPlanes.add(planeInput)
sketch = component.sketches.add(cPlane)
cPlane.deleteMe()
sketch.isComputeDeferred = True
# Draws All Teeth
# TODO: Optimize by copying instead of regenerating
for i in range(self.toothCount):
involute.draw(sketch, 0, (i / self.toothCount) * 2 * math.pi)
# Base Circle
sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0),
self.rootDiameter / 2)
else:
sketch = component.sketches.add(component.xYConstructionPlane)
sketch.isComputeDeferred = True
# Draws All Teeth
# TODO: Optimize by copying instead of regenerating
for i in range(self.toothCount):
involute.draw(sketch, 0, (i / self.toothCount) * 2 * math.pi)
# Base Circle
sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0),
self.rootDiameter / 2)
# Creates path line for sweep feature
if (not self.herringbone):
line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0),
adsk.core.Point3D.create(0, 0, self.width))
else:
line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0),
adsk.core.Point3D.create(0, 0, self.width / 2))
line2 = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0),
adsk.core.Point3D.create(0, 0, -self.width / 2))
# Reactivates sketch computation and puts all profules into an OC
sketch.isComputeDeferred = False
profs = adsk.core.ObjectCollection.create()
for prof in sketch.profiles:
profs.add(prof)
# Creates sweeep features
if (not self.herringbone):
path1 = component.features.createPath(line1)
sweepInput = component.features.sweepFeatures.createInput(profs, path1,
adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
sweepInput.twistAngle = adsk.core.ValueInput.createByReal(-self.tFor(self.width))
if (baseFeature):
sweepInput.targetBaseFeature = baseFeature
gearBody = sweepFeature = component.features.sweepFeatures.add(sweepInput).bodies.item(0)
else:
path1 = component.features.createPath(line1)
sweepInput = component.features.sweepFeatures.createInput(profs, path1,
adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
sweepInput.twistAngle = adsk.core.ValueInput.createByReal(-self.tFor(self.width / 2))
if (baseFeature):
sweepInput.targetBaseFeature = baseFeature
sweepFeature = component.features.sweepFeatures.add(sweepInput)
path2 = component.features.createPath(line2)
sweepInput = component.features.sweepFeatures.createInput(profs, path2,
adsk.fusion.FeatureOperations.JoinFeatureOperation)
sweepInput.twistAngle = adsk.core.ValueInput.createByReal(self.tFor(self.width / 2))
if (baseFeature):
sweepInput.targetBaseFeature = baseFeature
gearBody = sweepFeature = component.features.sweepFeatures.add(sweepInput).bodies.item(0)
# "Inverts" internal Gears
if (self.internalOutsideDiameter):
cyl = cylinder = tbm.createCylinderOrCone(adsk.core.Point3D.create(0, 0, -self.width / 2),
self.internalOutsideDiameter / 2,
adsk.core.Point3D.create(0, 0, self.width / 2),
self.internalOutsideDiameter / 2)
tbm.booleanOperation(cyl, tbm.copy(gearBody), 0)
# Deletes external gear
gearBody.deleteMe()
if (baseFeature):
gearBody = component.bRepBodies.add(cyl, baseFeature)
else:
gearBody = component.bRepBodies.add(cyl)
# Delete tooth sketch for performance
sketch.deleteMe()
# Storres a copy of the newly generated gear
lastGear = tbm.copy(gearBody)
else:
if (baseFeature):
component.bRepBodies.add(lastGear, baseFeature)
else:
component.bRepBodies.add(lastGear)
# Draws pitch diameter
pitchDiameterSketch = component.sketches.add(component.xYConstructionPlane)
pitchDiameterSketch.name = "PD: {0:.3f}mm".format(self.pitchDiameter * 10)
pitchDiameterCircle = pitchDiameterSketch.sketchCurves.sketchCircles.addByCenterRadius(
adsk.core.Point3D.create(0, 0, 0), self.pitchDiameter / 2)
pitchDiameterCircle.isConstruction = True
pitchDiameterCircle.isFixed = True
# Finishes BaseFeature if it exists
if (baseFeature):
baseFeature.finishEdit()
return occurrence
class RackGear:
def __init__(self):
pass
@staticmethod
def createInNormalSystem(normalModule, normalPressureAngle, helixAngle, herringbone, length, width, height,
backlash=0, addendum=1, dedendum=1.25):
gear = RackGear()
gear.normalModule = normalModule
gear.normalPressureAngle = normalPressureAngle
gear.helixAngle = helixAngle
gear.herringbone = herringbone
gear.length = length
gear.width = width
gear.height = height
gear.backlash = backlash
gear.addendum = addendum * gear.normalModule
gear.dedendum = dedendum * gear.normalModule
cosHelixAngle = math.cos(helixAngle)
gear.module = gear.normalModule / cosHelixAngle
gear.pressureAngle = math.atan2(math.tan(gear.normalPressureAngle), cosHelixAngle)
return gear
@staticmethod
def createInRadialSystem(radialModule, radialPressureAngle, helixAngle, herringbone, length, width, height,
backlash=0, addendum=1, dedendum=1.25):
gear = RackGear()
gear.module = radialModule
gear.pressureAngle = radialPressureAngle
gear.helixAngle = helixAngle
gear.herringbone = herringbone
gear.length = length
gear.width = width
gear.height = height
gear.backlash = backlash
gear.addendum = addendum * gear.module
gear.dedendum = dedendum * gear.module
cosHelixAngle = math.cos(helixAngle)
gear.normalModule = gear.module * cosHelixAngle
gear.normalPressureAngle = math.atan(math.tan(radialPressureAngle) * math.cos(gear.helixAngle))
return gear
def __str__(self):
str = ''
str += '\n'
str += 'module.......................: {0:.3f} mm\n'.format(self.module * 10)
str += 'normal module...........: {0:.3f} mm\n'.format(self.normalModule * 10)
str += 'pressure angle............: {0:.3f} deg\n'.format(math.degrees(self.pressureAngle))
str += 'normal pressure angle: {0:.3f} deg\n'.format(math.degrees(self.normalPressureAngle))
str += '\n'
return str
@property
def isInvalid(self):
if (self.length <= 0):
return "Length too low"
if (self.width <= 0):
return "Width too low"
if (self.height <= 0):
return "Height too low"
if (self.module <= 0):
return "Module too low"
if (self.addendum < 0):
return "Addendum too low"
if (self.dedendum < 0):
return "Dedendum too low"
if (self.addendum + self.dedendum <= 0):
return "Addendum too low"
if (not (0 < self.pressureAngle < math.radians(90))):
return "Invalid pressure angle"
if (not (math.radians(-90) < self.helixAngle < math.radians(90))):
return "Invalid helix angle"
# Not actually the limit but close enough
if ((-3 * self.normalModule) > self.backlash):
return "Backlash too low"
if (self.backlash > (3 * self.normalModule)):
return "Backlash too high"
return False
def rackLines(self, x, y, z, m, n, height, pAngle, hAngle, backlash, addendum, dedendum):
strech = 1 / math.cos(hAngle)
P = m * math.pi
# Clamps addendum and dedendum
addendum = min(addendum, (-(1 / 4) * (backlash - P) * (1 / math.tan(pAngle))) - 0.0001)
dedendum = min(dedendum, -(1 / 4) * (-backlash - P) * (1 / math.tan(pAngle)) - 0.0001)
dedendum = min(dedendum, height - 0.0001)
lines = []
for i in range(n):
# Root
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(x + ((i * P)) * strech, y, z - dedendum),
adsk.core.Point3D.create(x + ((i * P) + (P / 2) + backlash / 2 - (
math.tan(pAngle) * 2 * dedendum)) * strech, y, z - dedendum))
)
# Left Edge
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(
x + ((i * P) + (P / 2) + backlash / 2 - (math.tan(pAngle) * 2 * dedendum)) * strech, y,
z - dedendum),
adsk.core.Point3D.create(x + ((i * P) + (P / 2) + backlash / 2 - (
math.tan(pAngle) * (dedendum - addendum))) * strech, y,
z + addendum))
)
# Tip
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(
x + ((i * P) + (P / 2) + backlash / 2 - (math.tan(pAngle) * (dedendum - addendum))) * strech, y,
z + addendum),
adsk.core.Point3D.create(x + ((i * P) + P - (
math.tan(pAngle) * (dedendum + addendum))) * strech, y,
z + addendum))
)
# Right Edge
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(
x + ((i * P) + P - (math.tan(pAngle) * (dedendum + addendum))) * strech, y,
z + addendum),
adsk.core.Point3D.create(x + ((i * P) + P) * strech, y, z - dedendum))
)
# Right Edge
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(x + (n * P) * strech, y, z - dedendum),
adsk.core.Point3D.create(x + (n * P) * strech, y, z - height))
)
# Bottom Edge
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(x + (n * P) * strech, y, z - height),
adsk.core.Point3D.create(x, y, z - height))
)
# Left Edge
lines.append(
adsk.core.Line3D.create(adsk.core.Point3D.create(x, y, z - height),
adsk.core.Point3D.create(x, y, z - dedendum))
)
return lines
def modelGear(self, parentComponent, sameAsLast=False):
# Storres a copy of the last gear generated to speed up regeneation of the same gear
global lastGear
# Create new component
occurrence = parentComponent.occurrences.addNewComponent(adsk.core.Matrix3D.create())
component = occurrence.component
component.name = 'Helical Rack ({0}mm {1}@{2:.2f} m={3})'.format(
self.length * 10,
'L' if self.helixAngle < 0 else 'R',
abs(math.degrees(self.helixAngle)),
round(self.normalModule * 10, 4))
if (parentComponent.parentDesign.designType):
baseFeature = component.features.baseFeatures.add()
baseFeature.startEdit()
else:
baseFeature = None
if (not (sameAsLast and lastGear)):
teeth = math.ceil(
(self.length + 2 * math.tan(abs(self.helixAngle)) * self.width) / (self.normalModule * math.pi))
# The temporaryBRep manager is a tool for creating 3d geometry without the use of features
# The word temporary referrs to the geometry being created being virtual, but It can easily be converted to actual geometry
tbm = adsk.fusion.TemporaryBRepManager.get()
# Array to keep track of TempBRepBodies
tempBRepBodies = []
# Creates BRep wire object(s), representing edges in 3D space from an array of 3Dcurves
if (self.herringbone):
wireBody1, _ = tbm.createWireFromCurves(self.rackLines(
-self.length / 2 - (math.tan(abs(self.helixAngle)) + math.tan(self.helixAngle)) * self.width / 2,
-self.width / 2,
0,
self.normalModule, teeth, self.height, self.normalPressureAngle, self.helixAngle,
self.backlash, self.addendum, self.dedendum
))
wireBody2, _ = tbm.createWireFromCurves(self.rackLines(
-self.length / 2 - math.tan(abs(self.helixAngle)) * self.width / 2,
0,
0,
self.normalModule, teeth, self.height, self.normalPressureAngle, self.helixAngle,
self.backlash, self.addendum,
self.dedendum
))
wireBody3, _ = tbm.createWireFromCurves(self.rackLines(
-self.length / 2 - (math.tan(abs(self.helixAngle)) + math.tan(self.helixAngle)) * self.width / 2,
self.width / 2,
0,
self.normalModule, teeth, self.height, self.normalPressureAngle, self.helixAngle,
self.backlash, self.addendum, self.dedendum
))
else:
wireBody1, _ = tbm.createWireFromCurves(self.rackLines(
-self.length / 2 - (math.tan(abs(self.helixAngle)) + math.tan(self.helixAngle)) * self.width,
-self.width / 2,
0,
self.normalModule, teeth, self.height, self.normalPressureAngle, self.helixAngle,
self.backlash, self.addendum, self.dedendum
))
wireBody2, _ = tbm.createWireFromCurves(self.rackLines(
-self.length / 2 - math.tan(abs(self.helixAngle)) * self.width,
self.width / 2,
0,
self.normalModule, teeth, self.height, self.normalPressureAngle, self.helixAngle,
self.backlash, self.addendum,
self.dedendum
))
# Creates the planar end caps.
tempBRepBodies.append(tbm.createFaceFromPlanarWires([wireBody1]))
if (self.herringbone):
tempBRepBodies.append(tbm.createFaceFromPlanarWires([wireBody3]))
else:
tempBRepBodies.append(tbm.createFaceFromPlanarWires([wireBody2]))
# Creates the ruled surface connectiong the two end caps
tempBRepBodies.append(tbm.createRuledSurface(wireBody1.wires.item(0), wireBody2.wires.item(0)))
if (self.herringbone):
tempBRepBodies.append(tbm.createRuledSurface(wireBody2.wires.item(0), wireBody3.wires.item(0)))
# Turns surfaces into real BRep so they can be boundary filled
tools = adsk.core.ObjectCollection.create()
for b in tempBRepBodies:
if (baseFeature):
tools.add(component.bRepBodies.add(b, baseFeature))
else:
tools.add(component.bRepBodies.add(b))
# Boundary fills enclosed voulume
boundaryFillInput = component.features.boundaryFillFeatures.createInput(tools,
adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
if baseFeature:
boundaryFillInput.targetBaseFeature = baseFeature
boundaryFillInput.bRepCells.item(0).isSelected = True
body = component.features.boundaryFillFeatures.add(boundaryFillInput).bodies.item(0)
# Creates a box to cut off angled ends
obb = adsk.core.OrientedBoundingBox3D.create(adsk.core.Point3D.create(0, 0, 0),
adsk.core.Vector3D.create(1, 0, 0),
adsk.core.Vector3D.create(0, 1, 0),
self.length, self.width * 2, (self.height + self.addendum) * 2)
box = tbm.createBox(obb)
tbm.booleanOperation(box, tbm.copy(body), 1)
if (baseFeature):
gearBody = component.bRepBodies.add(box, baseFeature)
else:
gearBody = component.bRepBodies.add(box)
body.deleteMe()
# Deletes tooling bodies
for b in tools:
b.deleteMe()
# Storres a copy of the newly generated gear
lastGear = tbm.copy(gearBody)
else:
if (baseFeature):
component.bRepBodies.add(lastGear, baseFeature)
else:
component.bRepBodies.add(lastGear)
# Adds "pitch diameter" line
pitchDiameterSketch = component.sketches.add(component.xYConstructionPlane)
pitchDiameterSketch.name = "Pitch Diameter Line"
pitchDiameterLine = pitchDiameterSketch.sketchCurves.sketchLines.addByTwoPoints(
adsk.core.Point3D.create(-self.length / 2, 0, 0),
adsk.core.Point3D.create(self.length / 2, 0, 0)
)
pitchDiameterLine.isFixed = True
pitchDiameterLine.isConstruction = True
if (baseFeature):
baseFeature.finishEdit()
return occurrence
# Fires when the CommandDefinition gets executed.
# Responsible for adding commandInputs to the command &
# registering the other command _handlers.
class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
# Registers the CommandExecuteHandler
onExecute = CommandExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
# Registers the CommandExecutePreviewHandler
onExecutePreview = CommandExecutePreviewHandler()
cmd.executePreview.add(onExecutePreview)
_handlers.append(onExecutePreview)
# Registers the CommandInputChangedHandler
onInputChanged = CommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# Registers the CommandDestryHandler
onDestroy = CommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
# Registers the CommandValidateInputsEventHandler
onValidate = CommandValidateInputsEventHandler()
cmd.validateInputs.add(onValidate)
_handlers.append(onValidate)
# Get the CommandInputs collection associated with the command.
inputs = cmd.commandInputs
# Tabs
tabSettings = inputs.addTabCommandInput("TabSettings", "Settings")
tabAdvanced = inputs.addTabCommandInput("TabAdvanced", "Advanced")
tabPosition = inputs.addTabCommandInput("TabPosition", "Position")
tabProperties = inputs.addTabCommandInput("TabProperties", "Info")
# Setting command Inputs
ddType = tabSettings.children.addDropDownCommandInput("DDType", "Type", 0)
ddType.listItems.add("External Gear", pers['DDType'] == "External Gear", "resources/external")
ddType.listItems.add("Internal Gear", pers['DDType'] == "Internal Gear", "resources/internal")
ddType.listItems.add("Rack Gear", pers['DDType'] == "Rack Gear", "resources/rack")
viModule = tabSettings.children.addValueInput("VIModule", "Module", "mm",
adsk.core.ValueInput.createByReal(pers['VIModule']))
viModule.tooltip = "Module"
viModule.tooltipDescription = "The module is the fundamental unit of size for a gear.\nMatching gears must have the same module."
viHelixAngle = tabSettings.children.addValueInput("VIHelixAngle", "Helix Angle", "deg",
adsk.core.ValueInput.createByReal(pers['VIHelixAngle']))
viHelixAngle.tooltip = "Helix Angle"
viHelixAngle.tooltipDescription = "Angle of tooth twist.\n0 degrees produces a standard spur gear.\nHigh angles produce worm gears\nNegative angles produce left handed gears"
viHelixAngle.toolClipFilename = 'resources/captions/HelixAngle.png'
isTeeth = tabSettings.children.addIntegerSpinnerCommandInput("ISTeeth", "Teeth", 1, 99999, 1,
pers['ISTeeth'])
isTeeth.isVisible = pers['DDType'] != "Rack Gear"
isTeeth.tooltip = "Number of Teeth"
isTeeth.tooltipDescription = "The number of teeth a gear has.\nGears with higher helix angle can have less teeth.\nFor example mots worm gears have only one."
viWidth = tabSettings.children.addValueInput("VIWidth", "Gear Width", "mm",
adsk.core.ValueInput.createByReal(pers['VIWidth']))
viWidth.tooltip = "Gear Width"
viWidth.tooltipDescription = "Represenets the width or thickness of a gear"
viHeight = tabSettings.children.addValueInput("VIHeight", "Height", "mm",
adsk.core.ValueInput.createByReal(pers['VIHeight']))
viHeight.tooltip = "Rack Height"
viHeight.tooltipDescription = "Represents the distance from the bottom to the pitch diameter.\nDoes not include Addendum."
viHeight.isVisible = pers['DDType'] == "Rack Gear"
viLength = tabSettings.children.addValueInput("VILength", "Length", "mm",
adsk.core.ValueInput.createByReal(pers['VILength']))
viLength.tooltip = "Rack Length"
viLength.isVisible = pers['DDType'] == "Rack Gear"
viDiameter = tabSettings.children.addValueInput("VIDiameter", "Outside Diameter", "mm",
adsk.core.ValueInput.createByReal(pers['VIDiameter']))
viDiameter.tooltip = "Internal Gear Outside Diameter"
viDiameter.isVisible = pers['DDType'] == "Internal Gear"
bvHerringbone = tabSettings.children.addBoolValueInput("BVHerringbone", "Herringbone", True, "",
pers['BVHerringbone'])
bvHerringbone.toolClipFilename = 'resources/captions/Herringbone.png'
bvHerringbone.tooltip = "Herringbone"
bvHerringbone.tooltipDescription = "Generates gear as herringbone."
bvPreview = tabSettings.children.addBoolValueInput("BVPreview", "Preview", True, "", pers['BVPreview'])
bvPreview.tooltip = "Preview"
bvPreview.tooltipDescription = "Generates a real-time preview of the gear.\nThis makes changes slower as the gear has to re-generate."
tbWarning1 = tabSettings.children.addTextBoxCommandInput("TBWarning1", "", '', 2, True)
# Advanced command inputs
ddStandard = tabAdvanced.children.addDropDownCommandInput("DDStandard", "Standard", 0)
ddStandard.listItems.add("Normal", pers['DDStandard'] == "Normal", "resources/normal")
ddStandard.listItems.add("Radial", pers['DDStandard'] == "Radial", "resources/radial")
ddStandard.toolClipFilename = 'resources/captions/NormalVsRadial.png'
ddStandard.tooltipDescription = "Normal System: Pressure angle and module are defined relative to the normal of the tooth.\n\nRadial System: Pressure angle and module are defined relative to the plane of rotation."
viPressureAngle = tabAdvanced.children.addValueInput("VIPressureAngle", "Pressure Angle", "deg",
adsk.core.ValueInput.createByReal(
pers['VIPressureAngle']))
viPressureAngle.tooltip = "Pressure Angle"
viPressureAngle.tooltipDescription = "Represent the angle of the line of contact.\nStandart values are: 20°, 14.5° "
viBacklash = tabAdvanced.children.addValueInput("VIBacklash", "Backlash", "mm",
adsk.core.ValueInput.createByReal(pers['VIBacklash']))
viBacklash.tooltip = "Backlash"
viBacklash.tooltipDescription = "Represents the distance between two mating teeth at the correct spacing.\nThis value is halved as it should be distributed between both gears."
viAddendum = tabAdvanced.children.addValueInput("VIAddendum", "Addendum", "",
adsk.core.ValueInput.createByReal(pers['VIAddendum']))
viAddendum.tooltip = "Addendum"
viAddendum.tooltipDescription = "Represents the factor that the tooth extends past the pitch diameter."
viDedendum = tabAdvanced.children.addValueInput("VIDedendum", "Dedendum", "",
adsk.core.ValueInput.createByReal(pers['VIDedendum']))
viDedendum.tooltip = "Dedendum"
viDedendum.tooltipDescription = "Represents the factor that the root diameter is below the pitch diameter."
tbWarning2 = tabAdvanced.children.addTextBoxCommandInput("TBWarning2", "", '', 2, True)
# Position
siPlane = tabPosition.children.addSelectionInput("SIPlane", "Plane", "Select Gear Plane")
siPlane.addSelectionFilter("ConstructionPlanes")
siPlane.addSelectionFilter("Profiles")
siPlane.addSelectionFilter("PlanarFaces")
siPlane.setSelectionLimits(0, 1)
siPlane.tooltip = "Gear Plane"
siPlane.tooltipDescription = "Select the plane the gear will be placed on.\n\nValid selections are:\n Sketch Profiles\n Construction Planes\n BRep Faces"
siDirection = tabPosition.children.addSelectionInput("SIDirection", "Line", "Select Rack Direction")
siDirection.addSelectionFilter("ConstructionLines")
siDirection.addSelectionFilter("SketchLines")
siDirection.addSelectionFilter("LinearEdges")
siDirection.setSelectionLimits(0, 1)
siDirection.isVisible = False
siDirection.tooltip = "Rack Path"
siDirection.tooltipDescription = "Select the line the rack is placed on.\nWill be projected onto the plane.\n\nValid selections are:\n Sketch Lines\n Construction Lines\n BRep Edges"
siOrigin = tabPosition.children.addSelectionInput("SIOrigin", "Center", "Select Gear Center")
siOrigin.addSelectionFilter("ConstructionPoints")
siOrigin.addSelectionFilter("SketchPoints")
siOrigin.addSelectionFilter("Vertices")
siOrigin.addSelectionFilter("CircularEdges")
siOrigin.setSelectionLimits(0, 1)
siOrigin.tooltip = "Gear Center Point"
siOrigin.tooltipDescription = "Select the center point of the gear.\nWill be projected onto the plane.\n\nValid selections:\n Sketch Points\n Construction Points\n BRep Vertices\n Circular BRep Edges\n"
bvFlipped = tabPosition.children.addBoolValueInput("BVFlipped", "Flip", True)
bvFlipped.isVisible = False
bvFlipped.tooltip = "Flips rack direction"
ddDirection = tabPosition.children.addDropDownCommandInput("DDDirection", "Direction", 0)
ddDirection.listItems.add("Front", True, "resources/front")
ddDirection.listItems.add("Center", False, "resources/center")
ddDirection.listItems.add("Back", False, "resources/back")
ddDirection.tooltip = "Direction"
ddDirection.tooltipDescription = "Choose what side of the plane the gear is placed on."
avRotation = tabPosition.children.addAngleValueCommandInput("AVRotation", "Rotation",
adsk.core.ValueInput.createByReal(0))
avRotation.isVisible = False
avRotation.tooltip = "Rotation"
avRotation.tooltipDescription = "Rotates the gear around its axis."
dvOffsetX = tabPosition.children.addDistanceValueCommandInput("DVOffsetX", "Offset (X)",
adsk.core.ValueInput.createByReal(0))
dvOffsetX.setManipulator(
adsk.core.Point3D.create(0, 0, 0),
adsk.core.Vector3D.create(1, 0, 0)
)