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

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

JeromeBriot
Mentor Mentor
720 Views
5 Replies
Message 1 of 6

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

JeromeBriot
Mentor
Mentor

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

 

 

 

721 Views
5 Replies
Replies (5)
Message 2 of 6

BrianEkins
Mentor
Mentor

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
Mentor
Mentor

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

 

0 Likes
Message 4 of 6

MichaelT_123
Advisor
Advisor

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
Collaborator
Collaborator

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
Mentor
Mentor

@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')

 

 

0 Likes