Welcome
Collapse Getting StartedGetting Started
Overview
Expand Types of SolidWorks API ApplicationsTypes of SolidWorks API Applications
Expand SolidWorks API Object Model and Class HierarchySolidWorks API Object Model and Class Hierarchy
Collapse Programming with the SolidWorks APIProgramming with the SolidWorks API
Collapse Add-insAdd-ins
Callbacks
Icons
Shortcut Menus
Toolbars
API Dependent on SolidWorks Being Visible
Arrays
Attributes Imported from ACIS SAT Files
Bitmasks
Block Definitions and Block Instances
Bodies in Body Folders
BOOL and VARIANT_BOOL
Bounding Box and Note Text
COM vs. Dispatch
CommandManager and CommandGroups
Components, Configurations, and Suppression States
Compound Note
ComVisibleAttribute in VSTA macros
Controls, Visual Basic 6.0, and Windows XP
Expand Double Arrays and Integer PairsDouble Arrays and Integer Pairs
Drawing Views and Model Entities
Early and Late Binding
Expand EventsEvents
Features of Components
Helper Functions
Hole Wizard Features and Objects
IDispatch Object Arrays as Input in .NET
Implementation Guidelines
Import and Export File Options
In-process Methods
Instantiate ActiveX Controls as Tabs
Interface Pointers
Keystrokes and Accelerator Keys
Library Features and Objects
Lightweight Components
Line Attributes for View::GetPolyLinesAndCurves
Line Weights
Long vs. Integer
Expand Macro FeaturesMacro Features
Manipulators
Mass Properties
Expand Multibody PartsMultibody Parts
Option Explicit Statement
Partition Rollback and API Handles
Persistent Reference IDs
Presentation Transforms
Expand PropertyManager PagesPropertyManager Pages
.NET Interop Assemblies
Quick Tips and Bubble ToolTips
Return Values
Expand SafeArraysSafeArrays
Selection Criteria
Selection Lists
Selections that Define Features
Setup Project to Distribute SolidWorks Add-in
Smart Pointers
SolidWorks Objects
Sorting Tables
Splines
SQLite
STL Container Classes and Smart Pointers
Suspend Automatic Rebuilds
System Options and Document Properties
Tessellation and Edges
Tracking IDs
Expand Type LibrariesType Libraries
Units
Unmanaged C++ and C++/CLI Code
VBA and SolidWorks x64
Examples and Projects
Expand SOLIDWORKS API HelpSOLIDWORKS API Help
Expand SOLIDWORKS Enterprise PDM API HelpSOLIDWORKS Enterprise PDM API Help
Expand FeatureWorks API HelpFeatureWorks API Help
Expand SOLIDWORKS Costing API HelpSOLIDWORKS Costing API Help
Expand SOLIDWORKS Document Manager API HelpSOLIDWORKS Document Manager API Help
Expand SOLIDWORKS Routing API HelpSOLIDWORKS Routing API Help
Expand SOLIDWORKS Simulation API HelpSOLIDWORKS Simulation API Help
Expand SOLIDWORKS Sustainability API HelpSOLIDWORKS Sustainability API Help
Expand SOLIDWORKS Toolbox API HelpSOLIDWORKS Toolbox API Help
Expand SOLIDWORKS Utilities API HelpSOLIDWORKS Utilities API Help
Expand SOLIDWORKS Workgroup PDM API HelpSOLIDWORKS Workgroup PDM API Help
Expand eDrawings API HelpeDrawings API Help
Hide Table of Contents

Add-in Callback and Enable Methods

There are two possible signatures for the callback and enable methods of menu items and toolbar commands of SolidWorks add-ins:

  • No parameters

  • One string parameter (supported in SolidWorks 2012 SP0 and later)

No Parameters in the Callback or Enable Method

Implement one callback method and one enable method for each menu item and toolbar command in the add-in. These callback and enable methods take no arguments.

Example:

cmdIndex0 = cmdGroup.AddCommandItem2("CreateCube", -1, "Create a cube", "Create cube", 0, "CreateCube", "EnableCube", mainItemID1, menuToolbarOption);

cmdIndex1 = cmdGroup.AddCommandItem2("Show PMP", -1, "Display sample property manager", "Show PMP", 2, "ShowPMP", "EnablePMP", mainItemID2, menuToolbarOption);

 

#Region UI Callbacks

public void CreateCube

{

    // Create Cube command code

}

public int EnableCube

{

    // Return status of the Create Cube enablement

    return 0;

}

public void ShowPMP

{

    // Show PMP command code

}

public int EnablePMP

{

    // Return status of Show PMP enablement

    return 0;

}

In the example above, one callback method and one enable method are implemented for each command item. These methods are implemented somewhere else in the add-in, usually in the UI Callback region.

One String Parameter in the Callback or Enable Method

Implement one callback method and one enable method, each having one string parameter. Do this when you want to use the same callback/enable method for all of your menu items and toolbar commands. The argument in the method call indicates which menu item or toolbar command behavior to invoke.

Example:

// Create command items, passing to the callback and enable methods a number indicating which menu item or toolbar command

    cmdIndex0 = cmdGroup.AddCommandItem2("CreateCube", -1, "Create a cube", "Create cube", 0, "CallBackFunction(1)", "EnableFunction(1)", mainItemID1, menuToolbarOption);

    cmdIndex1 = cmdGroup.AddCommandItem2("Show PMP", -1, "Display PMP", "Show PMP", 2, "CallBackFunction(2)", "EnableFunction(2)", mainItemID2, menuToolbarOption);

 

#Region UI Callbacks       

        // Single-parameter callback method

        public void CallBackFunction(string data)

        {

            int commandType = int.Parse(data);

            switch (commandType)

            {

                case 1:

                    // Create cube command code

                    break;

                case 2:

                    // Show PMP command code

                    break;

                default:

                    break;

            }

        }

 

        // Single-parameter enable method

        public int EnableFunction(string data)

        {

            int commandType = int.Parse(data);

            int arg;

            switch (commandType)

            {

                case 1:

                    // Return status of Create Cube enablement

                    arg = 0;

                    break;

                case 2:

                    // Return status of Show PMP enablement

                    arg = 1

                    break;

                default:

                    arg = 0

                    break;

            }

            return arg;

        }

 

In the example above, the same callback and enable methods are used for multiple menu items. The parameter in the callback and enable methods differentiates which behavior to invoke. The callback and enable method parameters must be of type string.

Download a complete add-in example from the SolidWorks API forum. You must be a SolidWorks subscription customer to access the SolidWorks API forum documents:

  1. Open https://forum.solidworks.com/welcome.

  2. Log in.

  3. Click API.

  4. Click API Examples.

  5. Locate and download Using Single-Argument Callbacks in SolidWorks Add-ins.

 

To learn more about add-ins and their menu items and toolbars:

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Add-in Callback and Enable Methods
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:




x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.