forked from Audiohotshot/fretgenerator360
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFret Generator.py
342 lines (287 loc) · 14.8 KB
/
Fret Generator.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
# Audiohotshot Fret Generator
# This plugin is under Common Creative License
# February 2019
import adsk.core, adsk.fusion, adsk.cam, traceback
# Global list to keep all event handlers in scope.
# This is only needed with Python.
handlers = []
def drawSketch(scalehigh, scalelow, fretno, fretwidth, fretoffset, dots, radius):
# DRAWING STARTS HERE
# Get the root component of the active design.
app = adsk.core.Application.get()
design = app.activeProduct
rootComp = design.rootComponent
# Create a new sketch on the xy plane.
sketches = rootComp.sketches;
xyPlane = rootComp.xYConstructionPlane
sketch = sketches.add(xyPlane)
yup = 0- fretwidth / 2
ydown = fretwidth / 2
lines = sketch.sketchCurves.sketchLines;
# draw scalelines high and low
lines.addByTwoPoints(adsk.core.Point3D.create(0, yup, 0), adsk.core.Point3D.create(scalehigh, yup, 0))
lines.addByTwoPoints(adsk.core.Point3D.create(0 - fretoffset, ydown, 0), adsk.core.Point3D.create(scalelow - fretoffset, ydown, 0))
#draw tremolo position line
lines.addByTwoPoints(adsk.core.Point3D.create(scalehigh, yup, 0), adsk.core.Point3D.create(scalelow - fretoffset, ydown, 0))
#draw center construction line
xconstructionstart = (0 - fretoffset) / 2
yconstructionstart = 0
xconstructionend = ((scalehigh + (scalelow - fretoffset)) /2)
yconstructionend = 0
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(xconstructionstart, yconstructionstart, 0), adsk.core.Point3D.create(xconstructionend, yconstructionend, 0))
line1.isConstruction = True
# draw individual frets
n = 0
while True:
#draw fretlines
distancehigh = scalehigh - (scalehigh / 2 ** (n/12))
distancelow = scalelow - fretoffset - (scalelow / 2 ** (n/12))
lines.addByTwoPoints(adsk.core.Point3D.create(distancehigh, yup, 0), adsk.core.Point3D.create(distancelow, ydown, 0))
#counter
n = n + 1
if n == fretno:
break
#draw dots
a = [0,0,1,0,1,0,1,0,1,0,0, 2,0,0,1,0,1,0,1,0,1,0,0, 2,0,0,1,0,1,0,1,0,1,0,0,2]
if dots == True:
n = 0
while True:
if n != 0:
s = a[n-1]
#calculate centre dot
xcentrehigh = scalehigh - (scalehigh / 2 ** ((n-0.5)/12))
xcentrelow = scalelow - fretoffset - (scalelow / 2 ** ((n-0.5)/12))
xcentre = (xcentrehigh + xcentrelow) /2
ycentre = 0
if s == 1:
#draw dot
circles = sketch.sketchCurves.sketchCircles
circles.addByCenterRadius(adsk.core.Point3D.create(xcentre, ycentre, 0), radius)
if s == 2:
#calculate x position dots
xdistance = xcentrehigh - xcentrelow
if xdistance != 0:
xcircletop= xcentrehigh - (1/3 * fretwidth) / (fretwidth/xdistance)
xcirclebottom = xcentrehigh - (2/3 * fretwidth) / (fretwidth/xdistance)
else:
xcircletop= xcentrehigh
xcirclebottom = xcentrehigh
#calculate y position dots
ycircletop = (0 -fretwidth / 2) + (fretwidth * (1/3))
ycirclebottom = (0 -fretwidth / 2) + (fretwidth * (2/3))
#draw dots
circles = sketch.sketchCurves.sketchCircles
circles.addByCenterRadius(adsk.core.Point3D.create(xcircletop, ycircletop, 0), radius)
circles.addByCenterRadius(adsk.core.Point3D.create(xcirclebottom, ycirclebottom, 0), radius)
n = n + 1
if n == fretno:
break
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Get the CommandDefinitions collection.
cmdDefs = ui.commandDefinitions
# Create a button command definition.
buttonSample = cmdDefs.addButtonDefinition('MyButtonDefIdPython',
'Fret Generator',
'Sample button tooltip',
'./Resources')
# Connect to the command created event.
sampleCommandCreated = SampleCommandCreatedEventHandler()
buttonSample.commandCreated.add(sampleCommandCreated)
handlers.append(sampleCommandCreated)
# Get the ADD-INS panel in the model workspace.
addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
# Add the button to the bottom of the panel.
addInsPanel.controls.addCommand(buttonSample)
if context['IsApplicationStartup'] == False:
ui.messageBox('The "Fret Generator" command has been added\nto the ADDIN panel')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Clean up the UI.
cmdDef = ui.commandDefinitions.itemById('MyButtonDefIdPython')
if cmdDef:
cmdDef.deleteMe()
addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
cntrl = addinsPanel.controls.itemById('MyButtonDefIdPython')
if cntrl:
cntrl.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the inputChanged event.
class SampleCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
app = adsk.core.Application.get()
ui = app.userInterface
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
# Check the value of the check box.
changedInput = eventArgs.input
if changedInput.id == 'slanted':
inputs = eventArgs.firingEvent.sender.commandInputs
targetInput = inputs.itemById('scalelow')
targetInput2 = inputs.itemById('slantedcentre')
# Change the visibility of the scale value input.
if changedInput.value == False:
targetInput.isVisible = False
targetInput2.isVisible = False
else:
targetInput.isVisible = True
targetInput2.isVisible = True
# Check the value of the check box.
if changedInput.id == 'drawdots':
inputs = eventArgs.firingEvent.sender.commandInputs
targetInput = inputs.itemById('dotsize')
# Change the visibility of the scale value input.
if changedInput.value == False:
targetInput.isVisible = False
else:
targetInput.isVisible = True
except:
if ui:
ui.messageBox(('Input changed event failed: {}').format(traceback.format_exc()))
# Event handler for the execute event.
class SampleCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
# Code to react to the event.
app = adsk.core.Application.get()
ui = app.userInterface
#ui.messageBox('In command execute event handler.')
try:
command = args.firingEvent.sender
ui.messageBox(('command: {} executed successfully').format(command.parentCommandDefinition.id))
except:
if ui:
ui.messageBox(('command executed failed: {}').format(traceback.format_exc()))
# Event handler for the commandCreated event.
class SampleCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
app = adsk.core.Application.get()
ui = app.userInterface
# Get the commandS
cmd = eventArgs.command
# Get the CommandInputs collection to create new command inputs.
inputs = cmd.commandInputs
# Create the value input to get the scale.
inputs.addValueInput('scalehigh', 'Scale high', 'mm', adsk.core.ValueInput.createByReal(63.5))
# Create a check box to get if it should be slanted or not.
inputs.addBoolValueInput('slanted', 'Slanted', True, '', False)
# Create the value input to get the lower scale.
scaleLow = inputs.addValueInput('scalelow', 'Scale Low', 'mm', adsk.core.ValueInput.createByReal(68))
scaleLow.isVisible = False
# Create the value input to get the centre fret of the slanted scale.
slantedCentre = inputs.addValueInput('slantedcentre', 'Centre fret', '', adsk.core.ValueInput.createByReal(12))
slantedCentre.isVisible = False
# Create the value input to get the amount of frets.
inputs.addValueInput('fretsno', 'Number of frets', '', adsk.core.ValueInput.createByReal(24))
#fretsNo = inputs.addIntegerSliderCommandInput('fretsno', 'Number of frets', 1, 36)
# Create the value input to get the width of the board.
inputs.addValueInput('fretswidth', 'Width', 'mm', adsk.core.ValueInput.createByReal(5.5))
# Create a check box to get if it should draw dots or not.
inputs.addBoolValueInput('drawdots', 'Draw dots', True, '', False)
# Create the value input to get the dotsize
dotsize = inputs.addValueInput('dotsize', 'Diameter', 'mm', adsk.core.ValueInput.createByReal(0.4))
dotsize.isVisible = False
#errMessage = inputs.addTextBoxCommandInput('errMessage', '', '', 2, True)
errMessage = inputs.addTextBoxCommandInput('errmessage', 'Text Box 1', '', 2, True)
errMessage.isFullWidth = True
# Connect to the execute event.
onExecute = SampleCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
# Connect to the inputChanged event.
onInputChanged = SampleCommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
handlers.append(onInputChanged)
# execute preview
onExecutePreview = SampleCommandExecutePreviewHandler()
cmd.executePreview.add(onExecutePreview)
handlers.append(onExecutePreview)
# execute validation
onValidateInputs = SampleCommandValidateInputsHandler()
cmd.validateInputs.add(onValidateInputs)
handlers.append(onValidateInputs)
except:
if ui:
ui.messageBox('Failed CreatedEventHandler:\n{}'.format(traceback.format_exc()))
# Event handler for the validateInputs event.
class SampleCommandValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
app = adsk.core.Application.get()
ui = app.userInterface
eventArgs = adsk.core.ValidateInputsEventArgs.cast(args)
inputs = eventArgs.firingEvent.sender.commandInputs
# Verify that the scale is greater than 0.1.
inputarg = inputs.itemById('fretsno').value
if inputarg < 1:
eventArgs.areInputsValid = False
return
else:
inputarg = inputs.itemById('slantedcentre').value
if inputarg < 1:
eventArgs.areInputsValid = False
return
else:
eventArgs.areInputsValid = True
except:
if ui:
ui.messageBox('Failed ValidateInputsHandler:\n{}'.format(traceback.format_exc()))
# Event handler for the executePreview event.
class SampleCommandExecutePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
app = adsk.core.Application.get()
ui = app.userInterface
eventArgs = adsk.core.CommandEventArgs.cast(args)
# Get the command
cmd = eventArgs.command
# Get the CommandInputs collection to create new command inputs.
inputs = cmd.commandInputs
slanted = inputs.itemById('slanted').value
if slanted == False:
scalehigh = inputs.itemById('scalehigh').value
scalelow = inputs.itemById('scalehigh').value
fretoffset = 0
else:
scalehigh = inputs.itemById('scalehigh').value
scalelow = inputs.itemById('scalelow').value
slantedcentre = inputs.itemById('slantedcentre').value
frethighoffset = scalehigh - (scalehigh / 2 ** ( slantedcentre /12))
fretlowoffset = scalelow - (scalelow / 2 ** ( slantedcentre /12))
fretoffset = fretlowoffset - frethighoffset
fretno = inputs.itemById('fretsno').value + 1
fretwidth = -(inputs.itemById('fretswidth').value)
drawdots = inputs.itemById('drawdots').value
if drawdots == False:
dots = False
else:
dots = True
radius = inputs.itemById('dotsize').value /2
drawSketch(scalehigh, scalelow, fretno, fretwidth, fretoffset, dots, radius)
# Set the isValidResult property to use these results at the final result.
# This will result in the execute event not being fired.
eventArgs.isValidResult = True
except:
if ui:
ui.messageBox('Failed ValidateInputsHandler:\n{}'.format(traceback.format_exc()))