US$0.00
0

SolidWorks Macro to Instantly Isolate Selected Components in an Assembly

Description

Pre-select one or more components in an assembly and execute the macro. The selected components will become isolated in the assembly. The SolidWorks macro to instantly isolate selected components in an assembly can be placed on the Graphics Area menu (which pops up when you right-click a component) for convenient access.

SolidWorks Macro to Instantly Isolate Selected Components in an Assembly 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 red cylindrical component attached to its top surface, and a feature tree on the left listing "Control Panel" and "Valve Assembly" with various constraints and features like "Lifted Control." The "After" section shows the same block with the red cylindrical component removed, leaving a cutout section, and the feature tree updated to reflect the modified assembly. Both sections include toolbars and a 3D view with axes indicators.

System Requirements

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

Prerequisites

  • At least one component must be pre-selected in an active SolidWorks assembly document.
  • The active document must be an assembly document (.SLDASM).

Results

  • The pre-selected components will be isolated in the assembly environment.
  • If no components are selected, or if the active document is not an assembly document, a message box will appear to direct the user to do so.

Steps to Set Up the Macro

  1. Registering the Macro File: Make sure you have SolidWorks open, and navigate to Tools > Macro > New… 
  2. Name and Register: Name the macro meaningfully (e.g., IsolateComponents.swp), then save the file. This will automatically open your VBA editor.
  3. Paste the VBA code: Please download the VBA code we have listed below and place it within your editor.
  4. Run Macro (Optional Next Setup): Close the VBA editor and save when prompted. Now you can run the macro with Tools > Macro > Run, or with a shortcut, or you can add it to a shortcut menu for convenience.

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

' Main subroutine to isolate selected components in an assembly
Sub main()

    ' Declare SolidWorks application and active document objects
    Dim swApp As Object                              ' SolidWorks application object
    Dim Part As Object                               ' Active document object (assembly)
    Dim boolstatus As Boolean                        ' Boolean status to capture operation results
    Dim longstatus As Long, longwarnings As Long     ' Long variables for capturing status and warnings

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

    ' Check if there is an active document open
    If Part Is Nothing Then
        MsgBox "No active document found. Please open an assembly and try again.", vbCritical, "No Active Document"
        Exit Sub
    End If

    ' Check if the active document is an assembly
    If Part.GetType <> swDocASSEMBLY Then
        MsgBox "This macro only works on assemblies. Please open an assembly and try again.", vbCritical, "Invalid Document Type"
        Exit Sub
    End If

    ' Isolate the pre-selected components in the assembly
    ' RunCommand with ID 2726 is used to isolate components in SolidWorks
    boolstatus = Part.Extension.RunCommand(2726, "")

    ' Note: The following command can be used to exit isolation mode if required:
    ' boolstatus = Part.Extension.RunCommand(2732, "")   ' RunCommand ID 2732 exits isolate mode

    ' Clean up by releasing references to objects
    Set Part = Nothing
    Set swApp = Nothing

End Sub

Macro

You can download the macro fromĀ here.

We build custom macros and add-ins that meet your specific workflow needs. Contact us to discuss custom development needs and projects!

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
    ×