Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

InputEventChangedHandler prevents data input on other command input IDs

2 REPLIES 2
Reply
Message 1 of 3
Six1Seven1
207 Views, 2 Replies

InputEventChangedHandler prevents data input on other command input IDs

Hi, back again... (my apologies),

 

 

So, I added an InputChangedEvent handler to my program. The reason for this is to detect a checkbox. If checkbox is true, then another value input will expose itself.

 

Before this checkbox, there are 3 value input commands that execute the creation of a box based on their input dimensions. The problem I am experiencing is that when adding this new InputChangedEvent handler, it prevents the execution of my def drawBox function. (the program just idles and does nothing on execution). Its as if it is waiting for a change in the checkbox (inputchangedevent).

 

Now, i'm actually thinking it has to do with one specific line in my inputChangedEvent handler, which is...

inputs = eventArgs.firingEvent.sender.commandInputs

i'm not sure. Any help is greatly appreciated. Here is the rest of my 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.
        cmdDefs = ui.commandDefinitions
        
        # Create a button command definition.
        buttonSample = cmdDefs.addButtonDefinition('MainButton','BM Addin Pro', 'This is an Addin for generating boxes','./Resources/Main Icons')
        
        # Connect to the command created event.
        MainButtonEvent = SampleCommandCreatedEventHandler()
        buttonSample.commandCreated.add(MainButtonEvent)
        handlers.append(MainButtonEvent)
        
        # Get the ADD-INS panel in the model workspace. 
        addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        
        # Add the button to the bottom of the panel.
        buttonControl = addInsPanel.controls.addCommand(buttonSample)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.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()
        
            # Get the command
            cmd = eventArgs.command
            # Get the CommandInputs collection to create new command inputs.            
            inputs = cmd.commandInputs
            #Add image input
            imageinput1 = inputs.addImageCommandInput('imageinput1','Diagram:','./Resources/Image Input/image main.png')

            # Create the value inputs
            length_in = inputs.addValueInput('length_in', 'Length','', adsk.core.ValueInput.createByReal(3)) 
            width_in = inputs.addValueInput('width_in','Width','',adsk.core.ValueInput.createByReal(2))
            thick_in = inputs.addValueInput('thick_in','Thickness','',adsk.core.ValueInput.createByReal(1))
            
            
            fillet_cb = inputs.addBoolValueInput('fillet_cb','Add Fillets', True,'', True)
            fillet_in = inputs.addValueInput('fillet_in','Fillet Radius','',adsk.core.ValueInput.createByReal(0))
            
            
            onExecute = SampleCommandExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)   
            
            
            onInputChanged = inputChangedEvent()
            cmd.inputChanged.add(onInputChanged)
            handlers.append(onInputChanged)

            
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))



# Event handler for the execute event.
class SampleCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
       
            eventArgs = adsk.core.CommandEventArgs.cast(args)

            # Code to react to the event.
            app = adsk.core.Application.get()
            ui  = app.userInterface
            ui.messageBox('In command execute event handler.')
    
            eventArgs = adsk.core.CommandEventArgs.cast(args)
    
            # Get the values from the command inputs. 
            inputs = eventArgs.command.commandInputs
    
            length = inputs.itemById('length_in').value
            width = inputs.itemById('width_in').value
            thickness = inputs.itemById('thick_in').value
            
    
            #call drawBox function and add input values
            drawBox(length, width, thickness)
            
            #call addFillets function and add input value
            
        
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))	



def drawBox(length_in, width_in, thick_in): #these values are item IDs - not variables (see above)
    try: 
        # Get the active sketch. 
        app = adsk.core.Application.get()
        sketch = adsk.fusion.Sketch.cast(app.activeEditObject)
        ui = app.userInterface
                        
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)             
        act = app.activeProduct
                
        # Get the root component of the active design.
        rootComp = act.rootComponent  
          
        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch1 = sketches.add(xyPlane)
        # Load the lines collection and draw geometry for BOX sketch (division for center around origin)          
        Load_lines = sketch1.sketchCurves.sketchLines                                                                       #load the line API
        L1 = Load_lines.addByTwoPoints(adsk.core.Point3D.create((length_in/-2), (width_in/-2), 0), adsk.core.Point3D.create((length_in/2),(width_in/-2), 0))
        L2 = Load_lines.addByTwoPoints(adsk.core.Point3D.create((length_in/2),(width_in/-2),0), adsk.core.Point3D.create((length_in/2),(width_in/2),0))
        L3 = Load_lines.addByTwoPoints(adsk.core.Point3D.create((length_in/2),(width_in/2),0), adsk.core.Point3D.create((length_in/-2),(width_in/2),0))                 #draw lines (start, end)
        L4 = Load_lines.addByTwoPoints(adsk.core.Point3D.create((length_in/-2),(width_in/2),0), adsk.core.Point3D.create((length_in/-2),(width_in/-2),0))
        
        #get sketch profile
        prof1 = sketch1.profiles.item(0)
                    
        #Extrude thickness                                                                                                                     
        extrude1 = rootComp.features.extrudeFeatures                                                                  
        extrude1_prof = extrude1.createInput(prof1,adsk.fusion.FeatureOperations.NewComponentFeatureOperation)    
                                                                                                                                    
        distance = adsk.core.ValueInput.createByReal(thick_in)
        extrude1_prof.setDistanceExtent(False, distance)
        finish_extrude1 = extrude1.add(extrude1_prof)    #executes the extrusion                                                                                                                       
             
        
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))	       


class inputChangedEvent(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            
            changedInput = eventArgs.input
            
            if changedInput.id == 'fillet_cb':
                inputs = eventArgs.firingEvent.sender.commandInputs
                fillet_in = inputs.itemById('fillet_in')
			
            # Change the visibility of the scale value input.
            if changedInput.value == True:
                fillet_in.isVisible = True
            else:
                fillet_in.isVisible = False

        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('MainButton')
        if cmdDef:
            cmdDef.deleteMe()
            
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cntrl = addinsPanel.controls.itemById('MainButton')
        if cntrl:
            cntrl.deleteMe()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))	

 

2 REPLIES 2
Message 2 of 3
marshaltu
in reply to: Six1Seven1

Hello,

 

It looked there was a minor error in your codes. It worked well after I corrected it.

 

Thanks,

Marshal

 

class inputChangedEvent(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            
            changedInput = eventArgs.input
            
            fillet_in = None
            if changedInput.id == 'fillet_cb':
                inputs = eventArgs.firingEvent.sender.commandInputs
                fillet_in = inputs.itemById('fillet_in')
			
            # Change the visibility of the scale value input.
            if fillet_in:
                if changedInput.value == True:
                    fillet_in.isVisible = True
                else:
                    fillet_in.isVisible = False

        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))	                
                
  


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 3
Six1Seven1
in reply to: marshaltu

Marshal,

 

Thank you! Your solution worked great.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report