CAD+ Toolset | Utilities for SOLIDWORKS automation
SOLIDWORKS utilities for improving your productivity in SOLIDWORKS. Automate properties, geometry, export and much more

Demo Download Buy

Configuring toggles in Toolbar+


Toggle button in SOLIDWORKS toolbar
Toggle button in SOLIDWORKS toolbar

Toggle behavior can be assigned to a button or menu command, allowing it to switch between checked and unchecked states.

To activate toggle behavior, select the Toggle Button option from the triggers drop-down.

Note that the same macro is run whether the toggle button is checked or unchecked. The macro itself needs to determine the current state to run the appropriate portion of the code.

Configuration

Once Toggle Button option is selected in the triggers, the following options can be configured.

Settings of the toggle button
Settings of the toggle button

  1. Toggle Button State Expression - enable the custom code to resolve the state of the button
  2. Toggle button state resolve code. Code which calculates the current state of the button. This code will be executed every time to resolve the state of the button.

Note, this code will be run every time state of SOLIDWORKS changes, e.g. mouse hover, selection, rebuild, i.e. potentially thousands of times per session. So it is only recommended to use simple code to avoid performance issues or use the Cache State Value option

  1. Cache State Value - if this option is checked, the resolve code runs only once at startup to determine the initial state of the button (checked/unchecked). After that, the toggle state is switched on every click without calling the resolve code.

Custom Code Resolver

When specifying the code to resolve the state, you need to complete the body of a function that returns a boolean, where true corresponds to the checked state and false to unchecked.

The code should be written in C#.

The function provides access to the top-level application (ISwApplication from the xCAD.NET framework). Use late binding to access the underlying SOLIDWORKS API objects.

Example

This example demonstrates how to create a toggle button that switches the Input dimension value setting on and off. The macro should correctly display the button state as checked or unchecked depending on the state of this user preference.

Input dimension value settings in SOLIDWORKS
Input dimension value settings in SOLIDWORKS

To find the current value of this setting, call the ISldWorks::GetUserPreferenceToggle API and pass the value of swInputDimValOnCreate.

As late binding is required, the corresponding integer value of the enumeration must be found:

swInputDimValOnCreate enumeration constant
swInputDimValOnCreate enumeration constant

Adding the following code synchronizes the checked state of the button with the toggle option:

{ app }.Sw.GetUserPreferenceToggle(10)

The macro code itself to toggle the state can be defined as follows

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim curVal As Boolean
    curVal = False <> swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate)
    
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, Not curVal
    
End Sub

As a result, the toggle button is always synchronized with the user preference setting and can be triggered from the button or from the settings dialog.


Product of Xarial Product of Xarial