Hosting SOLIDWORKS commands in menu, toolbar and context menu using SwEx.AddIn

Edit ArticleEdit Article

Defined commands can be hosted in different locations of SOLIDWORKS commands area: command group, which includes menu, toolbar and command tab box (ribbon) as well as in the context menu

Command Group

In order to add command group it is required to call the AddCommandGroup method and pass the enumeration type as a generic parameter.

It is required to provide the void handler function with a parameter of enumerator which will be called by framework when command is clicked.

public override bool OnConnect()
{
    AddCommandGroup<CommandsA_e>(OnCommandsAButtonClick);
    AddCommandGroup<CommandsB_e>(OnCommandsBButtonClick);
    AddCommandGroup<CommandsC_e>(OnCommandsCButtonClick);

    return true;
}

private void OnCommandsAButtonClick(CommandsA_e cmd)
{
    //TODO: handle the button click
}

private void OnCommandsBButtonClick(CommandsB_e cmd)
{
    //TODO: handle the button click
}

private void OnCommandsCButtonClick(CommandsC_e cmd)
{
    //TODO: handle the button click
}

Commands displayed in the SOLIDWORKS menu
Commands displayed in the SOLIDWORKS menu

By default command will be added to menu and toolbar. This behaviour can be changed by assigning the hasMenu boolean parameter of the CommandItemInfoAttribute attribute.

Toolbar

Commands displayed in the SOLIDWORKS toolbar
Commands displayed in the SOLIDWORKS toolbar

By default command will be added to menu and toolbar. This behaviour can be changed by assigning the hasToolbar boolean parameter of the CommandItemInfoAttribute attribute.

Command Tab Box

Commands added to command tab box
Commands added to command tab box

Command item can be added to tab box by setting the showInCmdTabBox parameter of CommandItemInfoAttribute to true for the specific command defined in the enumeration.

textStyle parameter allows to specify the alignment of the hint text relative to the icon.

Text display styles in command tab box
Text display styles in command tab box

  • Icon only (without text) (swCommandTabButton_NoText)
  • Text below icon (swCommandTabButton_TextBelow)
  • Text to the right to icon, aligned horizontally (swCommandTabButton_TextHorizontal)

using CodeStack.SwEx.AddIn.Attributes;
using CodeStack.SwEx.AddIn.Enums;
using SolidWorks.Interop.swconst;

public enum CommandsC_e
{
    [CommandItemInfo(true, true, swWorkspaceTypes_e.Assembly,
        true, swCommandTabButtonTextDisplay_e.swCommandTabButton_NoText)]
    CommandC1,

    [CommandItemInfo(true, true, swWorkspaceTypes_e.AllDocuments,
        true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow)]
    CommandC2,

    [CommandItemInfo(true, true, swWorkspaceTypes_e.AllDocuments,
        true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal)]
    CommandC3,
}

Context Menu

Commands displayed in the context menu
Commands displayed in the context menu

In order to add context menu it is required to call the AddContextMenu method and pass the enumeration as a template parameter.

It is required to provide the void handler function with a parameter of enumeration which will be called by framework when command is clicked.

It is optionally required to specify the selection type of where this menu should be displayed.

public override bool OnConnect()
{
    AddContextMenu<CommandsD_e>(OnCommandsDContextMenuClick);
    AddContextMenu<CommandsE_e>(OnCommandsEContextMenuClick, swSelectType_e.swSelFACES);

    return true;
}

private void OnCommandsDContextMenuClick(CommandsD_e cmd)
{
    //TODO: handle the context menu click
}

private void OnCommandsEContextMenuClick(CommandsE_e cmd)
{
    //TODO: handle the context menu click
}

Product of Xarial Product of Xarial