Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

UserParameters.add causing SelectionCommandInput.selection to be empty

mcd8604
Enthusiast

UserParameters.add causing SelectionCommandInput.selection to be empty

mcd8604
Enthusiast
Enthusiast

I created a command script that will generate multiple offsets from a single-loop Profile.

The command inputs are:

  1. A SelectionInput filtered for 1 Profile
  2. An IntegerSpinnerCommandInput to specify the number of sketch curves to generate.
  3. A DistanceValueCommandInput to specify the total offset distance to span.

I decided to add a userParameter for the totalOffsetDistance so that I could make adjustments. Adding this line of code..

 

design.userParameters.add('totalOffsetDistance', ValueInput.createByReal(totalOffsetDistance), "cm", "")

 

to the execute handler gives two different behaviors based on whether it is before or after this (assuming there is exactly 1 valid Profile selected).

 

profileSelectionCommand:SelectionCommandInput = inputs.itemById('profileSelection')
profileSelection:Selection = profileSelectionCommand.selection(0)

 

If the UserParameter is added before the selection is gotten, it we get an invalid index and we can test to see that the selectionCount is indeed 0. However, if we move that line afterwards, we are able to retrieve the selected Profile just as normal. It's not hard to work around but it is undocumented/unregulated and perhaps unintended behavior from a software engineering perspective.

0 Likes
Reply
Accepted solutions (1)
471 Views
3 Replies
Replies (3)

Bowen.Christopher58NJL
Explorer
Explorer

Interesting, nice discovery 

0 Likes

BrianEkins
Mentor
Mentor
Accepted solution

Selections are somewhat fragile and it's by design that when you perform an operation that makes a change to persistent data that the current selections are cleared.  To work around this you can get the entities currently selected and save them in a variable and then do your work using these saved references.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
2 Likes

mcd8604
Enthusiast
Enthusiast

I see. Thanks for the clarification! I ended up creating a utility method to make this easier:

 

def GetSelections(sci:SelectionCommandInput):
    return [sci.selection(i) for i in range(sci.selectionCount)]

 

I just have to make sure to remember to cache references prior to making any changes to persistent data.

0 Likes