Description
- Adds a best-fit Bounding Box feature to the active assembly.
- Adds the same feature to every unique part in the assembly, including parts in sub-assemblies.
- Useful for packaging, stock size and shipping dimensions in the BOM.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2018 or newer (Bounding Box feature)
- Operating System: Windows 10 or later
Pre-Requisites
- The active document must be an assembly.
- Parts must not be read-only if you want to save the new feature.
Results
- Each part and the assembly get a Bounding Box feature and the related size properties.
- Use File > Save All to keep the changes. Running the macro twice adds a second bounding box.
Steps to Set Up the Macro
- Open SOLIDWORKS and the document described in the pre-requisites.
- 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.
- 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
' Adds a best-fit Bounding Box feature to the active assembly and to every
' unique part in it. Requires SOLIDWORKS 2018 or newer.
' Run it once: running it again adds a second bounding box.
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim vComps As Variant
Dim vItem As Variant
Dim done As Object
Dim key As String
Dim added As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Open an assembly before running this macro.", vbExclamation
Exit Sub
End If
If swModel.GetType <> swDocASSEMBLY Then
MsgBox "The active document must be an assembly.", vbExclamation
Exit Sub
End If
Set swAssy = swModel
swAssy.ResolveAllLightWeightComponents False
' Top-level assembly bounding box
If CreateBoundingBox(swModel) Then added = added + 1
' One bounding box per unique part file
Set done = CreateObject("Scripting.Dictionary")
vComps = swAssy.GetComponents(False)
If Not IsEmpty(vComps) Then
For Each vItem In vComps
Set swComp = vItem
Set swCompModel = swComp.GetModelDoc2
If Not swCompModel Is Nothing Then
If swCompModel.GetType = swDocPART Then
key = LCase(swCompModel.GetPathName)
If Not done.Exists(key) Then
done.Add key, True
If CreateBoundingBox(swCompModel) Then added = added + 1
End If
End If
End If
Next
End If
swModel.ForceRebuild3 False
MsgBox added & " bounding box(es) added. Use File > Save All to keep them.", vbInformation
End Sub
Private Function CreateBoundingBox(ByVal swDoc As SldWorks.ModelDoc2) As Boolean
Dim swFeatMgr As SldWorks.FeatureManager
Dim swData As SldWorks.BoundingBoxFeatureData
Dim swFeat As SldWorks.Feature
Dim wasVisible As Boolean
wasVisible = swDoc.Visible
If Not wasVisible Then swDoc.Visible = True
Set swFeatMgr = swDoc.FeatureManager
Set swData = swFeatMgr.CreateDefinition(swFmBoundingBox)
If Not swData Is Nothing Then
swData.ReferenceFaceOrPlane = swBoundingBoxType_BestFit
swData.IncludeHiddenBodies = False
swData.IncludeSurfaces = False
Set swFeat = swFeatMgr.CreateFeature(swData)
End If
If Not wasVisible Then swDoc.Visible = False
CreateBoundingBox = Not swFeat Is Nothing
End Function
Code Review Notes
We reviewed this macro before publishing it here. Changes from the original version:
- Added a check that the active document is an assembly.
- Parts are tracked by full path instead of title, so parts with the same name in different folders are both handled.
- Sub-assemblies are skipped, and window visibility is restored to its original state.
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.