Description
This macro converts an active SolidWorks assembly into a part file that contains only the exterior components. This feature is useful for reducing the complexity of the assembly when sharing with external stakeholders or for performance improvements in visualization and analysis.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2014 or newer
- Operating System: Windows 7 or later
Prerequisites
The following conditions must be satisfied for the macro to function as intended:
- An assembly document (.SLDASM) must be currently open and the frontmost document in SOLIDWORKS.
- The active assembly document must contain at least one part component to make the operation meaningful.
Results
- The macro successfully takes the active assembly and saves it as a new part file (.SLDPRT) that contains only exterior geometry.
- The part file is automatically saved in the same directory as the original assembly file using the same file name base, except with the extension .SLDPRT.
- The original assembly document remains unchanged.
Steps to Set Up the Macro
To deploy the SOLIDWORKS macro to save assembly as part utility, you must follow these simple steps:
Prepare SolidWorks
- Open SolidWorks and load the assembly document you wish to modify.
- Your assembly must contain at least one part component.
Configure and Run the Macro
- Open the SolidWorks VBA editor by pressing (Alt + F11) from within SolidWorks.
- Add a new module, and copy the VBA macro code provided below into the module you added. The module serves as the permanent destination for the code, ensuring the utility is saved within SOLIDWORKS for repeated, quick access.
- Next, execute the macro in SolidWorks by navigating to Tools > Macro > Run, and select the file you saved.
Outcome of Using the Macro
- The macro will automatically save the active document as a new part file containing only the exterior parts.
- The automation handles all the complex API settings for Save As Part simplification, ensuring that every time it is executed, in the same way, and the document preserves the history as a part file.
- The original assembly will remain unchanged.
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 52 53 54 |
' 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 ' SolidWorks application and document variables Dim swApp As SldWorks.SldWorks ' SolidWorks application object Dim swModel As SldWorks.ModelDoc2 ' Active SolidWorks document object Dim swModelDocExt As SldWorks.ModelDocExtension ' Extension object for advanced file operations Dim FilePath As String ' Full file path of the current document Dim PathSize As Long ' Length of the file path Dim PathNoExtension As String ' File path without extension Dim NewFilePath As String ' File path for the new part file Dim nErrors As Long ' Counter for errors during the save operation Dim nWarnings As Long ' Counter for warnings during the save operation ' Main subroutine Sub main() ' Initialize SolidWorks application and get the active document Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc ' Check if a document is open If swModel Is Nothing Then MsgBox "No active document found. Please open a file." Exit Sub End If ' Get the ModelDocExtension object for advanced operations Set swModelDocExt = swModel.Extension ' Extract the file path and prepare the new file path FilePath = swModel.GetPathName ' Get the full file path of the active document PathSize = Strings.Len(FilePath) ' Get the length of the file path PathNoExtension = Strings.Left(FilePath, PathSize - 6) ' Remove the last 6 characters (e.g., ".SLDASM") NewFilePath = PathNoExtension & "SLDPRT" ' Append "SLDPRT" to create the new file path ' Set options to save only the exterior components swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, swSaveAsmAsPart_ExteriorComponents ' Save the assembly as a new part file swModelDocExt.SaveAs NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings ' Check for errors and warnings during the save operation If nErrors = 0 And nWarnings = 0 Then ' Success: Notify the user that the save operation was successful MsgBox "Assembly saved as part file successfully at: " & NewFilePath Else ' Failure: Notify the user about errors and warnings MsgBox "Failed to save assembly as part file. Errors: " & nErrors & ", Warnings: " & nWarnings End If End Sub |
Macro
You can download the macro from here.
Customization and Advanced Solutions by Experts
To get started with customizing this macro or to create any other fully custom solutions for your engineering projects, reach out to our team of engineers today!
