US$0.00
0

SOLIDWORKS Macro to Automate Parent Assembly Selection

Description

This macro selects the parent assembly of the currently selected assembly component in SolidWorks. It is particularly useful for navigating assembly hierarchies and understanding component relationships within complex assemblies.

SOLIDWORKS Macro to Automate Parent Assembly Selection and An image divided into two side-by-side sections labeled "Before" and "After," showing a SOLIDWORKS software interface. The "Before" section displays a 3D model of a rectangular block with a single red spherical component and a blue cylindrical attachment on top, and a feature tree on the left listing "Valve Assembly," "Control Panel," and components like "Valve Mixer Shaft" and "Lifted Control Knob" with constraints. The "After" section shows the same block with two blue spherical components and cylindrical attachments on the right side, and the feature tree updated with "Sketch2" and similar constraints. Both sections include toolbars and an isometric 3D view with axes indicators.

System Requirements

  • SOLIDWORKS Version: SOLIDWORKS 2014 or newer
  • Operating System: Windows 7 or later

Pre-requisites

The conditions below must be satisfied before the macro is executed:

  • An assembly document must be currently open in your SOLIDWORKS environment.
  • At least one part/sub-assembly must be previously selected in the assembly window.

Results

  • The assembly selection will immediately change to the parent assembly component.
  • If the item selected is already a top-level component (i.e., there are no parents), the selection will not change.

Steps to Set Up the Macro

To deploy the SolidWorks macro to automate parent assembly selection, go through these easy-to-follow steps:

Create the VBA Modules

  • Launch the SolidWorks VBA Editor by pressing (Alt + F11).
  • Insert a new module into your project and paste the provided VBA code into this module.

Execute the Macro

  • Open an assembly with components that can be selected.
  • Run the macro from within SolidWorks via Tools > Macro > Run and select the saved macro.

Using the Macro

  • The last selected item will automatically be replaced by its parent assembly, making it the active selection.
  • If the selected item has no parent assembly or is a top-level component, the selection will not change.

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).

' SolidWorks VBA macro to select the parent assembly of a selected component

Option Explicit

' Main subroutine
Sub main()
    ' Declare variables for SolidWorks objects and operations
    Dim swApp As SldWorks.SldWorks                  ' SolidWorks application object
    Dim swModel As SldWorks.ModelDoc2               ' Active document object
    Dim swSelMgr As SldWorks.SelectionMgr           ' Selection manager object
    Dim swSelComp As SldWorks.Component2            ' Selected assembly component object
    Dim bRet As Boolean                             ' Boolean for operation success
    Dim i As Integer                                ' Loop counter (unused in this version)
    Dim CurSelCount As Long                         ' Number of selected objects
    Dim NewObjToSelect As Object                    ' Object to select (parent assembly)
    Dim DwgDocComp As DrawingComponent              ' Drawing component object for drawings
    Dim OldToggleVal As Long                        ' Stores the previous toggle state of a user preference

    ' Initialize the SolidWorks application and active document
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    ' Check if a document is open
    If swModel Is Nothing Then Exit Sub

    ' Validate the document type (must be assembly or assembly drawing)
    If swModel.GetType = swDocPART Then
        Exit Sub ' Exit if the document is a part
    ElseIf swModel.GetType = swDocDRAWING Then
        ' Check if the drawing references a part document
        If swModel.ActiveDrawingView.ReferencedDocument.GetType = swDocPART Then
            Exit Sub ' Exit if the referenced document is a part
        End If
    End If

    ' Get the selection manager
    Set swSelMgr = swModel.SelectionManager

    ' Get the number of selected objects
    CurSelCount = swSelMgr.GetSelectedObjectCount
    If CurSelCount = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    End If

    ' Get the selected object (component or drawing component)
    If swModel.GetType = swDocDRAWING Then
        ' For drawings, retrieve the component associated with the drawing view
        Set DwgDocComp = swSelMgr.GetSelectedObjectsComponent2(CurSelCount)
        Set swSelComp = DwgDocComp.Component
    Else
        ' For assemblies, get the selected assembly component
        Set swSelComp = swSelMgr.GetSelectedObjectsComponent(CurSelCount)
    End If

    ' Get the parent assembly of the selected component
    Set NewObjToSelect = swSelComp.GetParent
    If Not NewObjToSelect Is Nothing Then
        ' Deselect the current selection
        swSelMgr.DeSelect CurSelCount
        
        If swModel.GetType = swDocDRAWING Then
            ' For drawings, adjust selection to the drawing context
            Set NewObjToSelect = NewObjToSelect.GetDrawingComponent(swSelMgr.GetSelectedObject6(swSelMgr.GetSelectedObjectCount2(-1), -1))
            swModel.ClearSelection2 True

            ' Temporarily disable the property manager auto-show preference
            OldToggleVal = swApp.GetUserPreferenceToggle(swAutoShowPropertyManager)
            swApp.SetUserPreferenceToggle swAutoShowPropertyManager, False

            ' Select the new object (parent assembly)
            bRet = NewObjToSelect.Select(True, Nothing)

            ' Restore the original preference toggle value
            swApp.SetUserPreferenceToggle swAutoShowPropertyManager, OldToggleVal
        Else
            ' For assemblies, directly select the parent assembly
            bRet = NewObjToSelect.Select(True)
        End If
    End If
End Sub

Macro

You can download the macro from here.

Customization and Advanced Solutions

Contact our experts today to discuss a fully customized solution that enhances your use of the Parent Assembly Selector Macro and all your automation needs.

Author

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×