Description
The macro will traverse your open SOLIDWORKS assembly file (.SLDASM) methodically and recursively, detect each child component (part), and then proceed to automatically save each part in the stable Parasolid (X_T) format. The macro traverses recursively through the assembly structure so that each part is saved based on the part name.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2014 or newer
- Operating System: Windows 7 or later.
Pre-requisites
To execute the macro, the following conditions must be satisfied:
- The active document within SOLIDWORKS needs to be an assembly document (.SLDASM).
- There should be a valid output folder path ready to receive the Parasolid (X_T) files.
Results
The results of your automated batch export process are:
- A separate Parasolid (X_T) file will be created for each unique part that is processed in the assembly’s hierarchy.
- The files will be exported to the user-specified folder path and retain the original part file name used when exporting.
- The macro also intelligently skips parts that are suppressed or already opened in a second SOLIDWORKS window.
Steps to Set Up the Macro
- Register Macro File: Open either a new or existing SolidWorks session and navigate to the Tools > Macro > Newā¦
- Name Macro File: It is advisable to give the macro an appropriate title, such as “BatchAssemblyToXT.swp”, which will stand for easier identification in the future. Once you have saved, another dialog will then display with the VBA editor open.
- Paste the code: Download and copy the VBA code provided for you below, and paste it into your editor. This process setup is typically used when a creating SOLIDWORKS macro to export assembly components as parasolid efficiently.
- Execute the Macro: Wait for the prompt to save your work and close the editor. With the assembly open, you can now run your macro through Tools > Macro > Run, or by using a previously assigned shortcut.
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
' --------------------------------------------------------------------------
' Main subroutine to initialize the process and prompt the user for a save path
' --------------------------------------------------------------------------
Sub main()
' Declare necessary SolidWorks objects
Dim swApp As SldWorks.SldWorks ' SolidWorks application object
Dim swModel As SldWorks.ModelDoc2 ' Active document object (assembly)
Dim savepath As String ' User input for the folder path to save Parasolid files
' Initialize SolidWorks application and get the active document
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Check if there is an active document open
If swModel Is Nothing Then
MsgBox "No active document found. Please open an assembly and try again.", vbCritical, "No Active Document"
Exit Sub
End If
' Prompt user for the folder path to save Parasolid files
savepath = InputBox("Where do you want to save the Parasolid (X_T) files?")
' Call the traverse function to iterate through components and save as Parasolid files
traverse swApp.ActiveDoc, savepath
End Sub
' --------------------------------------------------------------------------
' Recursive function to traverse through the assembly components and save parts
' --------------------------------------------------------------------------
Function traverse(Pathname As ModelDoc2, savepath As String)
' Declare necessary variables and objects
Dim swApp As SldWorks.SldWorks ' SolidWorks application object
Dim swModel As SldWorks.ModelDoc2 ' Model document object for components
Dim swConfMgr As SldWorks.ConfigurationManager ' Configuration manager object
Dim swConf As SldWorks.Configuration ' Configuration object for the active configuration
Dim swRootComp As SldWorks.Component2 ' Root component of the assembly
Dim vChildComp As Variant ' Array of child components in the assembly
Dim swChildComp As SldWorks.Component2 ' Individual child component object
Dim i As Long ' Loop counter for iterating through child components
Dim longstatus As Long ' Status variable for capturing save operations
' Initialize SolidWorks application
Set swApp = Application.SldWorks
Set swModel = Pathname ' Set the model to the input pathname (active document)
' Get the configuration manager and active configuration of the model
Set swConfMgr = swModel.ConfigurationManager
Set swConf = swConfMgr.ActiveConfiguration
' Get the root component of the assembly
Set swRootComp = swConf.GetRootComponent3(True)
' Get the child components of the root component
vChildComp = swRootComp.GetChildren
' Loop through each child component in the assembly
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i) ' Set the child component
' Get the model document of the child component
Set swModel = swChildComp.GetModelDoc2
' If the child component is a part, traverse further or save as Parasolid (X_T)
If Not swModel Is Nothing Then
' Check if the component is an assembly (type 2 = swDocASSEMBLY)
If swModel.GetType = 2 Then
' Recursively traverse through sub-assemblies
traverse swModel, savepath
Else
' Save the part as a Parasolid (X_T) file in the specified folder
longstatus = swModel.SaveAs3(savepath & "\" & swModel.GetTitle & ".X_T", 0, 0)
End If
End If
Next i
End Function
' --------------------------------------------------------------------------
' Function to extract the title (filename without extension) from the path
' --------------------------------------------------------------------------
Public Function GetTitle(Path As String) As String
' Declare necessary variables
Dim path1 As Variant ' Array to hold path segments
Dim title As String ' Extracted title (filename without extension)
' Split the path into segments based on "\"
path1 = Split(Path, "\")
' Get the last segment of the path as the filename
title = path1(UBound(path1))
' Return the title without the file extension
GetTitle = Left(title, InStr(title, ".") - 1)
End Function
Macro
You can download the macro fromĀ here.