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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selection Count Bug in API After Creating Sketch

Message 1 of 3
211 Views, 2 Replies

Selection Count Bug in API After Creating Sketch

I am working on a python add in that will project user selected edges to a sketch on a user selected face, however creating a sketch on said face seems to wipe out the values in my edge selection. You can see my stripped down test code below which I have been testing with a simple cube.


It seems like this issue came about with the latest updates to Fusion as this was working for me earlier this week.


# This event handler is called when the user clicks the OK button in the command dialog or 
# is immediately called after the created event not command inputs were created for the dialog.
def command_execute(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')

    # Get a reference to your command's inputs.
    inputs = args.command.commandInputs
    edges = inputs.itemById('edge_Selection') # User selected edges from selectionInput
    top_face = inputs.itemById('face_Selection').selection(0).entity # User selected face from selectionInput

    # Output edge count to the log
    futil.log(f'Edges Qty: {edges.selectionCount}')

    # Create collection of edges
    edge_count1 = edges.selectionCount
    futil.log(f'Edge Count Variable 1= {edge_count1}')
    futil.log(f'Edge Selection Count = {edges.selectionCount}')

    # Create sketch on top surface plane to house outline profile
    sk_outline = root.sketches.addWithoutEdges(top_face) = 'Outline Sketch'

    # DEBUG Check edge count again
    edge_count2 = edges.selectionCount
    futil.log(f'Edge Count Variable 1 = {edge_count1}')
    futil.log(f'Edge Count Variable 2 = {edge_count2}')
    futil.log(f'Edge Selection Count = {edges.selectionCount}')

    # Get all edges from edges selection input
    edge_coll = adsk.core.ObjectCollection.create()
    i = 0
    edge_count3 = edges.selectionCount
    futil.log(f'Edges Qty = {edges.selectionCount}')
    futil.log(f'Edge Count Variable 3 = {edge_count3}')
    while i < edge_count1:
        i += 1


Labels (2)
Message 2 of 3
in reply to: aldenT3XVX

Selections are fragile, and typically, creating or editing something will clear the current selections. I'm pretty sure that's what is happening in your case. You need to save the entities from the selections into variables and then start using them.

Brian Ekins
Inventor and Fusion 360 API Expert
Message 3 of 3
in reply to: BrianEkins

Thanks for the response Brian, you seem to be correct. Storing those entities at the start of my script allows me to access them further in as I'd expect.

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

Post to forums  

Technology Administrators

Autodesk Design & Make Report