checkBoxCommandDefinition

checkBoxCommandDefinition

j.han97
Advocate Advocate
772 Views
4 Replies
Message 1 of 5

checkBoxCommandDefinition

j.han97
Advocate
Advocate

Hi all, 

 

I realized that I can add a check box command definition using 

 

commandDefinitions.addCheckBoxDefinition()

 

However, I found little information in reference manual regarding this to answer my questions:

 

1. How to differentiate the state change? During my testing, I connected it with a commandCreatedEventHandler, and it is executed every time I check/un-check the check box.

 

2. Can I pin it to toolbar? I have set 

 

control.isVisible = True; control.isPromoted = True; control.isPromotedByDefault = True

 

 but the check box is not promoted to the toolbar panel.

 

3. Is it possible to set an icon to this checkbox? Assuming the icon has two states, which correspond to two states of a check box (enabled, disabled). I have tried setting the resource folder of the command definition using 

 

#cmdDef: adsk.core.CommandDefinition
cmdDef.resourceFolder = <path_to_resource_folder>

 

but the icons are not applied.

 

The ultimate objective is to implement a 'state' button, which users can use it to switch between two views (one default view, another one with custom graphics groups). Please advice if this is possible to do in Fusion 360.

 

Any help is appreciated!

 

A sample code to demonstrate the problem is presented below. A resource folder with the icons is available in the attachment.

 

#Author-
#Description-

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

class checkBox_commandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    
    def notify(self, args):
        _ui.messageBox('Check box clicked')

_app = adsk.core.Application.get()
_ui = _app.userInterface
_handlers = []

def run(context):
    try:
        #Get command definitions & design workspace
        cmd_def = _ui.commandDefinitions
        des_wp = _ui.workspaces.itemById('FusionSolidEnvironment')

        #Get tools tab
        tools_tab = des_wp.toolbarTabs.itemById('ToolsTab')
        
        #Get add-in panel
        addin_panel = tools_tab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
        checkBox_cmdDef = cmd_def.itemById('checkBox')
        if checkBox_cmdDef: checkBox_cmdDef.deleteMe()
        checkBox_cmdDef = cmd_def.addCheckBoxDefinition('checkBox', 'Check box', 'Test check box', True)
        checkBox_cmdDef.resourceFoler = './resources/checkBox'
        checkBox_ctrl = addin_panel.controls.itemById('checkBox')
        if checkBox_ctrl: checkBox_ctrl.deleteMe()
        checkBox_ctrl = addin_panel.controls.addCommand(checkBox_cmdDef)
        
        checkBox_ctrl.isVisible = True; checkBox_ctrl.isPromoted = True; checkBox_ctrl.isPromotedByDefault = True
        
        checkBox_commandCreated = checkBox_commandCreatedEventHandler()
        checkBox_cmdDef.commandCreated.add(checkBox_commandCreated)
        _handlers.append(checkBox_commandCreated)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
            
def stop(context):
    try:
        #Clean up the control & command definiton
        cmd_def = _ui.commandDefinitions
        des_wp = _ui.workspaces.itemById('FusionSolidEnvironment')
        tools_tab = des_wp.toolbarTabs.itemById('ToolsTab')
        addin_panel = tools_tab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
        checkBox_ctrl = addin_panel.controls.itemById('checkBox')
        if checkBox_ctrl: checkBox_ctrl.deleteMe()
        checkBox_cmdDef = cmd_def.itemById('checkBox')
        if checkBox_cmdDef: checkBox_cmdDef.deleteMe()

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

 

Accepted solutions (1)
773 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor

Hi @j.han97 .

 

I haven't used it, but it looks the same as the selection filters.

1.png

Perhaps the checkboxes felt like resource folders were not available.

0 Likes
Message 3 of 5

j.han97
Advocate
Advocate

Hi @kandennti ,

 

Seems like the checkBoxCommandDefinition has very limited functionality. If it cannot be pinned to toolbar panels, it is difficult to be used as view toggle.

 

But your screencap reminded me the existence of Component Color Cycling Toggle which is very similar to what I was planning to do. The downside is that users cannot determine the current state by the appearance of the button but I guess that is not too important.

 

I will give up on this checkBoxCommandDefinition and continue to use regular buttonCommandDefinition instead. Thank you for your help!

0 Likes
Message 4 of 5

tykapl.breuil
Advocate
Advocate

Checkbox definitions only seem to be able to do what your addin already does, you can just create a regular button whose behavior you change as you click on it (or two buttons) and deal with the isChecked part directly in python.

0 Likes
Message 5 of 5

j.han97
Advocate
Advocate
Accepted solution

I will write a short summary and pin this as solution so that others who are interested in this topic can understand the situation quickly.

 

Objective: Create a view toggle

Attempt: Use checkBoxCommandDefinition to add a state button on toolbar panels

Reason of failure: Limited functionality of checkBoxCommandDefinition (cannot set icon, cannot pin to panel)

Workaround: Take Component Color Cycling Toggle as reference, create a regular buttonDefinition and a global variable to control the state (boolean if two states only)

0 Likes