Custom enable command state for SOLIDWORKS commands
There are 4 command states supported by SOLIDWORKS:
- Deselected and enabled. This is default option when button can be clicked
- Deselected and disabled. This option is used when the command is not supported in certain framework. For example mate command will be disabled in parts and drawings as it is only supported in the assemblies.
- Selected and disabled. This represents the disabled checked button
- Selected and enabled. This represents checked button
xCAD.NET framework will assign the appropriate state (enabled or disabled) for the commands based on their supported workspaces if defined in the CommandItemInfoAttribute. However user can alter the state to provide more advanced management (for example it might be required to enable command if certain object is selected or if any bodies or components are present in the model). To do this it is required to specify to subscribe to IXCommandGroup::CommandStateResolve event. IXCommandGroup is created as the result of calling AddCommandGroup or AddContextMenu methods.
The value of state will be preassigned based on the workspace and can be changed by the user within the method.
This method allows to implement the toggle button in toolbar and menu. To set the checked state use the Checked.
public enum Commands_e { Command1, Command2 } public override void OnConnect() { this.CommandManager.AddCommandGroup<Commands_e>().CommandStateResolve += OnButtonEnable; } private void OnButtonEnable(Commands_e cmd, CommandState state) { switch (cmd) { case Commands_e.Command1: case Commands_e.Command2: //implement logic to identify the state of the button state.Checked = false; state.Enabled = false; break; } }
Refer Toggle Command Example for the demonstration of how to achieve check box effect for toolbar button in SOLIDWORKS using the command states.