-
Notifications
You must be signed in to change notification settings - Fork 0
/
idiophone-bar.py
131 lines (100 loc) · 4.64 KB
/
idiophone-bar.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
import adsk.core, adsk.fusion, traceback
import os, math
IDIOPHONE_NODE_RATIO = 0.2242
handlers = []
app = adsk.core.Application.get()
ui = app.userInterface
def createNewComponent():
root = adsk.fusion.Design.cast(app.activeProduct).rootComponent
return root.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
class ModalBarCommandExecuteHandler(adsk.core.CommandEventHandler):
def notify(self, args):
try:
intputById = args.firingEvent.sender.commandInputs.itemById
evaluate = app.activeProduct.unitsManager.evaluateExpression
getInput = lambda id: evaluate(intputById(id).expression, "cm")
l, w, h, b = getInput('barLength'), getInput('barWidth'), getInput('barHeight'), getInput('brightness')
buildModalBar(l, w, h, b)
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class ModalBarCommandDestroyHandler(adsk.core.CommandEventHandler):
def notify(self, args):
try:
adsk.terminate()
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class ModalBarCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def notify(self, args):
try:
onExecute = ModalBarCommandExecuteHandler()
args.command.execute.add(onExecute)
handlers.append(onExecute)
onDestroy = ModalBarCommandDestroyHandler()
args.command.destroy.add(onDestroy)
handlers.append(onDestroy)
inputs = args.command.commandInputs
val = adsk.core.ValueInput.createByReal
inputs.addValueInput('barLength', 'Bar Length.', 'cm' , val (50.0))
inputs.addValueInput('barWidth', 'Bar Width.', 'cm' , val (5.715))
inputs.addValueInput('barHeight', 'Bar Height.', 'cm' , val(1.27))
inputs.addValueInput('brightness', 'Brightness', '' , val(0.8))
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def createExtrude(component, profile, thickness):
'''
Create an extrude feature on the provided component with the provided profile and thickness
'''
join = adsk.fusion.FeatureOperations.JoinFeatureOperation
extrudes = component.features.extrudeFeatures
extInput = extrudes.createInput(profile, join)
extInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(thickness))
return extrudes.add(extInput)
def buildModalBar(l, w, h, b):
'''
Create a modal bar feature with the provided length, width, and height dimensions. Bar arc
is determined by the provided brightness [0, 1.0).
'''
component = createNewComponent()
if component is None:
ui.messageBox('New component failed to create', 'New Component Failed')
return
sketch = component.sketches.add(component.xYConstructionPlane)
sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints
sketchCircle = sketch.sketchCurves.sketchCircles.addByCenterRadius
Point = adsk.core.Point3D.create
# Sketch the outline
sketchLine(Point(0, 0, 0), Point(l, 0, 0))
sketchLine(Point(l, 0, 0), Point(l, h, 0))
sketchLine(Point(l, h, 0), Point(0, h, 0))
sketchLine(Point(0, h, 0), Point(0, 0, 0))
# Sketch the holes
holeOffset = l * IDIOPHONE_NODE_RATIO
sketchCircle(Point(holeOffset, h / 2, 0), h / 4)
sketchCircle(Point(l - holeOffset, h / 2, 0), h / 4)
# Sketch the arc
points = adsk.core.ObjectCollection.create()
points.add(Point(holeOffset, 0, 0))
points.add(Point(l / 2, h * b, 0))
points.add(Point(l - holeOffset, 0, 0))
sketch.sketchCurves.sketchFittedSplines.add(points)
# Create the extrusion.
extOne = createExtrude(component, sketch.profiles[3], w)
extOne.faces[0].body.name = 'Modal Bar'
def run(context):
'''
Set up the handlers for the ModalBar command and execute it.
'''
try:
commandId = 'ModalBar'
cmdDef = ui.commandDefinitions.itemById(commandId)
if not cmdDef:
resourceDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../resources') # absolute resource file path is specified
cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, 'Create Modal Bar', 'Create Modal Bar', resourceDir)
onCommandCreated = ModalBarCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
# keep the handler referenced beyond this function
handlers.append(onCommandCreated)
cmdDef.execute(adsk.core.NamedValues.create())
adsk.autoTerminate(False)
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))