API: Using Single-Argument Callbacks in Add-ins (C#) for Command Group Items

Version 9

    This example shows how to use one single-argument callback for all of the menu and toolbar commands of a SolidWorks add-in.

      

    1. Download and unzip the attached zip file in a convenient location.
    2. Modify download_directory\RunTimeAddin\RunTimeAddin\bin\Debug\AddinDlls.txt to point to the location of DynamicLoadAddin.dll in your download directory. (This example contains two add-in projects: RunTimeAddin and DynamicLoadAddin. addindlls.txt must reside in the same directory as RunTimeAddin.dll. addindlls.txt contains the path to the DynamicLoadAddin projects DLL which contains all of the behavior of this application.)
    3. Open DynamicLoadAddin project in Visual Studio 2008.
    4. Update the paths to all of the project references.
    5. Delete the obj and bin project folders.
    6. Rebuild the solution.
    7. Open RunTimeAddin project in Visual Studio 2008.
    8. Update the project Properties Debug executable to point to your SolidWorks executable.
    9. Update the paths to all of the project references.
    10. Delete the obj and bin project folders.
    11. Rebuild the solution.
    12. Run RunTimeAddin in Debug mode.

     

    At run-time, the RunTimeAddin loads DynamicLoadAddin.dll, determines by reflection which functions are callbacks, and creates a menu item for each. When the user clicks a component in the UI, a callback is made to the corresponding function in DynamicLoadAddin.dll.

     

    In RunTimeAddin > SwAddin.cs > AddCommandMgr() notice how the callback and enable methods are specified as arguments to ICommandGroup::AddCommandItem2(). The method names are encased in quotes, and both methods take one argument that indicates the purpose of the command item. The type of the argument is not specified, because it is converted to a string by the callback.

     

    The callback and enable methods are implemented in the UI Callbacks region. Notice the single string parameter in each of these methods. These parameters are used by the method to decide which behavior to invoke at runtime. Because the callback and enable methods are parameterized, one callback method and one enable method can handle all of the menu and toolbar callback needs of the add-in.

     

    Note:
    - Subscription Services required for full access.

    - Looking for more API Examples?

     

    Copyright © 2011 Dassault Systèmes SolidWorks Corp. All rights reserved.
    Do not distribute or reproduce without the written consent of Dassault Systèmes SolidWorks Corp.