Custom enable command state for SOLIDWORKS commands


There are 4 command states supported by SOLIDWORKS:

  1. Deselected and enabled. This is default option when button can be clicked
  2. 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.
  3. Selected and disabled. This represents the disabled checked button
  4. Selected and enabled. This represents checked button

Supported command states
Supported command states

SwEx 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 the enable method handler as the last parameter of AddCommandGroup or AddContextMenu methods.

The method is defined as EnableMethodDelegate delegate and provides the command id as first parameter and state passed by reference as second parameter.

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 assign the SelectEnable or SelectDisable values of CommandItemEnableState_e enumeration.

public enum Commands_e

public override bool OnConnect()
    AddCommandGroup<Commands_e>(OnButtonClick, OnButtonEnable);
    return true;

private void OnButtonEnable(Commands_e cmd, ref CommandItemEnableState_e state)
    switch (cmd)
        case Commands_e.Command1:
        case Commands_e.Command2:
            //TODO: implement logic to identify the state of the button
            state = CommandItemEnableState_e.DeselectDisable;

Product of Xarial Product of Xarial