Description
- Explodes the active assembly if it is collapsed, and collapses it if it is exploded.
- Uses the first exploded view of the active configuration.
- Ideal for a keyboard shortcut or toolbar button.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2014 or newer
- Operating System: Windows 10 or later
Pre-Requisites
- The active document must be an assembly.
- The active configuration needs at least one exploded view.
Results
- The assembly switches between its exploded and collapsed states.
Steps to Set Up the Macro
- Open SOLIDWORKS and the document described in the pre-requisites.
- Create the macro
- Go to Tools > Macro > New, give the .swp file a name and save it. The VBA editor opens.
- Replace everything in the module with the code below.
- Run the macro
- Press F5 in the VBA editor, or use Tools > Macro > Run.
- For daily use, assign it to a toolbar button or keyboard shortcut via Tools > Customize.
VBA Macro Code
' Disclaimer:
' The code provided should be used at your own risk.
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.
' For more information, visit https://bluebyte.biz
' Reviewed and updated by Blue Byte Systems Inc.
Option Explicit
' Toggles the first exploded view of the active configuration:
' explodes the assembly if it is collapsed, collapses it if it is exploded.
' Assign it to a keyboard shortcut or toolbar button for one-click toggling.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim configName As String
Dim vViewNames As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
If swModel.GetType <> swDocASSEMBLY Then
MsgBox "The active document must be an assembly.", vbExclamation
Exit Sub
End If
Set swAssy = swModel
configName = swModel.ConfigurationManager.ActiveConfiguration.Name
If swAssy.GetExplodedViewCount2(configName) < 1 Then
MsgBox "The configuration '" & configName & "' has no exploded view.", vbInformation
Exit Sub
End If
vViewNames = swAssy.GetExplodedViewNames2(configName)
If Not swModel.Extension.SelectByID2(vViewNames(0), "EXPLODEDVIEWS", 0, 0, 0, False, 0, Nothing, 0) Then Exit Sub
If swAssy.IsExploded Then
swAssy.ViewCollapseAssembly
Else
swAssy.ViewExplodeAssembly
End If
swModel.ClearSelection2 True
End Sub
Code Review Notes
We reviewed this macro before publishing it here. Changes from the original version:
- Added a check for no open document.
- Replaced the abrupt End statements with clean exits and clear messages.
Need to customize the macro?
Contact us and we will adapt this macro to your workflow, or build a complete SOLIDWORKS or PDM automation for your team.