Addin Help - Can't get input data from dialog for sketching
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi folks,
Day two of writing an Addin for Fusion 360. Let me disclaim that I am a novice at programming and still learning. I apologize in advance. Here is what I am trying to achieve with my script at this point in time...
1. Toolbar icon clicked
2. Dialogue is presented to user
3. Three input commands are entered by user
4. Input data values are used to create a sketch
Quite simple. I have managed to achieve all of this, except #4 - actually getting the input values and making a sketch with them. I simply cannot get the sketch to even be created after hitting "OK" from the dialogue. I have reviewed the online user manual for how to write command inputs and such, but I still could not get it with the equilateral triangle generator that the tutor made.
If you could review my code and let me know exactly what I'm doing wrong, that would be very appreciated. P.S. my stop function is not working properly so my code is buggy (something I still cant figure out how to do), so im forced to restart fusion after executing the dialogue... or if i'm luck enough that fusion doesnt crash on me from the code.
CODE:
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 run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface # Get the CommandDefinitions collection. load_cmds = ui.commandDefinitions # Create a button command definition. definition1 = load_cmds.addButtonDefinition('mainbutton', 'BM Addin Pro', 'My Awesome Addin!', './Resources/Main Icons') # Connect to the command created event. mainbuttonpressed = MainCommandCreatedEventHandler() definition1.commandCreated.add(mainbuttonpressed) handlers.append(mainbuttonpressed) # Get the ADD-INS panel in the model workspace. addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel') # Add the button to the bottom of the panel. mainbutton_add = addInsPanel.controls.addCommand(definition1) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # Event handler for the commandCreated event. class MainCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.CommandCreatedEventArgs.cast(args) # Get the command cmd = eventArgs.command # Get the CommandInputs collection to create new command inputs. inputs = cmd.commandInputs # Create the value inputs imageinput = inputs.addImageCommandInput('imageinput','Diagram:','./Resources/Image Input/image.png') platelength = inputs.addValueInput('platelength', 'Length', '', adsk.core.ValueInput.createByReal(1.00)) platewidth = inputs.addValueInput('platewidth','Width','', adsk.core.ValueInput.createByReal(0.500)) platethick = inputs.addValueInput('platethick','Thickness','', adsk.core.ValueInput.createByReal(0.250)) # Connect to the execute event. onExecute = SampleCommandExecuteHandler() cmd.execute.add(onExecute) handlers.append(onExecute) # Event handler for the execute event. class SampleCommandExecuteHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.CommandEventArgs.cast(args) ### Execution AFTER pressing 'OK'################# import math # Get the values from the command inputs. inputs = eventArgs.command.commandInputs length = inputs.itemById('platelength').valueOne width = inputs.itemById('platewidth').valueOne thickness = inputs.itemById('platethick').valueOne drawgeometry(length,width,thickness) def drawgeometry(length,width,thickness): # Get the active sketch. app = adsk.core.Application.get() sketch = adsk.fusion.Sketch.cast(app.activeEditObject) # Get the root component of the active design. rootComp = design.rootComponent # Create a new sketch on the xy plane. sketches = rootComp.sketches xyPlane = rootComp.xYConstructionPlane sketch1 = sketches.add(xyPlane) # Draw the three lines for the triangle. load_lines = sketch.sketchCurves.sketchLines L1 = load_lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(length, 0, 0)) def stop(context): try: app = adsk.core.Application.get() ui = app.userInterface # Clean up the UI. definition1 = ui.commandDefinitions.itemById('mainbutton') if definition1: definition1.deleteMe() addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel') mainbuttonpressed = addinsPanel.controls.itemById('mainbutton') if mainbutton_add: mainbutton_add.deleteMe() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))