Description
This macro is designed to navigate through the assembly structure in SolidWorks to find the top-level assembly for a selected component and open it if it’s not already active. If the selected component is a part, the macro will open its file directly.
System Requirements
- SolidWorks Version: SolidWorks 2014 or newer
- Operating System: Windows 7 or later
Pre-Conditions
- The macro must run within an open SolidWorks document.
- That component must be selected in an open assembly or part document before the macro is run.
Results
The macro executes a specific file-related action and makes a specific window active based on the selection made:
- The macro opens and becomes active on the highest-level assembly document containing the selected component.
- If the selected object is a part, and it doesn’t have a parent assembly (so it is either a top-level component or a part selected in a part document), then the macro will open that part document and make it active directly.
Steps to Set Up the Macro
Follow these steps to get the SolidWorks macro for efficient assembly navigation running:
- Create the Module: Launch the VBA editor from SolidWorks by pressing Alt + F11. In the desired VBA project, right-click on the Modules folder, and select Insert → Module.
- Insert the Code: Paste the VBA code provided below into the newly created module. Save the file as your macro (for example, OpenTopAssembly.swp).
- Set up the Document: Ensure that an assembly document is open, and that a component (or feature associated with a component) is selected in SolidWorks in the feature manager tree or in the graphics area.
- Execute the Macro: Run the macro from the Tools → macro → run menu in SolidWorks. Then choose your saved macro. Doing so will immediately allow the macro to handle traversing the hierarchy and activating the files, minimising clicks and search time.
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
' SolidWorks application and document objects
Dim swApp As SldWorks.SldWorks ' SolidWorks application object
Dim swmodel As SldWorks.ModelDoc2 ' Active document object
Dim assemdoc As SldWorks.ModelDoc2 ' Assembly document object
Dim swselmgr As SldWorks.SelectionMgr ' Selection manager object
Dim selcomp As SldWorks.Component2 ' Selected component object
Dim swent As SldWorks.Entity ' Entity object representing selected geometry
Dim parcomp As SldWorks.Component2 ' Parent component in the hierarchy
Dim tempcomp As SldWorks.Component2 ' Temporary component for parent traversal
Sub main()
' Initialize SolidWorks application
Set swApp = Application.SldWorks
' Get the active document
Set swmodel = swApp.ActiveDoc
' Get the selection manager for the active document
Set swselmgr = swmodel.SelectionManager
' Get the first selected object as an entity
Set swent = swselmgr.GetSelectedObject(1)
' Retrieve the component associated with the selected entity
Set selcomp = swent.GetComponent
' Get the parent component of the selected component
Set tempcomp = selcomp.GetParent
' Check if the selected component has no parent (it is a standalone part)
If tempcomp Is Nothing Then
' Open the document associated with the selected component
Set swmodel = swApp.OpenDoc(selcomp.GetPathName, 2) ' Open as a part document
' Activate the document to display it in the SolidWorks interface
Set swmodel = swApp.ActivateDoc(selcomp.Name)
Exit Sub
End If
' Initialize parent component as the immediate parent
Set parcomp = tempcomp
' Traverse the hierarchy to find the topmost parent component
Do While Not tempcomp Is Nothing
' Get the parent of the current component
Set tempcomp = tempcomp.GetParent
' Update the parent component to the current one if valid
If Not tempcomp Is Nothing Then
Set parcomp = tempcomp
End If
Loop
' Get the assembly document associated with the topmost parent component
Set assemdoc = parcomp.GetModelDoc2
' Open the assembly document
Set swmodel = swApp.OpenDoc(assemdoc.GetPathName, 2) ' Open as an assembly document
' Activate the assembly document to display it
Set swmodel = swApp.ActivateDoc(assemdoc.GetTitle)
End Sub
Macro
You can download the macro from here.
Need Help Customizing The Macro?
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 today!
