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

Unexpected behavior with ui.activeSelections

3 REPLIES 3
Reply
Message 1 of 4
rmiglioriEKDK2
605 Views, 3 Replies

Unexpected behavior with ui.activeSelections

Hi,

I'm trying to write a script that does essentially the following: 

 

  • Find bodies inside components.
  • Select them.
  • Create a selection set from the selected bodies. 
  • Deselect them
  • Select other bodies.
  • Create a selection set from these selected bodies.
  • Deselect them.
  • Repeat 

I've managed to write a script that successfully finds bodies inside a design, select them, and then is supposed to create a selection set from them. However, when I run

ui.activeSelections.clear()

to clear the selections, the selection set created by the script is empty. 

 

I don't really get it, and I'm kind of at a loss as to what to do. Here's the script: 

 

#Author-
#Description-

# https://forums.autodesk.com/t5/fusion-360-api-and-scripts/api-for-working-with-selection-sets/td-p/8837150

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

# Produces empty selection set when it should produce a selection set containing selected bodies then clear the selection set. 
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        design = app.activeProduct
        rootComponent = design.rootComponent        
        
        # Add all bodies in components to acrtive selections 
        allComponents = rootComponent.allOccurrences        
        for component in allComponents:  
        	componentName = component.name.split(':')[0]
        	bRepBodies = component.bRepBodies
        	for body in bRepBodies:
        		ui.activeSelections.add(body)

        # Create selection set 
        cmd = ui.commandDefinitions.itemById('CreateSelectionGroupCmd')
        cmd.execute()      
        # Clear selections 
        ui.activeSelections.clear()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I made a screencast of the script creating an empty selection set. Here's a direct link in case it doesn't work: https://autode.sk/36elSyz

 

And I've also attached the test design illustrating the problem. 

 

Thanks for the help, and I'm happy to answer any questions! 

 

 

3 REPLIES 3
Message 2 of 4
kandennti
in reply to: rmiglioriEKDK2

Hi rmiglioriEKDK2.

 

At the end of the command, I felt that the selection set was completed, so I created the following code.
However, only an empty selection set was made.
Since it is not provided by API, it seems impossible.

_app = None
_ui  = None
_handlers = []

class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = _ui.commandDefinitions.itemById('CreateSelectionGroupCmd')
            cmd.execute()
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = adsk.core.Command.cast(args.command)

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)

            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        cmdDef = _ui.commandDefinitions.itemById('test')
        if cmdDef:
            cmdDef.deleteMe()
        cmdDef = _ui.commandDefinitions.addButtonDefinition('test', 'test', 'test')

        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        des = _app.activeProduct
        root = des.rootComponent
    
        occs = root.allOccurrences

        for occ in occs:
            bRepBodies = occ.bRepBodies
            for body in bRepBodies:
                _ui.activeSelections.add(body)

            cmdDef.execute()
            _ui.activeSelections.clear()
except: if _ui: _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 3 of 4
rmiglioriEKDK2
in reply to: kandennti

So even your attempt creates an empty selection set? Wow. I really wonder what is going on that clearing active selections executes with a higher priority than anything else. 

 

Sorry to take this in another direction, but I have another question. How could I run one script from another script? Is that possible to do ? 

Message 4 of 4
kandennti
in reply to: rmiglioriEKDK2

I'm sorry to bring up the old story.


With Ver2.0.8335, I found that this method can be used for processing.

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

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        txtCmd :str = 'NuComponents.CreateSelectionGroupCmd'
        actSels :adsk.core.Selections = _ui.activeSelections
        for occ in root.allOccurrences:
            bRepBodies = occ.bRepBodies
            actSels.clear()
            [actSels.add(body) for body in bRepBodies]
            _app.executeTextCommand(txtCmd)

        actSels.clear()
        _ui.messageBox('done')
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

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

Post to forums  

Autodesk Design & Make Report