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
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
' 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!
