API for working with Selection Sets

API for working with Selection Sets

nnikbin
Collaborator Collaborator
4,459 Views
8 Replies
Message 1 of 9

API for working with Selection Sets

nnikbin
Collaborator
Collaborator

I checked the documentation to find a way to create Selection Sets. It looks there isn't any API for working with them (checking, creating, deleting, etc). I think it will be useful to have such API. I was wondering whether Autodesk has any plan to expose Selection Sets to Fusion 360 API.

Accepted solutions (1)
4,460 Views
8 Replies
Replies (8)
Message 2 of 9

BrianEkins
Mentor
Mentor

In Fusion, there is essentially one selection set.  In Fusion, when no other command is running the Select command is running.  A command is always running, either the Selection command or some other command. When the Select command is running and you select entities they're being added to the current selection (select set). 

 

The API lets you interact with the set of entities currently selected, when the Select command is running. You use the activeSelections property of the UserInterface object.  This returns a Selections object which is a collection that contains everything that's currently selected.  Using functions on the Selections object, you can add and remove entities and clear the entire set.  The UserInterface object is accessed through the userInterface property on the Application object.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 9

nnikbin
Collaborator
Collaborator

Thank you @BrianEkins for your reply. What I am looking for is creating Selection Sets that are listed and managed in the browser.

 

Suppose I want to create a BRepBody and as by-products create Selection Sets to categorize created BRepFaces (Following picture). Or suppose I want to create bunch of objects and as by-products create a Selection Set for bolts, another for nuts, etc.

 

16.png

 

0 Likes
Message 4 of 9

kandennti
Mentor
Mentor
Accepted solution

Hi nnikbin.

 

Very limited but I found a way.

First, create multiple bodies in the root component.
1.png

Then, execute the next script.

#fusion360 python
#Test Create SelectionSets
import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent
        
        #Select the first body
        ui.activeSelections.add(root.bRepBodies.item(0))
        
        #Create SelectionSets
        cmd = ui.commandDefinitions.itemById('CreateSelectionGroupCmd')
        cmd.execute()
        
        #Update SelectionSets
        cmd = ui.commandDefinitions.itemById('UpdateSelectionGroupCmd')
        cmd.execute()
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

After execution, a SelectionSets is created to select the first Body.

2.png


It seems that it is possible to call many commands of Fusion360 if doing it this way.
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-d34065c0-b5b8-46f9-bd4c-36adfcdfda83

 

 

The executable command ID list can be written to a file by executing the following code.

#fusion360 python
#CommandID Write to File
import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        cmds = [cmd.id for cmd in ui.commandDefinitions]
        path = r'C:\temp\fusion360_commands_list.txt'
        output = open(path, 'w')
        output.writelines('\n'.join(cmds))
        output.close()        
        
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Message 5 of 9

nnikbin
Collaborator
Collaborator

Wonderful! Thank you @kandennti !

0 Likes
Message 6 of 9

Anonymous
Not applicable

To the Autodesk employees looking at this issue (if any): this is not resolved! The way of doing this is really ugly, and you can't cleanly access existing selection sets or anything like that. 

 

Overall the API support for automating the user interface is almost nonexistent. Just look at the selection filters in the main window for example: there is no API for changing those outside of a command context but yet I can see a load of things I would use that API for. Obviously there is internal API, otherwise it wouldn't be usable at all. Please release these things (or if you really don't want to do this, release debug symbols). 

 

Please add actual API for selection sets OR implement temporary view groups like Revit has. I'll take either because they will have the same result, but the former is probably easier for you. 

 

Sincerely,

A frustrated modeler trying to improve your UI

Message 7 of 9

Anonymous
Not applicable

Amazing solution! Wondering, do you know if there is any guide on what exactly all the commands that are saved in .txt   file do?

Message 8 of 9

kandennti
Mentor
Mentor

Hi @Anonymous .

 

I'm sorry, I didn't understand the meaning.

The method using "executeTextCommand" described here is better than the method described above.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/unexpected-behavior-with-ui-activeselections/m-p/9520878#M10185 

 

How to use TextCommands is summarized here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/td-p/9645688 

 

Message 9 of 9

therealsamchaney
Advocate
Advocate

I agree with others that the syntax for working with selections is a little counter-intuitive and that the documentation for interacting with them is sorely lacking. At least just one little example script would be very helpful. For practical purposes, most of the time you're just going to need to get the set of what's currently selected which would be ui.activeSelections. I made this little example script excerpt below to show how you could do it.

import adsk.core

app = adsk.core.Application.get()
ui = app.userInterface
selections = ui.activeSelections

# Now you can interact with our selections object like this:
active_sketch = adsk.core.Application.get().activeEditObject
curves = active_sketch.sketchCurves
for curve in curves: # Walk through all the curves in the current sketch 
    selections.add(curve) # Add each of the curves to the selections

 
This example would get the set of currently selected entities, then add all of the curves of the current sketch to it, effectively selecting all curves in the active sketch. Hope this is helpful!

0 Likes