Bug? UserInterface.selectEntity should return None but fails

Bug? UserInterface.selectEntity should return None but fails

JeromeBriot
Mentor Mentor
1,044 Views
4 Replies
Message 1 of 5

Bug? UserInterface.selectEntity should return None but fails

JeromeBriot
Mentor
Mentor

Hello,

 

According to the documentation, the UserInterface.selectEntity method returns None if the selection is aborted.

 

I guess that the selection is aborted by pressing the Esc key. But in that case, the method returns an error message:

fusion-360-api-selectEntity-failed.png

Here is a code to reproduce the issue:

import adsk.core, adsk.fusion, adsk.cam, traceback

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

        design = app.activeProduct

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)

        # Draw some circles.
        circles = sketch.sketchCurves.sketchCircles
        circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2)
        circle2 = circles.addByCenterRadius(adsk.core.Point3D.create(8, 3, 0), 3)

        # Add a circle at the center of one of the existing circles.
        circle3 = circles.addByCenterRadius(circle2.centerSketchPoint, 4)

        selection = ui.selectEntity('Select a profile', 'Profiles')

        if not selection:
            ui.messageBox('No profile selected')
        else:
            ui.messageBox('One profile selected')

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

Thanks.

0 Likes
1,045 Views
4 Replies
Replies (4)
Message 2 of 5

dinocoglitore
Advocate
Advocate

I found the same problem with the selection of a Body. 

bodySel = ui.selectEntity('Select a body''Bodies')
        if bodySel:
            body = adsk.fusion.BRepBody.cast(bodySel.entity)
        elif bodySel == None:
            ui.messageBox("Selection not correct")
            exit
 
If I look at the bodySel variable inside Debug Console this is the answer:
variable.JPG
Thanks
Dino
0 Likes
Message 3 of 5

kandennti
Mentor
Mentor

Hi .

 

This has been happening for a long time.
I always respond using try-except.

 

・・・
        # Add a circle at the center of one of the existing circles.
        circle3 = circles.addByCenterRadius(circle2.centerSketchPoint, 4)

        selection = selectEnt(ui, 'Select a profile', 'Profiles')

        if selection is None:
            ui.messageBox('No profile selected')
        else:
            ui.messageBox('One profile selected')

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

def selectEnt(ui, msg, filtter_str):
    try:
        selection = ui.selectEntity(msg, filtter_str)

        return selection
    except:
        return None
Message 4 of 5

JeromeBriot
Mentor
Mentor

Thank you@kandennti 

This workaround works perfectly!

Message 5 of 5

dinocoglitore
Advocate
Advocate

Thanks a lot.

Dino

 

0 Likes