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: 

[Bug] UserInterface.selectEntity returns errors instead of null when selection is aborted

5 REPLIES 5
Reply
Message 1 of 6
JeromeBriot
302 Views, 5 Replies

[Bug] UserInterface.selectEntity returns errors instead of null when selection is aborted

Hello,

 

The following code returns an error message if the selection is aborted using the Esc key: "InternalValidationError : selections.size() > 0" in adsk/core.py at line 16844.

 

import adsk.core, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        selection = ui.selectEntity('Select a B-Rep body', 'SolidBodies')

        if selection:

            pass

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

 

 

 

5 REPLIES 5
Message 2 of 6
BrianEkins
in reply to: JeromeBriot

You're correct that this is a bug in that the documentation says it should return null if the selection is aborted. This opens the question though of how should this be fixed.  Ideally, the function would be fixed to return null because handling null is the cleanest use of the function.  However, fixing it will change the behavior and would likely cause some existing programs to break because they're depending on the current assert.   The safest "fix" for this is to change the documentation to match the existing behavior.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 6
JeromeBriot
in reply to: BrianEkins

Here is a workaround:

import adsk.core, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        try:
            selection = ui.selectEntity('Select a B-Rep body', 'SolidBodies')
        except:
            selection = None

        if selection:
            ui.messageBox('B-Rep body selected')

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

 

Message 4 of 6
MichaelT_123
in reply to: JeromeBriot

Hi Mr JeromeBriot,

 

The type of problem you have pinpointed is persistent across other "functions/properties return" dilemmas.

It takes some time to realize that one must be careful and prudently manoeuvre between respective traps (as per your code example).

I have encountered quite a few cases that functions/properties that did not return a null pointer. However, I was not too brave (like you,... RESPECT) to report them, just have had bypassed them.

Still, being backed by such a courageous individual like you, I would like to bring to attention of the TF360 behaviour of many

xxxFeature = xxxFeatures.add( featureInput)

In the case of an unsuccessful operation, one would expect null/None pointer as a return.

Such a preferable outcome is also referred to in the documentation. It is not always the case. I still haven't tasted all of F360 features!... so let's call only one example here:

splitBodyFeat = splitBodyFeats.add(splitBodyInput)

The failed operation triggers exception instead of returning null/None pointer as per the documentation:

SplitBodyFeature

Returns the newly created SplitBodyFeature object or null if the creation failed.

 

Regards

MichaelT

 

MichaelT
Message 5 of 6
nnikbin
in reply to: JeromeBriot

I think it is worthwhile to mention that in C++ UserInterface.selectEntity Method returns nullptr when selection is aborted. It is in accordance with the way C++ implementation of the Fusion 360 API handles errors.

Message 6 of 6
JeromeBriot
in reply to: BrianEkins


@BrianEkins wrote:

However, fixing it will change the behavior and would likely cause some existing programs to break because they're depending on the current assert.   The safest "fix" for this is to change the documentation to match the existing behavior.


Hello,

 

Add an optional argument like the useNewWebBrowser parameter in palettes.add

And set it to False by default so existing codes won't be affected by this modification.

 

selection = ui.selectEntity('Select a B-Rep body', 'SolidBodies', 'useNewVersion')

 

 

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

Post to forums  

Technology Administrators