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.
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:
- Open the Target Assembly: Open the desired SolidWorks assembly file to be the active document that will hide sketches.
- Load the Macro: Open the VBA editor by selecting Alt+F11, then load the macro file (SWP) into the SolidWorks Environment.
- 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
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
' 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.
