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 the button and menu command allowing to switch between checked/unchecked state of the button.

In order to activate toggle behavior select Toggle Button option from the triggers drop-down.

Note, the same macro will be run regardless of toggle button is checked or unchecked. The current state needs to be determined in the macro itself 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 however, 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 then the resolve code runs only once on start to resolve the initial state of the button (check/unchecked). After that toggle state will be switched on every click without calling the resolve code.

Custom Code Resolver

When specifying the code to resolve the state, it is required to complete the construction of the function which returns boolean, where true corresponds to checked state and false to unchecked.

The code should be written in C#.

Function provides an access to top level application (ISwApplication from xCAD.NET framework). Use late binding to access underlying SOLIDWORKS API objects.

Example

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

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

In order to find the current value of this setting it is required to call the ISldWorks::GetUserPreferenceToggle API and pass the value of swInputDimValOnCreate.

As it is required to use late binding, corresponding integer value of the enumeration should be found:

swInputDimValOnCreate enumeration constant
swInputDimValOnCreate enumeration constant

Adding the following code would result into the check state of the button to be synchronized 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 the result the toggle button will always be synchronized with user preference setting and can be triggered via button or from settings dialog.


Product of Xarial Product of Xarial