Addin Help - Can't get input data from dialog for sketching

Addin Help - Can't get input data from dialog for sketching

Six1Seven1
Advocate Advocate
1,072 Views
8 Replies
Message 1 of 9

Addin Help - Can't get input data from dialog for sketching

Six1Seven1
Advocate
Advocate

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()))	

 

 

0 Likes
1,073 Views
8 Replies
Replies (8)
Message 2 of 9

Six1Seven1
Advocate
Advocate

Hello,

 

I am still having no luck with getting my 'OK' button to draw some sketch geometry with input data. I am following this guide HERE* under 'Execute Event' section. I am quite certain the code is faulty in this guide because it is not working regardless of what I do. 

 

Please disregard my OP's code and see the revised code below. Absolutely NOTHING happens when htting 'OK' on my dialog.

 

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,'mainbutton_add')
        mainbutton_add.isPromotedByDefault = True
        mainbutton_add.isPromoted = True
    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)
    
    
def drawgeometry(length):
    
    app = adsk.core.Application.get()
    sketch = adsk.fusion.Sketch.cast(app.activeEditObject)
    sketch.isComputeDeferred = True
    if sketch:
        # Draw the three lines for the triangle. 
        lines = sketch.sketchCurves.sketchLines
        l1 = lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), 
                                  adsk.core.Point3D.create(length,0,0))
        l2 = lines.addByTwoPoints(l1.endSketchPoint, 
                                  adsk.core.Point3D.create(baseLength/2, length, 0))
        l3 = lines.addByTwoPoints(l2.endSketchPoint, l1.startSketchPoint)
        return True
    else:
        return False

    sketch.isComputeDeferred = False
 
    
    
               

def stop(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('   Terminate the Addin?   ', 'Close BM Addin Pro',3,1)           #format: (message text, title of window, button type, icon type)
        # Clean up the UI.
        definition1 = ui.commandDefinitions.itemById('mainbutton')
        if definition1:
            definition1.deleteMe()
            
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        mainbutton_add = addinsPanel.controls.itemById('mainbutton')
        if mainbutton_add:
            mainbutton_add.deleteMe()
    
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))	
0 Likes
Message 3 of 9

Six1Seven1
Advocate
Advocate

Code is definitely not functioning properly that i written in the guide:   Click

 

I have copied all the code in that guide and the addin still does absolutely nothing after hitting the 'OK' button. No sketch is created. No lines are drawn. 

 

If someone would chime in, that would be great.

 

0 Likes
Message 4 of 9

Six1Seven1
Advocate
Advocate

Anyone?

0 Likes
Message 5 of 9

ekinsb
Alumni
Alumni

I didn't try debugging all of your code but there's one problem that immediately stands out.  In the execute event handler you're calling the valueOne property to get the current value from the command inputs.  The inputs you created are ValueCommandInput objects and it doesn't support a valueOne property but only the value property.  The code you copied must have used slider inputs which do support the valueOne and valueTwo properties.

 

A suggestion to help catch issues like this is to add try except statements to each of your functions.  This would have immediately pointed out that the valueOne property is not supported.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 6 of 9

Six1Seven1
Advocate
Advocate

Brian,

 

Thanks for noticing that. I did not look at the reference for that particular property, but had assumed it was acceptable, given that the guide used that property. However, looking back, the guide uses either the option of imputing a value, or using an input changed event handler with the slider to update the input value box. I will say the guide is great but it not basic enough for those who really need the guide (in my opinion). A simple guide to generate a dialogue with command inputs and then linking them to a sketch would be all that is needed to get most people up on their feet.

 

I will use your advice and tips and return back on any success or failure I have.

 

I appreciate your time,

Regards

0 Likes
Message 7 of 9

Six1Seven1
Advocate
Advocate

Brian,

 

I took your advice and my program now works! It was in fact the .valueOne property that was not allowing the program to pull the input data. Changing to the .value property solved the issues. Just one question, could you perhaps provide a link to the API Reference guide where this is explained? I cannot find the .value property.

 

Thank you very much.

 

Regards,

 

0 Likes
Message 8 of 9

ekinsb
Alumni
Alumni

Here's where I looked first.  It's the topic for the ValueCommandInput object, which is what you're creating to the sizes from the user.  It has the full list of methods and properties that the objects supports and you'll notice that "value" is one of the properties.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-671f0782-efe0-4e33-a744-afa3d5619a01

 

Here's the topic specific to the "value" property.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-5491a9b5-b8e9-4940-ba4e-6992c1eec5e7

 

Another useful topic in this area is this one that has a brief description of each type of command input and also has a direct link to the topic for the equivalent API object.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8B9041D5-75CC-4515-B4BB-4CF2CD5BC359 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 9 of 9

Six1Seven1
Advocate
Advocate

Highly appreciated, Brian. I will review the links you provided.

 

Thank you very much for your help with this. 

 

 

Best wishes,

0 Likes