Create Custom Command Tabs and Menus in SolidWorks

Create Custom Command Tabs and Menus in SolidWorks

To create new menus or command tabs in SolidWorks, you mainly use the properties and member functions of ICommandManager.

Add action to ConnectToSW

Add a member function CreateCommandTab() inside our custom ISwAddin class. This function adds new commands or menus within SolidWorks.

The code for the ConnectToSW() function is shown below:

public bool ConnectToSW(object ThisSW, int Cookie)
{
_swApp = (ISldWorks)ThisSW;
_addinCookie = Cookie;
_swApp.SetAddinCallbackInfo(0, this, _addinCookie);
CreateCommandTab();
return true;
}

Add command item to group

Inside the CreateCommandTab() function, use the member method CreateCommandGroup2() to create an ICommandGroup command group object.

Use the ICommandGroup member method AddCommandItem2() to add commands to the command group. The main code is shown below:

ICommandGroup cmdGroup = cmdMgr.CreateCommandGroup2(groupId, "Snail Add-in", "Snail Add-in Toolset", "ICommandGroup Demo", -1, true, ref cmdGroupErr);

var cmdIndexes = new List();
int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);
int idx1 = cmdGroup.AddCommandItem2("Cmd1",-1, "Export Data to STP", "To STP",0, "OnCommand1", "",1, menuToolbarOption);
cmdIndexes.Add(idx1);

Active the menu or toolbar

After adding commands, configure and activate the command display:

// Configure and activate the command group
cmdGroup.HasToolbar = true;
cmdGroup.HasMenu = true;
cmdGroup.Activate();

Once activated, the menu and toolbar will show the associated commands. However, tabs are the primary UI element in modern versions.

Create command tabe

To create a tab, two objects are required: ICommandTab and ICommandTabBox, along with the member function AddCommands().

// Create new Tab
ICommandTab newTab = cmdMgr.AddCommandTab(docTypes[0], "Snail Add-in");
// Create TabBox
ICommandTabBox cmdBox = newTab.AddCommandTabBox();
// Prepare command ID array
int[] commandIds = new int[cmdIndexes.Count];
int[] textTypes = new int[cmdIndexes.Count];
for (int i = 0; i < cmdIndexes.Count; i++)
{
commandIds[i] = cmdGroup.get_CommandID(cmdIndexes[i]);
textTypes[i] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow;
}
// Add commands (critical step)
bool addResult = cmdBox.AddCommands(commandIds, textTypes);
// Re-activate command group (to ensure changes take effect)
cmdGroup.Activate();

AddCommandItem2 Parameter Explanation

System.int AddCommandItem2(
    System.string Name,             // Name of the item to add to the CommandGroup
    System.int Position,            // Position of the item within the command group. Set to `-1` to append to the end.
    System.string HintString,       // Text shown in the SOLIDWORKS status bar when hovering over the item
    System.string ToolTip,          // Popup tooltip text shown on hover (API documentation is incorrect; this is the label displayed on the Tab)
    System.int ImageListIndex,      // Icon index for this item from the parent command group’s image list
    System.string CallbackFunction, // Function invoked when the item is clicked
    System.string EnableMethod,     // Optional function to control whether the item is enabled
    System.int UserID,              // Custom user-defined command ID; use 0 if unused
    System.int MenuTBOption         // Menu / toolbar option, specifies command item type (button, separator, popup menu, etc., maps to enum `swCommandItemType_e`)
)

Leave a comment

Your email address will not be published.