-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlayerCommand.py
578 lines (417 loc) · 20.5 KB
/
PlayerCommand.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
import adsk.core
import adsk.fusion
import traceback
from .Fusion360Utilities.Fusion360Utilities import AppObjects, get_default_dir
from .Fusion360Utilities.Fusion360CommandBase import Fusion360CommandBase
import json
from collections import defaultdict
display_state_object = {}
def show_all_occurrences():
ao = AppObjects()
for occurrence in ao.root_comp.allOccurrences:
occurrence.isLightBulbOn = True
def hide_all_occurrences():
ao = AppObjects()
for occurrence in ao.root_comp.allOccurrences:
occurrence.isLightBulbOn = True
def show_occurrence(occurrence: adsk.fusion.Occurrence):
occurrence.isLightBulbOn = True
for body in occurrence.bRepBodies:
make_body_visible(body)
def hide_all_bodies():
ao = AppObjects()
for component in ao.design.allComponents:
component.isBodiesFolderLightBulbOn = True
for body in component.bRepBodies:
body.isLightBulbOn = False
def show_sketch(sketch):
sketch.areDimensionsShown = True
sketch.areConstraintsShown = True
sketch.areProfilesShown = True
sketch.isLightBulbOn = True
def show_construction(entity):
entity.component.isConstructionFolderLightBulbOn = True
entity.component.isOriginFolderLightBulbOn = True
entity.isLightBulbOn = True
def show_joint(joint: adsk.fusion.Joint):
joint.parentComponent.isJointsFolderLightBulbOn = True
joint.isLightBulbOn = True
def hide_all_construction():
ao = AppObjects()
for component in ao.design.allComponents:
for item in get_all_construction(component):
item.isLightBulbOn = False
def hide_all_joints():
ao = AppObjects()
for component in ao.design.allComponents:
for item in component.joints:
if item.timelineObject.index < ao.time_line.markerPosition:
item.isLightBulbOn = False
def get_all_construction(component: adsk.fusion.Component):
construction_entities = []
for plane in component.constructionPlanes:
construction_entities.append(plane)
for axis in component.constructionAxes:
construction_entities.append(axis)
for point in component.constructionPoints:
construction_entities.append(point)
construction_entities.append(component.xConstructionAxis)
construction_entities.append(component.yConstructionAxis)
construction_entities.append(component.zConstructionAxis)
construction_entities.append(component.xYConstructionPlane)
construction_entities.append(component.xZConstructionPlane)
construction_entities.append(component.yZConstructionPlane)
construction_entities.append(component.originConstructionPoint)
return construction_entities
def hide_all_sketches():
ao = AppObjects()
for component in ao.design.allComponents:
for sketch in component.sketches:
sketch.isLightBulbOn = False
def isolate():
show_all_occurrences()
hide_all_bodies()
hide_all_sketches()
hide_all_construction()
hide_all_joints()
def make_component_visible(component):
ao = AppObjects()
occurrences = ao.root_comp.allOccurrencesByComponent(component)
for occurrence in occurrences:
if not occurrence.isVisible:
occurrence.isLightBulbOn = True
def make_body_visible(body: adsk.fusion.BRepBody):
parent_component = body.parentComponent
was_on = parent_component.isBodiesFolderLightBulbOn
parent_component.isBodiesFolderLightBulbOn = True
body.isLightBulbOn = True
def get_body(this_body: adsk.fusion.BRepBody):
body = {
"name": this_body.name,
"body": this_body,
"light_bulb": this_body.isLightBulbOn,
"visible": this_body.isVisible
}
return body
def get_component(feature):
component = {
"name": feature.parentComponent.name,
"component": feature.parentComponent,
"bodies": []
}
for this_body in feature.bodies:
component["bodies"].append(get_body(this_body))
if not this_body.isVisible:
make_body_visible(this_body)
make_component_visible(feature.parentComponent)
return component
def build_display_state_object():
global display_state_object
ao = AppObjects()
root_comp = ao.root_comp
display_state_object = {
"components": {},
"sketches": {},
"bodies": {},
"construction": {},
"origin": {},
"joints": {}
}
if root_comp is not None:
all_occurrences = ao.root_comp.allOccurrences
for occurrence in all_occurrences:
display_state_object[occurrence.fullPathName] = occurrence.isLightBulbOn
for component in ao.design.allComponents:
display_state_object["components"][component.name] = component.isBodiesFolderLightBulbOn
display_state_object["sketches"][component.name] = component.isSketchFolderLightBulbOn
display_state_object["construction"][component.name] = component.isConstructionFolderLightBulbOn
display_state_object["origin"][component.name] = component.isOriginFolderLightBulbOn
display_state_object["joints"][component.name] = component.isJointsFolderLightBulbOn
for sketch in component.sketches:
display_state_object["sketches"][component.name + "_" + sketch.name] = sketch.isLightBulbOn
for body in component.bRepBodies:
display_state_object["bodies"][component.name + "_" + body.name] = body.isLightBulbOn
for entity in get_all_construction(component):
display_state_object["construction"][component.name + "_" + entity.name] = entity.isLightBulbOn
for joint in component.joints:
display_state_object["joints"][component.name + "_" + joint.name] = joint.isLightBulbOn
# return display_state_object
def reset_display_state():
global display_state_object
ao = AppObjects()
root_comp = ao.root_comp
if root_comp is None:
return False
all_occurrences = root_comp.allOccurrences
for occurrence in all_occurrences:
state = display_state_object.get(occurrence.fullPathName, None)
if state is not None:
occurrence.isLightBulbOn = state
for component in ao.design.allComponents:
body_folder_state = display_state_object["components"].get(component.name, None)
if body_folder_state is not None:
component.isBodiesFolderLightBulbOn = body_folder_state
sketch_folder_state = display_state_object["sketches"].get(component.name, None)
if sketch_folder_state is not None:
component.isSketchFolderLightBulbOn = sketch_folder_state
construction_folder_state = display_state_object["construction"].get(component.name, None)
if construction_folder_state is not None:
component.isConstructionFolderLightBulbOn = construction_folder_state
origin_folder_state = display_state_object["origin"].get(component.name, None)
if origin_folder_state is not None:
component.isOriginFolderLightBulbOn = origin_folder_state
joint_folder_state = display_state_object["joints"].get(component.name, None)
if joint_folder_state is not None:
component.isJointsFolderLightBulbOn = joint_folder_state
for body in component.bRepBodies:
body_state = display_state_object["bodies"].get(component.name + "_" + body.name, None)
if body_state is not None:
body.isLightBulbOn = body_state
for sketch in component.sketches:
sketch_state = display_state_object["sketches"].get(component.name + "_" + sketch.name, None)
if sketch_state is not None:
sketch.isLightBulbOn = sketch_state
for entity in get_all_construction(component):
construction_state = display_state_object["construction"].get(component.name + "_" + entity.name, None)
if construction_state is not None:
entity.isLightBulbOn = construction_state
for joint in component.joints:
joint_state = display_state_object["joints"].get(component.name + "_" + joint.name, None)
if joint_state is not None:
joint.isLightBulbOn = joint_state
def make_message(result):
timeline_message = "<b>Feature Information</b><br />"
timeline_message += "<b> Name: </b>" + result["name"] + "<br />"
timeline_message += "<b> Type: </b>" + result["type"] + "<br />"
health_state = result.get("health_state", None)
if health_state is not None:
timeline_message += "<b> Health: </b>"
health = next(name for name, value in vars(adsk.fusion.FeatureHealthStates).items() if value == health_state)
if health_state == adsk.fusion.FeatureHealthStates.HealthyFeatureHealthState:
health = "Healthy"
timeline_message += health + "<br />"
if result.get("type", "") == "Sketch":
timeline_message += "<br /><b>Sketch Information</b><br />"
timeline_message += "<b> Fully Constrained: </b>"
timeline_message += str(result["fully_constrained"]) + "<br />"
timeline_message += "<b> Parent Component: </b>"
timeline_message += result["parent_component"] + "<br />"
timeline_message += "<b> Sketch Plane: </b>"
timeline_message += result["plane"] + "<br />"
timeline_message += "<b> Plane Type: </b>"
timeline_message += result["plane_type"] + "<br />"
elif result.get("type", "") == "Make Component":
timeline_message += "<b> Component: </b>"
timeline_message += result["parent_component"] + "<br />"
elif result.get("type", "") == "Construction Geometry":
timeline_message += "<b> Parent Component: </b>"
timeline_message += result["parent_component"] + "<br />"
timeline_message += "<b> Type: </b>"
timeline_message += result["construction_type"] + "<br />"
elif result.get("type", "") == "Joint":
timeline_message += "<b> Parent Component: </b>"
timeline_message += result["parent_component"] + "<br />"
timeline_message += "<b> Reference 1: </b>"
timeline_message += result["part_1_name"] + "<br />"
timeline_message += "<b> Reference 2: </b>"
timeline_message += result["part_2_name"] + "<br />"
elif result.get("type", "") == "Feature":
components = result.get("components", [])
if len(components) > 0:
timeline_message += "<br /><b>Affected Components: </b>"
for component in components:
timeline_message += "<br /><b> " + component["name"] + "</b><br />"
bodies = component.get("bodies", [])
if len(bodies) > 0:
timeline_message += " "
timeline_message += "Affected Bodies:<br />"
for body in bodies:
timeline_message += " "
timeline_message += body["name"] + "<br />"
warning = result.get("error_message", "")
if len(warning) > 0:
timeline_message += "<br /><b>Error or Warning Message: </b><br />"
timeline_message += warning
return timeline_message
def play_feature():
ao = AppObjects()
timeline = ao.time_line
if timeline is None:
return False
elif timeline.markerPosition == timeline.count:
return False
timeline_object = timeline.item(timeline.markerPosition)
result = {}
if timeline_object.isGroup:
timeline_object.isCollapsed = False
result["type"] = "group"
else:
timeline.movetoNextStep()
result["entity"] = timeline_object.entity
result["index"] = timeline_object.index
result["suppressed"] = timeline_object.isSuppressed
result["name"] = timeline_object.name
feature = adsk.fusion.Feature.cast(timeline_object.entity)
sketch = adsk.fusion.Sketch.cast(timeline_object.entity)
occurrence = adsk.fusion.Occurrence.cast(timeline_object.entity)
plane = adsk.fusion.ConstructionPlane.cast(timeline_object.entity)
axis = adsk.fusion.ConstructionAxis.cast(timeline_object.entity)
point = adsk.fusion.ConstructionPoint.cast(timeline_object.entity)
joint = adsk.fusion.Joint.cast(timeline_object.entity)
if plane is not None:
construction_entity = plane
construction_type = "Plane"
elif axis is not None:
construction_entity = axis
construction_type = "Axis"
elif point is not None:
construction_entity = point
construction_type = "Point"
else:
construction_entity = None
construction_type = ""
if feature is not None:
isolate()
result["error_message"] = timeline_object.errorOrWarningMessage
result["health_state"] = timeline_object.healthState
result["type"] = "Feature"
result["feature"] = feature
result["components"] = []
result["components"].append(get_component(feature))
if feature.linkedFeatures.count > 0:
for this_feature in feature.linkedFeatures:
result["components"].append(get_component(this_feature))
elif sketch is not None:
isolate()
show_sketch(sketch)
make_component_visible(sketch.parentComponent)
result["error_message"] = sketch.errorOrWarningMessage
result["health_state"] = sketch.healthState
result["type"] = "Sketch"
result["sketch"] = sketch
result["fully_constrained"] = sketch.isFullyConstrained
result["parent_component"] = sketch.parentComponent.name
reference_plane = sketch.referencePlane
face = adsk.fusion.BRepFace.cast(reference_plane)
plane = adsk.fusion.ConstructionPlane.cast(reference_plane)
if face is not None:
result["plane"] = "Face ID: " + str(face.tempId)
result["plane_type"] = "Planar Face"
result["plane_entity"] = face
make_body_visible(face.body)
elif plane is not None:
result["plane"] = plane.name
result["plane_type"] = "Construction Plane"
result["plane_entity"] = plane
else:
result["plane"] = "Unknown Sketch Plane"
result["plane_type"] = "unknown"
result["plane_entity"] = reference_plane
elif occurrence is not None:
isolate()
show_occurrence(occurrence)
result["type"] = "Make Component"
result["occurrence"] = occurrence
result["parent_component"] = occurrence.component.name
elif construction_entity is not None:
isolate()
show_construction(construction_entity)
make_component_visible(construction_entity.component)
result["error_message"] = construction_entity.errorOrWarningMessage
result["health_state"] = construction_entity.healthState
result["type"] = "Construction Geometry"
result["construction_type"] = construction_type
result["construction_entity"] = construction_entity
result["parent_component"] = construction_entity.component.name
elif joint is not None:
isolate()
show_joint(joint)
show_occurrence(joint.occurrenceOne)
show_occurrence(joint.occurrenceTwo)
result["error_message"] = joint.errorOrWarningMessage
result["health_state"] = joint.healthState
result["type"] = "Joint"
result["joint"] = joint
result["part_1"] = joint.occurrenceOne
result["part2"] = joint.occurrenceTwo
result["part_1_name"] = joint.occurrenceOne.name
result["part_2_name"] = joint.occurrenceTwo.name
result["parent_component"] = joint.parentComponent.name
else:
result["type"] = timeline_object.entity.objectType
# ao.app.activeViewport.fit()
return result
def more_features():
ao = AppObjects()
timeline = ao.time_line
if timeline is None:
return False
elif timeline.markerPosition == timeline.count:
return False
else:
return True
# Class for a Fusion 360 Command
# Place your program logic here
# Delete the line that says "pass" for any method you want to use
class PlayerCommand(Fusion360CommandBase):
# Run whenever a user makes any change to a value or selection in the addin UI
# Commands in here will be run through the Fusion processor and changes will be reflected in Fusion graphics area
def on_preview(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
pass
# Run after the command is finished.
# Can be used to launch another command automatically or do other clean up.
def on_destroy(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, reason, input_values):
if reason == adsk.core.CommandTerminationReason.CancelledTerminationReason or \
reason == adsk.core.CommandTerminationReason.PreEmptedTerminationReason:
reset_display_state()
# Run when any input is changed.
# Can be used to check a value and then update the add-in UI accordingly
def on_input_changed(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, changed_input,
input_values):
# ao = AppObjects()
# if changed_input.id == "play_controls_id":
# button = changed_input.selectedItem
# if button is not None:
# if button.name == "play":
# if more_features():
# ao.ui.commandDefinitions.itemById("cmdID_PlayerCommand").execute()
# button.isSelected = False
pass
# Run when the user presses OK
# This is typically where your main program logic would go
def on_execute(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
ao = AppObjects()
if more_features():
ao.ui.commandDefinitions.itemById("cmdID_PlayerCommand").execute()
else:
reset_display_state()
# Run when the user selects your command icon from the Fusion 360 UI
# Typically used to create and display a command dialog box
# The following is a basic sample of a dialog UI
def on_create(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs):
result = play_feature()
if result:
message = make_message(result)
command.okButtonText = "Next"
command.cancelButtonText = "Done"
else:
message = "You are already at the end of the timeline. " \
"Rollback to use Player to step through the features in the timeline"
inputs.addTextBoxCommandInput("message_id", "", message, 20, True)
# button_row = inputs.addButtonRowCommandInput("play_controls_id", "Controls: ", False)
# button_row.listItems.add("play", False, "./resources", -1)
# button_row.listItems.add("stop", False, "./resources", -1)
# inputs.addTextBoxCommandInput("message_id", "", str(result), 20, True)
class PlayFromHereCommand(Fusion360CommandBase):
def on_execute(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
global display_state_object
ao = AppObjects()
build_display_state_object()
ao.ui.commandDefinitions.itemById("cmdID_PlayerCommand").execute()
class PlayFromStartCommand(Fusion360CommandBase):
def on_execute(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
global display_state_object
ao = AppObjects()
build_display_state_object()
ao.time_line.moveToBeginning()
ao.ui.commandDefinitions.itemById("cmdID_PlayerCommand").execute()