US$0.00
0

SolidWorks Macro to Efficiently Manage Assembly by Hiding All Sketches

Description

This macro hides all visible sketches in the active assembly. This is useful for cleaning up the view in complex assemblies where multiple sketches may clutter the workspace.

SolidWorks Macro to Efficiently Manage Assembly by Hiding All Sketches and An image divided into two side-by-side sections labeled "Before" and "After," showing a 3D model of a gas cylinder in a SOLIDWORKS interface. The "Before" section displays the cylinder with a handle and valve assembly at the top, a blue plane labeled "Plane2" and "Plane4" intersecting it, and a circular base outline. The "After" section shows the same cylinder with the handle and valve assembly adjusted, the blue planes repositioned, and the circular base outline removed, indicating a refined design. Both sections include a 3D view with axes indicators.

System Requirements

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

Prerequisites

  • An assembly document must be the active document and open in SolidWorks.
  • At least one of the components in the assembly must have a sketch feature currently displayed to be relevant to the macro’s action.

Results

  • The macro will iterate through the feature tree of the assembly and all nested components, and all visible sketches will become hidden globally across the active assembly.

Steps to Set Up the Macro

To quickly get the SolidWorks macro to efficiently manage assembly by hiding all sketches up and running, follow these brief instructions:

  1. Open the Target Assembly: Open the desired SolidWorks assembly file to be the active document that will hide sketches.
  2. Load the Macro: Open the VBA editor by selecting Alt+F11, then load the macro file (SWP) into the SolidWorks Environment.
  3. Execute The Macro: Run the macro by navigating to Tools → Macro → Run, in the SolidWorks interface.

Pro-Tip: It is highly recommended to assign a keyboard shortcut or a toolbar button to this macro to achieve maximum speed and efficiency.

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

' *********************************************************
' Hide All Sketches.swp
' Description: Macro to hide all sketches in the active assembly.
' *********************************************************

Option Explicit

' Subroutine to hide a sketch feature
Sub BlankSketchFeature(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swFeat As SldWorks.Feature)
    Dim bRet As Boolean
    ' Check if the feature is a sketch
    If "ProfileFeature" = swFeat.GetTypeName Then
        ' Select the sketch and hide it
        bRet = swFeat.Select2(False, 0): Debug.Assert bRet
        swModel.BlankSketch
    End If
End Sub

' Subroutine to traverse features and their subfeatures
Sub TraverseFeatureFeatures(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swFeat As SldWorks.Feature, nLevel As Long)
    Dim swSubFeat As SldWorks.Feature
    Dim swSubSubFeat As SldWorks.Feature
    Dim swSubSubSubFeat As SldWorks.Feature
    Dim sPadStr As String
    Dim i As Long
    Dim bRet As Boolean

    ' Create padding for debug output based on the level of recursion
    For i = 0 To nLevel
        sPadStr = sPadStr + "  "
    Next i

    ' Traverse through all features
    While Not swFeat Is Nothing
        Debug.Print sPadStr + swFeat.Name + " [" + swFeat.GetTypeName + "]"
        BlankSketchFeature swApp, swModel, swFeat

        ' Traverse subfeatures
        Set swSubFeat = swFeat.GetFirstSubFeature
        While Not swSubFeat Is Nothing
            Debug.Print sPadStr + "  " + swSubFeat.Name + " [" + swSubFeat.GetTypeName + "]"
            BlankSketchFeature swApp, swModel, swSubFeat

            ' Traverse sub-subfeatures
            Set swSubSubFeat = swSubFeat.GetFirstSubFeature
            While Not swSubSubFeat Is Nothing
                Debug.Print sPadStr + "    " + swSubSubFeat.Name + " [" + swSubSubFeat.GetTypeName + "]"
                BlankSketchFeature swApp, swModel, swSubSubFeat

                ' Traverse sub-sub-subfeatures
                Set swSubSubSubFeat = swSubSubFeat.GetFirstSubFeature
                While Not swSubSubSubFeat Is Nothing
                    Debug.Print sPadStr + "      " + swSubSubSubFeat.Name + " [" + swSubSubSubFeat.GetTypeName + "]"
                    BlankSketchFeature swApp, swModel, swSubSubSubFeat
                    Set swSubSubSubFeat = swSubSubSubFeat.GetNextSubFeature
                Wend

                Set swSubSubFeat = swSubSubFeat.GetNextSubFeature
            Wend

            Set swSubFeat = swSubFeat.GetNextSubFeature
        Wend

        Set swFeat = swFeat.GetNextFeature
    Wend
End Sub

' Subroutine to traverse features of a component
Sub TraverseComponentFeatures(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swComp As SldWorks.Component2, nLevel As Long)
    Dim swFeat As SldWorks.Feature
    Set swFeat = swComp.FirstFeature
    TraverseFeatureFeatures swApp, swModel, swFeat, nLevel
End Sub

' Subroutine to traverse components and their features
Sub TraverseComponent(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swComp As SldWorks.Component2, nLevel As Long)
    Dim vChildComp As Variant
    Dim swChildComp As SldWorks.Component2
    Dim sPadStr As String
    Dim i As Long

    ' Create padding for debug output based on the level of recursion
    For i = 0 To nLevel - 1
        sPadStr = sPadStr + "  "
    Next i

    ' Get child components and traverse them
    vChildComp = swComp.GetChildren
    For i = 0 To UBound(vChildComp)
        Set swChildComp = vChildComp(i)
        Debug.Print sPadStr & "+" & swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">"
        TraverseComponentFeatures swApp, swModel, swChildComp, nLevel
        TraverseComponent swApp, swModel, swChildComp, nLevel + 1
    Next i
End Sub

' Subroutine to traverse features in the main model
Sub TraverseModelFeatures(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, nLevel As Long)
    Dim swFeat As SldWorks.Feature
    Set swFeat = swModel.FirstFeature
    TraverseFeatureFeatures swApp, swModel, swFeat, nLevel
End Sub

' Main subroutine to execute the macro
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Dim nStart As Single

    ' Initialize SolidWorks application and active document
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "No active document found. Please open an assembly.", vbExclamation, "Error"
        Exit Sub
    End If

    Set swConf = swModel.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent

    ' Start timing the process
    nStart = Timer
    Debug.Print "File = " & swModel.GetPathName

    ' Traverse and hide all sketches in the model
    TraverseModelFeatures swApp, swModel, 1
    TraverseComponent swApp, swModel, swRootComp, 1

    ' Output elapsed time
    Debug.Print "Time = " & Timer - nStart & " s"
End Sub

Macro

You can download the macro from here.

Customize with Expert Support

Contact us today and get the most from your SolidWorks automation.

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
    ×