Description
This macro automates the process of saving a SolidWorks assembly as a single part file that includes all components. It is particularly useful for simplifying assemblies for external use or reducing file size.
System Requirements
- SolidWorks Version: SolidWorks 2014 or newer
- Operating System: Windows 7 or later
Pre-Conditions
- An assembly document must be open in SolidWorks.
- The assembly must contain at least one part.
Results
- The assembly will be saved as a single part file with all components included.
Steps to Set Up the Macro
1. Prepare the Assembly:
- Open the desired assembly in SolidWorks.
2. Run the Macro:
- Execute the macro to automate the process of saving your assembly as a part file. The SOLIDWORKS macro to save assembly as part ensures that all components are properly included, and the new file is created in the same directory as the original assembly with the
.SLDPRTextension.
VBA Macro Code
Visual Basic
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
' 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 ' Declare SolidWorks objects and variables Dim swApp As SldWorks.SldWorks ' SolidWorks application object Dim swModel As SldWorks.ModelDoc2 ' Active document object Dim swModelDocExt As SldWorks.ModelDocExtension ' Extension object for model operations Dim FilePath As String ' Full file path of the active document Dim PathSize As Long ' Length of the file path string Dim PathNoExtension As String ' File path without extension Dim NewFilePath As String ' Path for the new part file Dim nErrors As Long ' Variable to store errors during save operation Dim nWarnings As Long ' Variable to store warnings during save operation ' Main subroutine to perform the save operation Sub main() ' Initialize SolidWorks application and active document Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swModelDocExt = swModel.Extension ' Get the full path of the active assembly file FilePath = swModel.GetPathName PathSize = Strings.Len(FilePath) ' Remove the extension from the file path to prepare for the new part file PathNoExtension = Strings.Left(FilePath, PathSize - 6) ' Create the new file path with ".SLDPRT" extension NewFilePath = PathNoExtension & "SLDPRT" ' Set the user preference to save the assembly as a part with all components included swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, swSaveAsmAsPart_AllComponents ' Save the assembly as a part file swModelDocExt.SaveAs NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings ' Provide feedback to the user If nErrors = 0 And nWarnings = 0 Then MsgBox "Assembly successfully saved as part: " & NewFilePath, vbInformation, "Success" Else MsgBox "Save operation completed with errors or warnings." & vbCrLf & _ "Errors: " & nErrors & vbCrLf & "Warnings: " & nWarnings, vbExclamation, "Attention" End If End Sub |
You can download the macro from here.
Customization
Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us.