-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kresling.py
881 lines (714 loc) · 41.2 KB
/
Kresling.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
'''
Kris Dorsey, Sonia Roberts, and Ash Wu
K.Dorsey@Northeastern.edu
Kristen@Kristendorsey.com
This script is run from Autodesk Fusion 360 scripts and add ons interface
The sizing and equations come from Kaufman and Li 2021
Mapping from Kaufman to variables here
Assumes bottom rotation angle is 0
alpha -> top_rotation_angle
R -> radius
P -> edge_length
L1 -> height
N -> number_polygon_edges
phi -> half_inner_angle
'''
# adsk are libraries for Python Fusion API
import adsk.core, adsk.fusion, adsk.cam, traceback
import math
########### BEGIN USER MODIFIED PARAMETERS ############
#Kresling dimensions
# all in cm
edge_length = 3
number_polygon_edges = 6
wall_thickness = 0.1
lamb = 0.75
height_compressed = 1
chamber_length = 0 #1.5
#The distance between the original Kresling triangle and the hinge Kresling triangle
hinge_offset = 0.2
#this needs to be recalculated
#Collar dimensions
collar_height = 0.55
collar_ratio = 0.25/0.55
collar_thickness = wall_thickness * 2
gen_collar_holes = True
gen_symmetric_collars = False
#Generate lid if true, otherwise generate Kresling without the lid
keep_lid = True
tube_OD = 0.28
ratio_hinge_to_wall = 0
ratio_base_to_wall = 1
ratio_lip_to_wall = 1
########### END USER MODIFIED PARAMETERS ############
#Global event handlers tracker
handlers = []
#Command destruction event handler
class cmd_destroy_handler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#Terminate script when command is destroyed
adsk.terminate()
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#Command execution (hitting 'OK') event handler
#This is NOT USED due to the usage of the command execution preview event handler
class on_execute_handler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#Set global variables for all inputs
global edge_length, number_polygon_edges, wall_thickness, lamb, chamber_length, hinge_offset
global ratio_hinge_to_wall, ratio_base_to_wall, ratio_lip_to_wall
global collar_height, collar_ratio, collar_thickness, gen_collar_holes, gen_symmetric_collars
global keep_lid, tube_OD
global radius, half_inner_angle, top_rotation_angle, top_rotation_angle_compressed, height
#get inputs
inputs = args.command.commandInputs
#Update variables with new user inputs
edge_length = inputs.itemById('edge_length').value
number_polygon_edges = inputs.itemById('number_polygon_edges').valueOne
wall_thickness = inputs.itemById('wall_thickness').value
lamb = inputs.itemById('lamb').value
chamber_length = inputs.itemById('chamber_length').value
hinge_offset = inputs.itemById('hinge_offset').value
ratio_hinge_to_wall = inputs.itemById('ratio_hinge_to_wall').value
ratio_base_to_wall = inputs.itemById('ratio_base_to_wall').value
ratio_lip_to_wall = inputs.itemById('ratio_lip_to_wall').value
collar_height = inputs.itemById('collar_height').value
collar_ratio = inputs.itemById('collar_ratio').value
collar_thickness = inputs.itemById('collar_thickness').value
gen_collar_holes = inputs.itemById('gen_collar_holes').value
gen_symmetric_collars = inputs.itemById('gen_symmetric_collars').value
keep_lid = inputs.itemById('keep_lid').value
tube_OD = inputs.itemById('tube_OD').value
#calculate Kresling dimensions from input parameters
radius = edge_length * (2 * math.sin( math.pi / number_polygon_edges ))
half_inner_angle = math.pi / number_polygon_edges
top_rotation_angle_compressed = 2*lamb*(math.pi/2 - half_inner_angle)
top_rotation_angle = 2*(1-lamb)*(math.pi/2 - half_inner_angle)
height = math.sqrt(height_compressed**2 + 2*radius**2*(math.cos(top_rotation_angle + 2*half_inner_angle) - math.cos(top_rotation_angle_compressed + 2*half_inner_angle)))
#Calculate hinge and base thicknesses from ratios
hinge_thickness = wall_thickness * ratio_hinge_to_wall
base_thickness = wall_thickness * ratio_base_to_wall
lip_thickness = wall_thickness * ratio_lip_to_wall
# Make Kresling structure
Kresling = make_Kresling_body(loftFeats, radius, wall_thickness, hinge_thickness, number_polygon_edges, height, top_rotation_angle, base_thickness, lip_thickness, collar_height)
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#User input change event handler
#CURRENTLY UNUSED
class cmd_input_changed_handler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#Watch inputs
adsk.core.InputChangedEventArgs.cast(args)
event_args = adsk.core.InputChangedEventArgs.cast(args)
inputs = event_args.inputs
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#Command execution preview handler
class execute_preview_handler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
try:
#get inputs
event_args = adsk.core.CommandEventArgs.cast(args)
inputs = event_args.command.commandInputs
#Track status of inputs
inputs_valid = True
#Check validity of value inputs
for index in range(inputs.count):
input = inputs.item(index)
if (input.classType() == adsk.core.ValueCommandInput.classType()):
inputs_valid &= input.isValidExpression
#Preview ONLY if inputs are valid
if inputs_valid:
#Set global variables for all inputs
global edge_length, number_polygon_edges, wall_thickness, lamb, chamber_length, hinge_offset
global ratio_hinge_to_wall, ratio_base_to_wall, ratio_lip_to_wall
global collar_height, collar_ratio, collar_thickness, gen_collar_holes, gen_symmetric_collars
global keep_lid, tube_OD
global radius, half_inner_angle, top_rotation_angle, top_rotation_angle_compressed, height
#Update variables with new user inputs
edge_length = inputs.itemById('edge_length').value
number_polygon_edges = inputs.itemById('number_polygon_edges').valueOne
wall_thickness = inputs.itemById('wall_thickness').value
lamb = inputs.itemById('lamb').value
chamber_length = inputs.itemById('chamber_length').value
hinge_offset = inputs.itemById('hinge_offset').value
ratio_hinge_to_wall = inputs.itemById('ratio_hinge_to_wall').value
ratio_base_to_wall = inputs.itemById('ratio_base_to_wall').value
ratio_lip_to_wall = inputs.itemById('ratio_lip_to_wall').value
collar_height = inputs.itemById('collar_height').value
collar_ratio = inputs.itemById('collar_ratio').value
collar_thickness = inputs.itemById('collar_thickness').value
gen_collar_holes = inputs.itemById('gen_collar_holes').value
gen_symmetric_collars = inputs.itemById('gen_symmetric_collars').value
keep_lid = inputs.itemById('keep_lid').value
tube_OD = inputs.itemById('tube_OD').value
#calculate Kresling dimensions from input parameters
radius = edge_length * (2 * math.sin( math.pi / number_polygon_edges ))
half_inner_angle = math.pi / number_polygon_edges
top_rotation_angle_compressed = 2*lamb*(math.pi/2 - half_inner_angle)
top_rotation_angle = 2*(1-lamb)*(math.pi/2 - half_inner_angle)
height = math.sqrt(height_compressed**2 + 2*radius**2*(math.cos(top_rotation_angle + 2*half_inner_angle) - math.cos(top_rotation_angle_compressed + 2*half_inner_angle)))
#Calculate hinge and base thicknesses from ratios
hinge_thickness = wall_thickness * ratio_hinge_to_wall
base_thickness = wall_thickness * ratio_base_to_wall
lip_thickness = wall_thickness * ratio_lip_to_wall
#Make Kresling structure
Kresling = make_Kresling_body(loftFeats, radius, wall_thickness, hinge_thickness, number_polygon_edges, height, top_rotation_angle, base_thickness, lip_thickness, collar_height)
#Do not use normal execute command
event_args.isValidResult = True
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#Command creation event handler
class cmd_creation_handler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#get created command
cmd = adsk.core.Command.cast(args.command)
#setup command destruction event handler
on_destroy = cmd_destroy_handler()
cmd.destroy.add(on_destroy)
handlers.append(on_destroy)
#setup command input change event handler
on_input_changed = cmd_input_changed_handler()
cmd.inputChanged.add(on_input_changed)
handlers.append(on_input_changed)
#setup command execution preview event handler
on_execute_preview = execute_preview_handler()
cmd.executePreview.add(on_execute_preview)
handlers.append(on_execute_preview)
#setup command execution event handler
on_execute = on_execute_handler()
cmd.execute.add(on_execute)
handlers.append(on_execute)
#get command inputs collection
inputs = cmd.commandInputs
#create tab for Kresling dimensions
tab_input1 = inputs.addTabCommandInput('tab_1', 'Kresling Dimensions')
tab_child_inputs1 = tab_input1.children
#track inputs
positive_inputs = []
pos_zero_inputs = []
ratio_inputs = []
collar_ratio_inputs = []
#create inputs for Kresling dimensions
positive_inputs.append(tab_child_inputs1.addValueInput('edge_length', 'Edge Length', 'cm', adsk.core.ValueInput.createByReal(edge_length)))
tab_child_inputs1.addIntegerSliderCommandInput('number_polygon_edges', 'Polygon Edge Count', 3, 18)
tab_child_inputs1.itemById('number_polygon_edges').expressionOne = str(number_polygon_edges)
positive_inputs.append(tab_child_inputs1.addValueInput('wall_thickness', 'Wall Thickness', 'cm', adsk.core.ValueInput.createByReal(wall_thickness)))
positive_inputs.append(tab_child_inputs1.addValueInput('lamb', 'Lambda', '', adsk.core.ValueInput.createByReal(lamb)))
pos_zero_inputs.append(tab_child_inputs1.addValueInput('chamber_length', 'Length of Inner Chambers', 'cm', adsk.core.ValueInput.createByReal(chamber_length)))
pos_zero_inputs.append(tab_child_inputs1.addValueInput('hinge_offset', 'Hinge Offset', 'cm', adsk.core.ValueInput.createByReal(hinge_offset)))
ratio_inputs.append(tab_child_inputs1.addValueInput('ratio_hinge_to_wall', 'Hinge to Wall Ratio', '', adsk.core.ValueInput.createByReal(ratio_hinge_to_wall)))
ratio_inputs.append(tab_child_inputs1.addValueInput('ratio_base_to_wall', 'Base to Wall Ratio', '', adsk.core.ValueInput.createByReal(ratio_base_to_wall)))
ratio_inputs.append(tab_child_inputs1.addValueInput('ratio_lip_to_wall', 'Lip to Wall Ratio', '', adsk.core.ValueInput.createByReal(ratio_lip_to_wall)))
#create tab for additional settings
tab_input2 = inputs.addTabCommandInput('tab_2', 'Additional Settings')
tab_child_inputs2 = tab_input2.children
#create group inputs to group additional settings
group_input1 = tab_child_inputs2.addGroupCommandInput('collar', 'Collar Settings')
group_input1.isExpanded = True
group_child_inputs1 = group_input1.children
group_input2 = tab_child_inputs2.addGroupCommandInput('lid', 'Lid Settings')
group_input2.isExpanded = True
group_child_inputs2 = group_input2.children
#Create inputs for collar settings
pos_zero_inputs.append(group_child_inputs1.addValueInput('collar_height', 'Collar Height', 'cm', adsk.core.ValueInput.createByReal(collar_height)))
collar_ratio_inputs.append(group_child_inputs1.addValueInput('collar_ratio', 'Fractional Size of Collar Holes', '', adsk.core.ValueInput.createByReal(collar_ratio)))
positive_inputs.append(group_child_inputs1.addValueInput('collar_thickness', 'Collar Thickness at Corner', 'cm', adsk.core.ValueInput.createByReal(collar_thickness)))
group_child_inputs1.addBoolValueInput('gen_collar_holes', 'Generate Holes in Collar?', True, '', gen_collar_holes)
group_child_inputs1.addBoolValueInput('gen_symmetric_collars', 'Generate Symmetric Collars?', True, '', gen_symmetric_collars)
#Create inputs for lid settings
group_child_inputs2.addBoolValueInput('keep_lid', 'Keep Lid?', True, '', keep_lid)
pos_zero_inputs.append(group_child_inputs2.addValueInput('tube_OD', 'Tube Outer Diameter', 'cm', adsk.core.ValueInput.createByReal(tube_OD)))
#Set limits for input settings
for pos_input in positive_inputs:
pos_input.minimumValue = 0
pos_input.isMinimumInclusive = False
for pos_zero_input in pos_zero_inputs:
pos_zero_input.minimumValue = 0
pos_zero_input.isMinimumInclusive = True
for ratio_input in ratio_inputs:
ratio_input.minimumValue = 0
ratio_input.maximumValue = 1
for collar_ratio_input in collar_ratio_inputs:
collar_ratio_input.minimumValue = 0
collar_ratio_input.maximumValue = 1
collar_ratio_input.isMinimumInclusive = False
collar_ratio_input.isMaximumInclusive = False
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def generate_polygon_points(number_of_kresling_edges, offset_angle, sine_rotation):
polygon_points = \
[math.cos((2 * (k // 2) * math.pi / number_of_kresling_edges) - offset_angle * (k % 2) - sine_rotation) \
for k in range(4)]
return polygon_points
def gen_sketch(points_x, points_y, points_z):
#Generate closed sketch from point list in X, Y, Z
pgon_sketch = sketchObjs.add(rootComp.xYConstructionPlane)
#define a shape two points at a time
pgon_lines = pgon_sketch.sketchCurves.sketchLines
for k in range(len(points_x)):
#Wrap around to 0th point again to enclose the polygon
point0 = adsk.core.Point3D.create(points_x[k], points_y[k], points_z[k])
point1 = adsk.core.Point3D.create(points_x[((k+1) % len(points_x))], points_y[((k+1) % len(points_x))], points_z[((k+1) % len(points_x))])
#draw the shape by adding the line
pgon_sketch.sketchCurves.sketchLines.addByTwoPoints(point0, point1)
profile = pgon_sketch.profiles.item(0)
return profile
def add_loft(loft_features, profile_obj_array):
#Lofts objects from sketch profiles
loft_input = loft_features.createInput(adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
loft0 = loft_input.loftSections
for x in profile_obj_array:
loft0.add(x)
loft_input.isSolid = True
loft_output = loft_features.add(loft_input)
return loft_output
def make_base(x_points, y_points, radius, height, thickness, lofts, body_list):
#Take exterior points and create two drawings at spacing t from each other
base_lower = gen_sketch([i * radius for i in x_points], [i * radius for i in y_points], [height for k in range(len(x_points))])
base_upper = gen_sketch([i * radius for i in x_points], [i * radius for i in y_points], [height + thickness for k in range(len(x_points))])
#Loft between two drawings to make base
base_loft = add_loft(lofts,[base_lower,base_upper])
base_body = base_loft.bodies.item(0)
body_list.append(base_body)
return base_body
def make_collar(x_points, y_points, radius, height, thickness, collar_height, collar_ratio, collar_thickness, gen_collar_holes, lofts, body_list):
#Define the outer points of the quadrilateral of the collar
x_coord = [i * radius for i in x_points]
y_coord = [i * radius for i in y_points]
## LOWER QUADRILATERAL COORDINATES ##
#Define z height
z_coord_bottom = [height for k in range(4)]
#Define the inner points of the lower quadrilateral of the collar
distance_ratio_bottom = (radius - thickness) / radius
x_inner_bottom = [i * distance_ratio_bottom for i in reversed(x_coord)]
y_inner_bottom = [i * distance_ratio_bottom for i in reversed(y_coord)]
#Consolidate coordinates
x_coord_bottom = x_coord + x_inner_bottom
y_coord_bottom = y_coord + y_inner_bottom
## MIDDLE AND UPPER QUADRILATERAL COORDINATES
#Define z height
middle_height = height + (collar_height * collar_ratio)
top_height = height + collar_height
z_coord_middle = [middle_height] * 4
z_coord_top = [top_height] * 4
#Define the inner points of the upper quadrilaterals of the collar
distance_ratio_upper = (radius - collar_thickness) / radius
x_inner_upper = [i * distance_ratio_upper for i in reversed(x_coord)]
y_inner_upper = [i * distance_ratio_upper for i in reversed(y_coord)]
#Consolidate coordinates
x_coord_upper = x_coord + x_inner_upper
y_coord_upper = y_coord + y_inner_upper
#Draw quadrilaterals
bottom_quad = gen_sketch(x_coord_bottom, y_coord_bottom, z_coord_bottom)
middle_quad = gen_sketch(x_coord_upper, y_coord_upper, z_coord_middle)
top_quad = gen_sketch(x_coord_upper, y_coord_upper, z_coord_top)
#Loft quadrilaterials
lower_loft = add_loft(lofts,[bottom_quad, middle_quad])
lower_body = lower_loft.bodies.item(0)
upper_loft = add_loft(lofts,[middle_quad, top_quad])
upper_body = upper_loft.bodies.item(0)
#Combine into one collar body
tools = adsk.core.ObjectCollection.create()
tools.add(upper_body)
combined_collar = combine_bodies(lower_body, tools)
combined_collar_body = combined_collar.bodies.item(0)
body_list.append(combined_collar_body)
#GENERATE HOLES IF NEEDED
if gen_collar_holes:
#Find the point in the middle of the collar
x_coord_hole = sum(x_coord)/len(x_coord)
y_coord_hole = sum(y_coord)/len(y_coord)
z_coord_hole = (height + top_height)/2
hole_radius = (top_height - middle_height)/2
#Find circle plane
hole_plane_sketch = gen_sketch(x_coord * 2, y_coord * 2, [height, height] + [height + collar_height] * 2)
hole_plane = construct_offset_plane(hole_plane_sketch, 0)
# #Sketch circle
hole_sketch = sketchObjs.add(hole_plane)
hole_sketch_circle = hole_sketch.sketchCurves.sketchCircles
hole_coordinates = hole_sketch.modelToSketchSpace(adsk.core.Point3D.create(x_coord_hole, y_coord_hole, z_coord_hole))
cut_hole = hole_sketch_circle.addByCenterRadius(hole_coordinates, hole_radius)
#Extrude circle
cut_input = extrudeFeats.createInput(hole_sketch.profiles.item(0), adsk.fusion.FeatureOperations.CutFeatureOperation)
cut_distance = adsk.core.ValueInput.createByReal(radius*2)
cut_input.setSymmetricExtent(cut_distance, True)
cut_input.participantBodies = body_list
ext = extrudeFeats.add(cut_input)
return combined_collar_body
def createTubing(height, number_polygon_edges, top_rotation_angle, thickness, tube_OD):
#Add holes for tubing
#create offset plane from XY that's equal to the height of the Kresling
top_plane = construct_offset_plane(rootComp.xYConstructionPlane, height)
#sketch circles
cut_circle_sketch = sketchObjs.add(top_plane)
extrude_circle_sketch = sketchObjs.add(top_plane)
cut_circles = cut_circle_sketch.sketchCurves.sketchCircles
extrude_circles = extrude_circle_sketch.sketchCurves.sketchCircles
#center circle
cut_circles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), tube_OD/2)
extrude_circles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), tube_OD/2 + thickness)
#outside circles
circleAngle = 2*math.pi/number_polygon_edges - top_rotation_angle
for circle_count in range(3):
#circle cutouts
circleX = 2*radius/3 * math.cos(circleAngle + circle_count*2*math.pi/3)
circleY = 2*radius/3 * math.sin(circleAngle + circle_count*2*math.pi/3)
cut_circles.addByCenterRadius(adsk.core.Point3D.create(circleX,circleY,0), tube_OD/2)
#circle lips
extrude_circles.addByCenterRadius(adsk.core.Point3D.create(circleX,circleY,0), tube_OD/2 + thickness)
circle_bodies = adsk.core.ObjectCollection.create()
#Extrude lips for tubing
for circle_count in range(4):
ext_input = extrudeFeats.createInput(extrude_circle_sketch.profiles.item(circle_count), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance = adsk.core.ValueInput.createByReal(thickness*2)
ext_input.setDistanceExtent(False, distance)
ext = extrudeFeats.add(ext_input)
ext_body = ext.bodies.item(0)
circle_bodies.add(ext_body)
#Cut circles for tubing
for circle_count in range(4):
cut_input = extrudeFeats.createInput(cut_circle_sketch.profiles.item(circle_count), adsk.fusion.FeatureOperations.CutFeatureOperation)
distance = adsk.core.ValueInput.createByReal(thickness*2)
cut_input.setDistanceExtent(False, distance)
ext = extrudeFeats.add(cut_input)
return circle_bodies
def make_chambers(lofts, outer_radius, inner_radius, chamber_thickness, draw_pts_x, draw_pts_y, draw_pts_z):
chamber_bodies = []
draw_pts_x = draw_pts_x[0:2]
draw_pts_y = draw_pts_y[0:2]
lower_chamber = make_chamber_walls(lofts, outer_radius, inner_radius, chamber_thickness, draw_pts_x, draw_pts_y, draw_pts_z[0:3], chamber_bodies)
draw_pts_x.reverse()
draw_pts_y.reverse()
upper_chamber = make_chamber_walls(lofts, outer_radius, inner_radius, chamber_thickness, draw_pts_x, draw_pts_y, draw_pts_z[1:4], chamber_bodies)
return chamber_bodies
def make_chamber_walls(lofts, outer_radius, inner_radius, chamber_thickness, draw_x, draw_y, draw_z, body_list):
if draw_z[1] > draw_z[0]: #if the bottom triangle is being drawn
second_radius = inner_radius
else:
second_radius = outer_radius
points_x = make_chamber_points(outer_radius, second_radius, inner_radius - wall_thickness, draw_x)
points_y = make_chamber_points(outer_radius, second_radius, inner_radius - wall_thickness, draw_y)
chamber_triangle = param_Kresling(1, points_x, points_y, draw_z)
chamber_parent = chamber_triangle.parentSketch
front_chamber = create_hinge_extrude(chamber_parent, 0, chamber_thickness, 0)
back_chamber = create_hinge_extrude(chamber_parent, 0, chamber_thickness, 1)
body_list.append(front_chamber)
body_list.append(back_chamber)
return body_list
def make_chamber_points(outer_radius, second_radius, inner_radius, draw_pts):
#Take in chamber parameters and output the points to either side of the corner to loft between
points_for_chamber = \
[outer_radius * draw_pts[0], \
second_radius * draw_pts[1], \
inner_radius * draw_pts[0]]
return points_for_chamber
def param_Kresling(radius, points_x, points_y, points_z):
#Multiply all points by radius
points_x_parameterized = [i * radius for i in points_x]
points_y_parameterized = [i * radius for i in points_y]
#Make the Kresling triangle drawing
Kresling_profile = gen_sketch(points_x_parameterized, points_y_parameterized, points_z)
return Kresling_profile
def cut_combine(target_body, tool_body, keep_body):
#Cut the tool body out of the target body
tools = adsk.core.ObjectCollection.create()
tools.add(tool_body)
combine_input: adsk.fusion.CombineFeatureInput = combineFeats.createInput(target_body, tools)
#Keep or discard tool body
combine_input.isKeepToolBodies = keep_body
combine_input.operation = adsk.fusion.FeatureOperations.CutFeatureOperation
return combineFeats.add(combine_input)
def combine_bodies(target_body, tool_body_list):
#target_body is a single body, tool_body_list is an Object Collection
combine_input = combineFeats.createInput(target_body, tool_body_list)
combine_input.operation = adsk.fusion.FeatureOperations.JoinFeatureOperation
combined_bodies_feat = combineFeats.add(combine_input)
return combined_bodies_feat
def mirror_bodies(mirror_plane, mirror_bodies):
#tool_body_list is an Object Collection
mirror_input = mirrorFeats.createInput(mirror_bodies, mirror_plane)
mirrored_body = mirrorFeats.add(mirror_input)
return mirrored_body
def construct_offset_plane(plane_profile, offset_distance):
#create planes input
plane_input = cPlaneObjs.createInput()
#set offset distance
offset_value = adsk.core.ValueInput.createByReal(offset_distance)
plane_input.setByOffset(plane_profile, offset_value)
#create the offset construction plane
offset_plane = cPlaneObjs.add(plane_input)
return offset_plane
def project_sketch(input_sketch,working_plane):
#Make new sketch on the working plane
projected_sketch = sketchObjs.add(working_plane)
#Project curves of input sketch onto the working plane
for curve in input_sketch.sketchCurves:
projected_sketch.project(curve)
return projected_sketch
def offset_sketch_inside(input_sketch, offset_distance):
#Find all geometry to be offset
offset_geometry = adsk.core.ObjectCollection.create()
input_lines = input_sketch.sketchCurves.sketchLines
offset_geometry.clear()
[offset_geometry.add(line) for line in input_lines]
#Keep track of original sketches
original_sketch_count = input_sketch.sketchCurves.sketchLines.count
#Define offset direction by picking a point in the center of the geometry (centroid)
#Average the coordinate locations of all the sketch points in the geometry
x_tot = 0
y_tot = 0
z_tot = 0
for point in input_sketch.sketchPoints:
x_tot += point.geometry.x
y_tot += point.geometry.y
z_tot += point.geometry.z
total_points = input_sketch.sketchPoints.count
x_dir = x_tot / total_points
y_dir = y_tot / total_points
z_dir = z_tot / total_points
offset_dir = adsk.core.Point3D.create(x_dir,y_dir,z_dir)
#Make original lines construction lines
for i in range(original_sketch_count):
input_sketch.sketchCurves.sketchLines.item(i).isConstruction = True
#Offset sketch inwards (smaller than original sketch) by specified distance
input_sketch.offset(offset_geometry, offset_dir, offset_distance)
return
def create_hinge_extrude(original_sketch, offset_from_original, hinge_loft_thickness, upper_or_lower):
#Get profile of original Kresling triangle sketch
original_profile = original_sketch.profiles.item(0)
#Create construction plane by offsetting the original profile by the hinge thickness
if upper_or_lower == 0: #0 is lower
hinge_plane_offset = hinge_loft_thickness
else:
hinge_plane_offset = hinge_loft_thickness * (-1) #if a hinge is being generated on an upper plane, the offset must be reversed
hinge_plane = construct_offset_plane(original_profile, hinge_plane_offset)
#Project the original sketch onto the construction plane in a new sketch
hinge_sketch = project_sketch(original_sketch, hinge_plane)
#Offset the projection on the new sketch by however much the hinge triangle is smaller than the original
if offset_from_original != 0:
offset_sketch_inside(hinge_sketch, offset_from_original)
#Loft the original triangle to the hinge triangle
hinge_profile = hinge_sketch.profiles.item(0)
hinge_loft = add_loft(loftFeats,[original_profile, hinge_profile])
return hinge_loft
def circular_pattern(input_bodies, pattern_num):
#Pattern around the y-axis
z_axis = rootComp.zConstructionAxis
#Define input
circle_pattern_input = circlePatternFeats.createInput(input_bodies, z_axis)
# Create patternNum of copies
circle_pattern_input.quantity = adsk.core.ValueInput.createByReal(pattern_num)
# Pattern symmetrically across 360 degrees
circle_pattern_input.totalAngle = adsk.core.ValueInput.createByString('360 deg')
circle_pattern_input.isSymmetric = True
# Create the circular pattern
circle_pat_feat = circlePatternFeats.add(circle_pattern_input)
return circle_pat_feat
def rotate_around_z(input_bodies, input_angle):
#input_bodies is an Object Collection of bodies
move_input = moveFeats.createInput2(input_bodies)
#Rotate around z-axis by top rotation angle
real_angle = adsk.core.ValueInput.createByReal(input_angle)
move_input.defineAsRotate(rootComp.zConstructionAxis, real_angle)
return moveFeats.add(move_input)
def make_Kresling_body(lofts, radius, wall_thickness, hinge_thickness, number_polygon_edges, height, top_rotation_angle, base_thickness, lip_thickness, collar_height):
try:
#Create each Kresling triangle according to specified dimensions
body_list = [] #Make an empty body list to append new bodies to
#Create point lists for Kresling triangle points
tri_points_x = generate_polygon_points(number_polygon_edges, top_rotation_angle, 0)
tri_points_y = generate_polygon_points(number_polygon_edges, top_rotation_angle, math.pi/2)
tri_points_z = [0, height, 0, height]
#Draw upper and lower Kresling triangles from point lists
circular_pattern_bodies = adsk.core.ObjectCollection.create() #create collection to pattern
combined_kresling_bodies = adsk.core.ObjectCollection.create() #create collection to combine Kresling parts
for lower_count in range(2):
#draw Kresling polygons for bottom of module, then top of module
draw_points_x = tri_points_x[lower_count : lower_count + 3]
draw_points_y = tri_points_y[lower_count : lower_count + 3]
draw_points_z = tri_points_z[lower_count : lower_count + 3]
outer_kresling = param_Kresling(radius - hinge_thickness, draw_points_x, draw_points_y, draw_points_z)
inner_kresling = param_Kresling(radius - wall_thickness, draw_points_x, draw_points_y, draw_points_z)
if hinge_thickness > 0:
hinge_kresling_sketch = outer_kresling.parentSketch
hinge_loft = create_hinge_extrude(hinge_kresling_sketch, hinge_offset, hinge_thickness, lower_count)
#Add hinge bodies to list
hinge_bodies = hinge_loft.bodies.item(0)
body_list.append(hinge_bodies)
circular_pattern_bodies.add(hinge_bodies)
if hinge_thickness < wall_thickness:
#Loft between interior and exterior Kresling faces, create bodies from loft features
outer_loft = add_loft(lofts,[outer_kresling, inner_kresling])
outer_bodies = outer_loft.bodies.item(0)
body_list.append(outer_bodies)
circular_pattern_bodies.add(outer_bodies)
if chamber_length > 0:
outer_center_kresling = param_Kresling((radius - chamber_length), draw_points_x, draw_points_y, draw_points_z)
inner_center_kresling = param_Kresling((radius - chamber_length) - hinge_thickness, draw_points_x, draw_points_y, draw_points_z)
#Generate center Kresling walls
center_loft = add_loft(lofts,[inner_center_kresling, outer_center_kresling])
center_bodies = center_loft.bodies.item(0)
body_list.append(center_bodies)
circular_pattern_bodies.add(center_bodies)
#### Base, lip, and collar body generation
base_points_x = tri_points_x[0::2]
base_points_y = tri_points_y[0::2]
base_points_x.append(0)
base_points_y.append(0)
if lower_count == 0 and base_thickness > 0 and gen_symmetric_collars == False:
base_points_x = tri_points_x[0::2]
base_points_y = tri_points_y[0::2]
base_points_x.append(0)
base_points_y.append(0)
base_body = make_base(base_points_x, base_points_y, radius, 0, -1 * wall_thickness, lofts, body_list)
circular_pattern_bodies.add(base_body)
if collar_height <= 0:
#Generate lid at the Kresling height if there is no collar
lid_height = height
elif lower_count == 0:
#Make collar if collar height > 0
collar_points_x = tri_points_x[1::2]
collar_points_y = tri_points_y[1::2]
collar_body = make_collar(collar_points_x, collar_points_y, radius, height, wall_thickness, collar_height, collar_ratio, collar_thickness, gen_collar_holes, lofts, body_list)
circular_pattern_bodies.add(collar_body)
#Make mirrored collar
if lower_count == 0 and gen_symmetric_collars:
#Mirror plane
collar_mirror_plane = construct_offset_plane(rootComp.xYConstructionPlane, height/2)
#Mirror collar body
collar_mirror_bodies = adsk.core.ObjectCollection.create()
collar_mirror_bodies.add(collar_body)
#Rotate mirrored collar body by top rotation angle
mirrored_collar_body = mirror_bodies(collar_mirror_plane, collar_mirror_bodies).bodies.item(0)
rotate_collar_bodies = adsk.core.ObjectCollection.create()
rotate_collar_bodies.add(mirrored_collar_body)
rotated_collar = rotate_around_z(rotate_collar_bodies, top_rotation_angle)
#Circular pattern the collar body
circular_pattern_bodies.add(mirrored_collar_body)
#Generate lid above the collar
lid_height = height + collar_height
if lower_count == 1 and lip_thickness > 0:
base_points_x = tri_points_x[1::2]
base_points_y = tri_points_y[1::2]
base_points_x.append(0)
base_points_y.append(0)
target_lip = make_base(base_points_x, base_points_y, radius, lid_height, wall_thickness, lofts, body_list)
tool_lip = make_base(base_points_x, base_points_y, radius - wall_thickness, lid_height, wall_thickness, lofts, body_list)
#Cut top out of the Kresling to make a lip
cut_combine(target_lip, tool_lip, keep_lid)
circular_pattern_bodies.add(target_lip)
#Make mirrored lip for mirrored collar
if gen_symmetric_collars:
lip_mirror_bodies = adsk.core.ObjectCollection.create()
lip_mirror_bodies.add(target_lip)
mirrored_lip_body = mirror_bodies(collar_mirror_plane, lip_mirror_bodies).bodies.item(0)
#Rotate mirrored lip by top rotation angle
rotate_lip_bodies = adsk.core.ObjectCollection.create()
rotate_lip_bodies.add(mirrored_lip_body)
rotated_lip = rotate_around_z(rotate_lip_bodies, top_rotation_angle)
#Circular pattern the lip body
circular_pattern_bodies.add(mirrored_lip_body)
#Make lid
if keep_lid:
circular_pattern_lid = adsk.core.ObjectCollection.create() #create collection to pattern lid
circular_pattern_lid.add(tool_lip)
#Circular pattern lid and combine the pieces
patterned_lid = circular_pattern(circular_pattern_lid, number_polygon_edges)
patterned_lid_bodies = adsk.core.ObjectCollection.create()
for item_count in range(patterned_lid.bodies.count - 1): #ignore the original patterned body
patterned_lid_bodies.add(patterned_lid.bodies.item(item_count))
combined_lid = combine_bodies(tool_lip, patterned_lid_bodies)
combined_lid_body = combined_lid.bodies.item(0)
#Make mirrored lid for mirrored collar
if gen_symmetric_collars:
lid_mirror_bodies = adsk.core.ObjectCollection.create()
lid_mirror_bodies.add(combined_lid_body)
mirrored_lid_body = mirror_bodies(collar_mirror_plane, lid_mirror_bodies).bodies.item(0)
#Rotate mirrored lid by top rotation angle
rotate_lid_bodies = adsk.core.ObjectCollection.create()
rotate_lid_bodies.add(mirrored_lid_body)
rotated_lid = rotate_around_z(rotate_lid_bodies, top_rotation_angle)
rotated_lid_body = rotated_lid.bodies.item(0)
#Add lid to combine list
combined_kresling_bodies.add(rotated_lid_body)
#Cut tubing
if tube_OD > 0:
tubing_bodies = createTubing(lid_height, number_polygon_edges, top_rotation_angle, wall_thickness, tube_OD)
#Combine all lid bodies into one
combined_tube_lid = combine_bodies(combined_lid.bodies.item(0), tubing_bodies)
body_list.append(combined_tube_lid.bodies.item(0))
else:
body_list.append(combined_lid.bodies.item(0))
#Circular pattern all bodies by the number of Kresling polygon edges
patterned_kresling = circular_pattern(circular_pattern_bodies, number_polygon_edges)
#Combine Kresling
for item_count in range(patterned_kresling.bodies.count - 1): #ignore the first body
combined_kresling_bodies.add(patterned_kresling.bodies.item(item_count+1))
tool_kresling_body = patterned_kresling.bodies.item(0)
combined_kresling = combine_bodies(tool_kresling_body, combined_kresling_bodies)
combined_kresling_body = combined_kresling.bodies.item(0)
#Rename Kresling body according to key variables
kresling_name = '|' + str(edge_length) + '-EL_' + str(number_polygon_edges) + '-PE_' + str(wall_thickness) + '-WT_' + str(lamb) + '-LA_' + str(height_compressed) + '-HC|' + str(chamber_length) + '-CL|' + str(collar_height) + '-CH_' + str(gen_symmetric_collars) + '-SC|'
combined_kresling_body.name = kresling_name
body_list.append(combined_kresling_body)
if chamber_length > 0:
circular_chamber_bodies = adsk.core.ObjectCollection.create() #create collection to pattern
chamber_bodies = make_chambers(lofts, radius - wall_thickness, radius - chamber_length, wall_thickness, tri_points_x, tri_points_y, tri_points_z)
for chamberBody in chamber_bodies:
circular_chamber_bodies.add(chamberBody.bodies.item(0))
circular_chambers = circular_pattern(circular_chamber_bodies, 3)
body_list.append(circular_chambers)
return body_list
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
##### Main code #####
try:
# Create Fusion document
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = app.activeProduct
# Set up import functions
importManager = app.importManager
# Create sketch, construction plane, loft objects, and combine objects
rootComp = design.rootComponent
sketchObjs = rootComp.sketches
cPlaneObjs = rootComp.constructionPlanes
loftFeats = rootComp.features.loftFeatures
combineFeats = rootComp.features.combineFeatures
circlePatternFeats = rootComp.features.circularPatternFeatures
extrudeFeats = rootComp.features.extrudeFeatures
mirrorFeats = rootComp.features.mirrorFeatures
moveFeats = rootComp.features.moveFeatures
### Get input parameters from user and generate Kresling ###
# Create new command definition (delete old)
cmd_def = ui.commandDefinitions.itemById('input_parameters')
if cmd_def:
cmd_def.deleteMe()
cmd_def = ui.commandDefinitions.addButtonDefinition('input_parameters', 'Kresling Input Parameters', 'Input parameters to generate Kresling.')
# Connect to command created event
on_command_created = cmd_creation_handler()
cmd_def.commandCreated.add(on_command_created)
handlers.append(on_command_created)
# Execute command definition
cmd_def.execute()
# Prevent module terminimation and wait for event firing
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))