Adding attributes to the Design empties SelectionCommandInput.selection

Adding attributes to the Design empties SelectionCommandInput.selection

nnikbin
Collaborator Collaborator
1,112 Views
3 Replies
Message 1 of 4

Adding attributes to the Design empties SelectionCommandInput.selection

nnikbin
Collaborator
Collaborator

Adding attributes to the Design causes the SelectionCommandInput.selectionCount to return 0 and so SelectionCommandInput.selection(index) will fail.

 

In the following code block the following line (line 57) causes the selectionCommandInput.selectionCount to return 0:

design.attributes.add('TestGroup', 'TestName', 'TestValue')

 

 

import adsk.core, adsk.fusion, traceback

app = None
ui  = None
selectionCommandInput = None 
commandId = 'AttributesAndSelectionCommandInput'
commandName = 'Attributes and Selection Command Input'
commandDescription = 'Attributes and Selection Command Input Test'

handlers = []

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

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global selectionCommandInput
        try:
            cmd = args.command
            inputs = cmd.commandInputs

            global commandId

            selectionCommandInput = inputs.addSelectionInput(commandId + '_selection', 'Selection', '')
            
            onExecute = MyCommandExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)
            
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)            
            handlers.append(onDestroy)            
            
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            global ui
            global selectionCommandInput
            
            design = adsk.fusion.Design.cast(app.activeProduct)
            #Removing the following line solves the problem
            design.attributes.add('TestGroup', 'TestName', 'TestValue')
            ui.messageBox(str(selectionCommandInput.selectionCount))
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
           
    
def run(context):
    ui = None
    try:
        global app
        app = adsk.core.Application.get()
        global ui
        ui = app.userInterface

        global commandId
        global commandName
        global commandDescription

        cmdDef = ui.commandDefinitions.itemById(commandId)
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription)
        
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)

        cmdDef.execute()
        
        adsk.autoTerminate(False)

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

As a workaround I add the attributes to the Design after getting the selection.

 

Is it a bug or this behavior is 'By Design'?

 

0 Likes
Accepted solutions (1)
1,113 Views
3 Replies
Replies (3)
Message 2 of 4

liujac
Alumni
Alumni
Accepted solution

Hi,

 

It's as designed. Any edit to document will clear all the selections first. So you need to get the entities from the selections before doing any edit operations to the document if you want to use the selected entities later.

 

Regards,

Jack

0 Likes
Message 3 of 4

nnikbin
Collaborator
Collaborator

Hi Jack,

 

Many thanks for your prompt reply!

 

Navid

0 Likes
Message 4 of 4

martinhansdk
Contributor
Contributor

I'm so happy to have found this post. It just cost me a few days to track down this issue.

 

As far as I can tell this is not documented in the API User's Manual or the API Reference Manual. If that is indeed so, maybe it would be worth adding.

 

Martin

0 Likes