US$0.00
0

SOLIDWORKS Macro to Delete All Configurations Except the Active One

Clean up a SOLIDWORKS part or assembly by deleting every configuration except the active one.
SOLIDWORKS Macro to Delete All Configurations Except the Active One
Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Description

  • Deletes every configuration except the active one from the active part or assembly.
  • Asks for confirmation first and reports how many configurations were removed.
  • Optionally renames the remaining configuration to “Default”.

System Requirements

  • SOLIDWORKS Version: SOLIDWORKS 2014 or newer
  • Operating System: Windows 10 or later

Pre-Requisites

  • The active document must be a part or an assembly with more than one configuration.
  • Activate the configuration you want to keep before running the macro.

Results

  • Only the active configuration remains.
  • Set RENAME_TO_DEFAULT to True at the top of the code to rename it to “Default”.

Steps to Set Up the Macro

  1. Open SOLIDWORKS and the document described in the pre-requisites.
  1. 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.
  1. 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

' Deletes every configuration except the active one from the active part or assembly.
' Set RENAME_TO_DEFAULT to True to rename the remaining configuration to "Default".

Const RENAME_TO_DEFAULT As Boolean = False

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swConfig As SldWorks.Configuration
    Dim vNames As Variant
    Dim activeName As String
    Dim i As Long
    Dim deleted As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    If swModel Is Nothing Then
        MsgBox "Open a part or assembly before running this macro.", vbExclamation
        Exit Sub
    End If
    If swModel.GetType <> swDocPART And swModel.GetType <> swDocASSEMBLY Then
        MsgBox "The active document must be a part or an assembly.", vbExclamation
        Exit Sub
    End If
    If swModel.GetConfigurationCount < 2 Then
        MsgBox "Only one configuration exists in this model.", vbInformation
        Exit Sub
    End If

    Set swConfig = swModel.ConfigurationManager.ActiveConfiguration
    activeName = swConfig.Name

    If MsgBox("Delete " & (swModel.GetConfigurationCount - 1) & " configuration(s) and keep '" & _
              activeName & "'?" & vbCrLf & "This cannot be undone.", vbYesNo + vbQuestion) <> vbYes Then Exit Sub

    vNames = swModel.GetConfigurationNames
    For i = 0 To UBound(vNames)
        If vNames(i) <> activeName Then
            If swModel.DeleteConfiguration2(vNames(i)) Then deleted = deleted + 1
        End If
    Next

    If RENAME_TO_DEFAULT Then
        swConfig.Name = "Default"
        swConfig.UseAlternateNameInBOM = False
    End If

    swModel.ForceRebuild3 False
    MsgBox deleted & " configuration(s) deleted.", vbInformation
End Sub

Code Review Notes

We reviewed this macro before publishing it here. Changes from the original version:

  • The original needed a separate UserForm that was not published with the code. It now runs from a single module with a confirmation prompt.
  • Fixed an undefined variable that stopped the macro from compiling.

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.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop