US$0.00
0

SOLIDWORKS Macro for Automated Batch Export of Configurations to STEP

Description

The SOLIDWORKS macro provided here is a means to batch export configurations to STEP.

System Requirements

  • SolidWorks Version: SolidWorks 2014 or newer
  • Operating System: Windows 7 or later

Pre-requisites

  • You should have an assembly or part document with at least one configuration open.
  • The macro should be run in the SolidWorks environment.

Results

  • All configurations in the active document are saved as separate STEP files, ensuring that each design iteration is isolated from one another.
  • The user determines the file name and destination by providing a unique file prefix and output directory.
  • When the batch export process proceeds, status and progress are tracked via a progress bar.

Steps to Set Up the Macro

  1. Opening The SOLIDWORKS Document
  • Prior to executing the following instructions, ensure that the Assembly or Part file containing the design configurations you wish to export is the active document in SOLIDWORKS.
  1. Executing the Macro
  • Opening the VBA Editor: A VBA editor can be opened while SOLIDWORKS is open by pressing Alt + F11.
  • Loading the Code: Add a new module using Insert > Module and paste the provided VBA code from the SOLIDWORKS macro for automated batch export in the new module window.
  • Executing: You can either run the macro in the VBA Editor by selecting  Run > Run Sub/UserForm, or you can save the macro as a macro file (.swp), then it can be run from the SOLIDWORKS menu (Tools > Macro > Run).
  1. Specify Output Parameters

After the macro has been run, you will then be prompted to input two critical parameters:

  • File Prefix: Input a static string (e.g., “Project_Model_”) that will be added to the beginning of each exported file name, and help in identifying related files.
  • Output Directory: Input the name of a new folder (e.g., “STEP_Exports”) that the exported files will go into.

Verify Completion

Confirm the outcome of the process:

  1. The progress bar will display progress in real-time, showing each configuration’s status as it is processed and saved.
  2. Once all configurations have been successfully converted, one last message box will appear, listing the total number of new STEP files that were saved to the path you provided.

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 [Blue Byte Systems Inc.](https://bluebyte.biz).

Option Explicit

Sub main()
    ' Initialize SolidWorks application and document
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc

    ' Verify an active document is open
    If swModel Is Nothing Then
        MsgBox "No active document found. Please open a model.", vbCritical, "Error"
        Exit Sub
    End If

    ' Declare configuration management objects
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Set swConfigMgr = swModel.ConfigurationManager
    Dim swConfig As SldWorks.Configuration
    Set swConfig = swModel.GetActiveConfiguration

    ' Prepare file path and name variables
    Dim path As String, fname As String, current As String, prefix As String, dirName As String
    path = swModel.GetPathName
    fname = Mid(path, InStrRev(path, "\") + 1, Len(path) - InStr(path, ".") - 1)
    path = Left(path, InStrRev(path, "\") - 1)
    current = swModel.GetActiveConfiguration.Name
    Dim configs As Variant
    configs = swModel.GetConfigurationNames

    ' User input for file prefix and output directory
    prefix = InputBox("Enter the prefix:", "Names", fname)
    If prefix = "" Then
        MsgBox "Prefix cannot be empty.", vbCritical, "Error"
        Exit Sub
    End If
    dirName = InputBox("Enter the directory name for saving:", "Directory Name", "STEP")
    If dirName = "" Then
        MsgBox "Directory name cannot be empty.", vbCritical, "Error"
        Exit Sub
    End If

    ' Ensure output directory exists
    If Dir(dirName, vbDirectory) = "" Then MkDir dirName
    ChDir dirName

    ' Progress bar setup and configuration iteration
    Dim i As Long
    For i = 0 To UBound(configs)
        Dim name As String
        name = prefix & configs(i) & ".STEP"
        swModel.ShowConfiguration2 configs(i)
        Call swModel.SaveAs3(name, 0, 0)
    Next i

    ' Confirmation message and cleanup
    MsgBox "Saved " & CStr(i) & " files!", vbInformation, "Done"
    swModel.ShowConfiguration2 current
    ChDir path
End Sub

Macro

You can download the macro from here

Our team of experts is at your service!

Contact us today to develop a custom solution that meets your specific workflow needs!

Author

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. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×