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: 

ValidateInputs does not validate...

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Le_Bear
708 Views, 9 Replies

ValidateInputs does not validate...

Slowly progressing toward my first addin creation.

for now I have a single commandInputs, to select a sketchLine, and this is working as expected. But I want this selected line to be a construction line, so I am attempting to check if this is he case inside a ValidateInputs event, but does not seem to work, meaning a regular line is OK too...

Here is the code involved:

# Event handler for the commandCreated event.
class onCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        # Verify that a sketch is active.
        app = adsk.core.Application.get()
        if app.activeEditObject.objectType != adsk.fusion.Sketch.classType():
            ui = app.userInterface
            ui.messageBox('A sketch must be active for this command.')
            return False        
        
        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        
        #Get the command
        cmd = eventArgs.command
        # Get the CommandInputs collection associated with the command.
        inputs = cmd.commandInputs
        
        constructionLine = inputs.addSelectionInput('constructionLineUniqueID','Select a Construction Line', 'The line you select will be the Chord of your Airfoil' )
        constructionLine.addSelectionFilter('SketchLines')

        # Connect to the execute event.
        onExecute = SampleCommandExecuteHandler()
        cmd.execute.add(onExecute)
        handlers.append(onExecute)         

#        #Connect to the ValidateInputs Event
        onValidate = SampleCommandValidateInputsHandler()
        cmd.validateInputs.add(onValidate)
        handlers.append(onValidate)
        
class SampleCommandValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.ValidateInputsEventArgs.cast(args)
        inputs = eventArgs.firingEvent.sender.commandInputs        
        
        selLine = inputs.itemById('constructionLineUniqueID') 
                        
        if selLine.objectType == adsk.fusion.SketchLine.classType():
            myLine = adsk.fusion.SketchLine.cast(selLine)
            
        if myLine.isConstruction:
            eventArgs.areInputsValid = True 
            return
        else:
            eventArgs.areInputsValid = False
            return


Checking with the debugger, I can see the objectType for selLine is NOT sketchLine....Guess I have to find the way to make it so?

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
9 REPLIES 9
Message 2 of 10
Le_Bear
in reply to: Le_Bear

I found a solution myself, which is working well, but I have other questions....

Here is the working ValidateInputHandler

 

class SampleCommandValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.ValidateInputsEventArgs.cast(args)
        inputs = eventArgs.firingEvent.sender.commandInputs                
        selLineInput = adsk.core.SelectionCommandInput.cast(inputs.itemById('constructionLineUniqueID'))
        myLine = adsk.fusion.SketchLine.cast(selLineInput.selection(0).entity)
        if myLine.isConstruction:
            eventArgs.areInputsValid = True 
            return
        else:
            eventArgs.areInputsValid = False
            return

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
Message 3 of 10
Le_Bear
in reply to: Le_Bear

Now, the question is:

How can I let the user know that the OK button is disabled because he did not select a construction line? I believe it would be nice to let the user know why his selection does not pass the validation test.

Thanks for any help!
Bernard

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
Message 4 of 10
marshaltu
in reply to: Le_Bear

Hello,

 

One sample you would be able to refer to is "Spur Gear". It shows prompt message at the bottom of dialog when users give invalid inputs.

 

Thanks,

Marshal

 

Prompts.png



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 5 of 10
BrianEkins
in reply to: Le_Bear

What you want to use in this case is the selectionEvent of the Command object.  For the selection input you can only filter using types of objects, but using the selectionEvent you can use any criteria you want for what the user can select.  As the user is moving the mouse over the model to select something the selectionEvent is fired before it is highlighted to the user and you can look at what the mouse is currently over and choose whether it should be made selectable or not.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 10
Le_Bear
in reply to: marshaltu

That's an idea, I will like how this is done. Thanks!

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
Message 7 of 10
Le_Bear
in reply to: BrianEkins

I did try the selectionEvent, but it was not working for me, maybe I had bug. It would be the best if it worked. I will experiment with it again. Thanks!

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
Message 8 of 10
Le_Bear
in reply to: marshaltu

I did use a textBox with an HTML formated text, works well. Thanks, did not thought I could use it this way.

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
Message 9 of 10
BrianEkins
in reply to: Le_Bear

Here's a variation of your code that uses the selection input event to limit the selection to construction lines.  I think it's best to try and control the selection to get it correct the first time rather than let them select anything and then report the problems.

 

# Event handler for the commandCreated event.
class onCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        # Verify that a sketch is active.
        if _app.activeEditObject.objectType != adsk.fusion.Sketch.classType():
            _ui.messageBox('A sketch must be active for this command.')
            return False        
        
        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        
        #Get the command
        cmd = eventArgs.command

        # Get the CommandInputs collection associated with the command.
        inputs = cmd.commandInputs
        
        constructionLine = inputs.addSelectionInput('constructionLineUniqueID','Select a Construction Line', 'The line you select will be the Chord of your Airfoil' )
        constructionLine.addSelectionFilter('SketchLines')

        # Connect to the execute event.
        onExecute = SampleCommandExecuteHandler()
        cmd.execute.add(onExecute)
        _handlers.append(onExecute)         

        # Connect to the Selection event.
        onSelectionEvent = SampleCommandSelectionEventHandler()
        cmd.selectionEvent.add(onSelectionEvent)
        _handlers.append(onSelectionEvent)


# Event handler for the selectionEvent event.
class SampleCommandSelectionEventHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.SelectionEventArgs.cast(args)
    
            skLine = adsk.fusion.SketchLine.cast(eventArgs.selection.entity)
            if skLine.isConstruction:
                eventArgs.isSelectable = True
            else:
                eventArgs.isSelectable = False
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 10 of 10
Le_Bear
in reply to: BrianEkins


@BrianEkinswrote:

Here's a variation of your code that uses the selection input event to limit the selection to construction lines.  I think it's best to try and control the selection to get it correct the first time rather than let them select anything and then report the problems.

 

 

Thanks Brian, not sure what I did wrong when I tried the first time, but tis works perfectly!

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 

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