ValidateInputs does not validate...

ValidateInputs does not validate...

Le_Bear
Collaborator Collaborator
1,731 Views
9 Replies
Message 1 of 10

ValidateInputs does not validate...

Le_Bear
Collaborator
Collaborator

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
 
0 Likes
Accepted solutions (3)
1,732 Views
9 Replies
Replies (9)
Message 2 of 10

Le_Bear
Collaborator
Collaborator
Accepted solution

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
 
0 Likes
Message 3 of 10

Le_Bear
Collaborator
Collaborator

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
 
0 Likes
Message 4 of 10

marshaltu
Autodesk
Autodesk
Accepted solution

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 Developer
>
Message 5 of 10

BrianEkins
Mentor
Mentor

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
0 Likes
Message 6 of 10

Le_Bear
Collaborator
Collaborator

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

Bernard Grosperrin

Autodesk Certified Instructor

FaceBook Group | Forum | YouTube

Group Network Leader
 
0 Likes
Message 7 of 10

Le_Bear
Collaborator
Collaborator

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
 
0 Likes
Message 8 of 10

Le_Bear
Collaborator
Collaborator

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
 
0 Likes
Message 9 of 10

BrianEkins
Mentor
Mentor
Accepted solution

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
0 Likes
Message 10 of 10

Le_Bear
Collaborator
Collaborator

@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
 
0 Likes