Macro Hide all in parts and assembly

Library for macros
Koen88
Posts: 7
Joined: Tue May 18, 2021 3:26 am
Answers: 1
x 2
x 4

Macro Hide all in parts and assembly

Unread post by Koen88 »

Hi there,
I have this m
acro “Hide all in parts and assembly” it’s supposed to hide all sketches and planes and collapse the tree and move the freezebar to the end. It works fine in most cases but there are 2 unwanted behaviours I’d like to correct.

1) It does not hide the visible sketch of a structural member.
2) It flattens some (!) sheet metal parts.

Does anyone know what might cause this? And how to fix it offcourse? I tried some things but I’m not well versed in coding and macro’s.

(posted in full for reference)

Code: Select all

'Hide All in Parts and Assembly.swp -------------07/12/14

'Description: Macro to hide all planes, axis, co-ordinate system, 2d-3d sketches, origin in the active part/assembly file and set FreezeBar to end.
'Precondition: Any active part/assembly file to be saved.
'Postcondition: Active part/assembly will be saved with FreezeBar enabled and set to end with the specified entities set to *hidden.

' Macro is based on the requirements by Ruairi Mulligan: https://forum.solidworks.com/message/438806#438806

'*Hidden means as right-clicking an item and selecting Hide on the shortcut menu.

' Please back up your data before use and USE AT OWN RISK

' This macro is provided as is.  No claims, support, refund, safety net, or
' warranties are expressed or implied.  By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.  Free distribution
' and use of this code in other free works is welcome.  If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header).  All other
' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.  Use at your own risk!
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta (http://gupta9665 . com/)
' -------------------------------------------------------------------------------

Option Explicit
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc
    Dim vConfNameArr            As Variant
    Dim sConfigName             As String
    Dim nStart                  As Single
    Dim i                       As Long
    Dim bShowConfig             As Boolean
    Dim bRebuild                As Boolean
    Dim bRet                    As Boolean
    Dim swFeat                  As SldWorks.Feature
    Dim nErrors                 As Long
    Dim nWarnings               As Long
Sub main()

On Error Resume Next

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Check if there is any Part or Assembly file
If swModel Is Nothing Or swModel.GetType = 3 Then
    MsgBox "Please open a Part/Assembly first and then try again!!"
    Exit Sub
End If

' Enabling the Freeze bar
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swUserEnableFreezeBar, True

'Traverse All configuration Features and hiding the references geometries, origin and sketches

 vConfNameArr = swModel.GetConfigurationNames
    For i = 0 To UBound(vConfNameArr)
        sConfigName = vConfNameArr(i)
        bShowConfig = swModel.ShowConfiguration2(sConfigName)
             
        Set swFeat = swModel.FirstFeature
            Do While Not swFeat Is Nothing
            'Hiding 2d sketch, 3d sketch and Origin
        If "ProfileFeature" = swFeat.GetTypeName Or "3DProfileFeature" = swFeat.GetTypeName Or "OriginProfileFeature" = swFeat.GetTypeName Then
            bRet = swFeat.Select2(False, 0): Debug.Assert bRet
            swModel.BlankSketch
        End If

            'Hiding planes, axis, co-ordinate system
        If "RefPlane" = swFeat.GetTypeName Or "RefAxis" = swFeat.GetTypeName Or "CoordSys" = swFeat.GetTypeName Then
            bRet = swFeat.Select2(False, 0): Debug.Assert bRet
            swModel.BlankRefGeom
        End If

            Set swFeat = swFeat.GetNextFeature

        Loop

    Next i
    
    swModel.ClearSelection2 True
    

    swModel.ForceRebuild3 (False) 'Rebuild the model
    swModel.ShowNamedView2 "*Isometric", -1 ' Set the Isometric View
    swModel.ViewZoomtofit2 ' Zoom to fit
    swModel.Extension.RunCommand swCommands_Collapseallitems_Tree, ""  ' Collapse Feature Tree
    swModel.FeatureManager.EditFreeze SwConst.swMoveFreezeBarTo_e.swMoveFreezeBarToEnd, "", True ' Move freeze bar to end of the feature tree
    swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings 'Save the file
       
    MsgBox swModel.GetTitle & " is ready to Check-In!!" 'File ready to check in
  
End Sub
by AlexB » Fri May 21, 2021 10:29 am
1. Without testing it, I'm thinking that a structural member sketch is a sub-feature of the structural member feature so it may be needed to add another while loop inside the current one to iterate through those looking for the same "ProfileFeature" & "3DProfileFeature" names.

2. My initial thoughts are that since it's going through and activating each configuration in order to sort through its features, the last configuration of a sheet metal part is the "Flat-Pattern" configuration. To fix this, you can just add a line to remember the current configuration prior to running the for loop and then activate it again after it exits.


With updates. Disclaimer: I take no responsibility for using this in a production environment. UU

Code: Select all

'Hide All in Parts and Assembly.swp -------------07/12/14

'Description: Macro to hide all planes, axis, co-ordinate system, 2d-3d sketches, origin in the active part/assembly file and set FreezeBar to end.
'Precondition: Any active part/assembly file to be saved.
'Postcondition: Active part/assembly will be saved with FreezeBar enabled and set to end with the specified entities set to *hidden.

' Macro is based on the requirements by Ruairi Mulligan: https://forum.solidworks.com/message/438806#438806

'*Hidden means as right-clicking an item and selecting Hide on the shortcut menu.

' Please back up your data before use and USE AT OWN RISK

' This macro is provided as is.  No claims, support, refund, safety net, or
' warranties are expressed or implied.  By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.  Free distribution
' and use of this code in other free works is welcome.  If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header).  All other
' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.  Use at your own risk!
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta (http://gupta9665 . com/)
' -------------------------------------------------------------------------------

Option Explicit
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc
    Dim vConfNameArr            As Variant
    Dim sConfigName             As String
    Dim sConfigNameInitial      As String
    Dim nStart                  As Single
    Dim i                       As Long
    Dim bShowConfig             As Boolean
    Dim bRebuild                As Boolean
    Dim bRet                    As Boolean
    Dim swFeat                  As SldWorks.Feature
    Dim swSubFeat               As SldWorks.Feature
    Dim nErrors                 As Long
    Dim nWarnings               As Long
Sub main()

On Error Resume Next

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Check if there is any Part or Assembly file
If swModel Is Nothing Or swModel.GetType = 3 Then
    MsgBox "Please open a Part/Assembly first and then try again!!"
    Exit Sub
End If

' Enabling the Freeze bar
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swUserEnableFreezeBar, True

'Traverse All configuration Features and hiding the references geometries, origin and sketches


sConfigNameInitial = swApp.GetActiveConfigurationName(swModel.GetPathName)
vConfNameArr = swModel.GetConfigurationNames
    For i = 0 To UBound(vConfNameArr)
        sConfigName = vConfNameArr(i)
        bShowConfig = swModel.ShowConfiguration2(sConfigName)
             
        Set swFeat = swModel.FirstFeature
        
        Do While Not swFeat Is Nothing
            'Hiding 2d sketch, 3d sketch and Origin
            If "ProfileFeature" = swFeat.GetTypeName Or "3DProfileFeature" = swFeat.GetTypeName Or "OriginProfileFeature" = swFeat.GetTypeName Then
                If swFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                    bRet = swFeat.Select2(False, 0): Debug.Assert bRet
                    swModel.BlankSketch
                End If
            End If

                'Hiding planes, axis, co-ordinate system
            If "RefPlane" = swFeat.GetTypeName Or "RefAxis" = swFeat.GetTypeName Or "CoordSys" = swFeat.GetTypeName Then
                If swFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                    bRet = swFeat.Select2(False, 0): Debug.Assert bRet
                    swModel.BlankRefGeom
                End If
            End If
            
            Set swSubFeat = swFeat.GetFirstSubFeature
            Do While Not swSubFeat Is Nothing
                If "ProfileFeature" = swSubFeat.GetTypeName Or "3DProfileFeature" = swSubFeat.GetTypeName Then
                    If swSubFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                        bRet = swSubFeat.Select2(False, 0): Debug.Assert bRet
                        swModel.BlankSketch
                    End If
                End If
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Loop

            Set swFeat = swFeat.GetNextFeature

        Loop

    Next i
    
    swModel.ClearSelection2 True
    bShowConfig = swModel.ShowConfiguration2(sConfigNameInitial)

    swModel.ForceRebuild3 (False) 'Rebuild the model
    swModel.ShowNamedView2 "*Isometric", -1 ' Set the Isometric View
    swModel.ViewZoomtofit2 ' Zoom to fit
    swModel.Extension.RunCommand swCommands_Collapseallitems_Tree, ""  ' Collapse Feature Tree
    swModel.FeatureManager.EditFreeze SwConst.swMoveFreezeBarTo_e.swMoveFreezeBarToEnd, "", True ' Move freeze bar to end of the feature tree
    swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings 'Save the file
       
    MsgBox swModel.GetTitle & " is ready to Check-In!!" 'File ready to check in
  
End Sub

Go to full post
User avatar
AlexB
Posts: 493
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 27
x 265
x 440

Re: Macro Hide all in parts and assembly

Unread post by AlexB »

1. Without testing it, I'm thinking that a structural member sketch is a sub-feature of the structural member feature so it may be needed to add another while loop inside the current one to iterate through those looking for the same "ProfileFeature" & "3DProfileFeature" names.

2. My initial thoughts are that since it's going through and activating each configuration in order to sort through its features, the last configuration of a sheet metal part is the "Flat-Pattern" configuration. To fix this, you can just add a line to remember the current configuration prior to running the for loop and then activate it again after it exits.


With updates. Disclaimer: I take no responsibility for using this in a production environment. UU

Code: Select all

'Hide All in Parts and Assembly.swp -------------07/12/14

'Description: Macro to hide all planes, axis, co-ordinate system, 2d-3d sketches, origin in the active part/assembly file and set FreezeBar to end.
'Precondition: Any active part/assembly file to be saved.
'Postcondition: Active part/assembly will be saved with FreezeBar enabled and set to end with the specified entities set to *hidden.

' Macro is based on the requirements by Ruairi Mulligan: https://forum.solidworks.com/message/438806#438806

'*Hidden means as right-clicking an item and selecting Hide on the shortcut menu.

' Please back up your data before use and USE AT OWN RISK

' This macro is provided as is.  No claims, support, refund, safety net, or
' warranties are expressed or implied.  By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.  Free distribution
' and use of this code in other free works is welcome.  If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header).  All other
' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.  Use at your own risk!
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta (http://gupta9665 . com/)
' -------------------------------------------------------------------------------

Option Explicit
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc
    Dim vConfNameArr            As Variant
    Dim sConfigName             As String
    Dim sConfigNameInitial      As String
    Dim nStart                  As Single
    Dim i                       As Long
    Dim bShowConfig             As Boolean
    Dim bRebuild                As Boolean
    Dim bRet                    As Boolean
    Dim swFeat                  As SldWorks.Feature
    Dim swSubFeat               As SldWorks.Feature
    Dim nErrors                 As Long
    Dim nWarnings               As Long
Sub main()

On Error Resume Next

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Check if there is any Part or Assembly file
If swModel Is Nothing Or swModel.GetType = 3 Then
    MsgBox "Please open a Part/Assembly first and then try again!!"
    Exit Sub
End If

' Enabling the Freeze bar
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swUserEnableFreezeBar, True

'Traverse All configuration Features and hiding the references geometries, origin and sketches


sConfigNameInitial = swApp.GetActiveConfigurationName(swModel.GetPathName)
vConfNameArr = swModel.GetConfigurationNames
    For i = 0 To UBound(vConfNameArr)
        sConfigName = vConfNameArr(i)
        bShowConfig = swModel.ShowConfiguration2(sConfigName)
             
        Set swFeat = swModel.FirstFeature
        
        Do While Not swFeat Is Nothing
            'Hiding 2d sketch, 3d sketch and Origin
            If "ProfileFeature" = swFeat.GetTypeName Or "3DProfileFeature" = swFeat.GetTypeName Or "OriginProfileFeature" = swFeat.GetTypeName Then
                If swFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                    bRet = swFeat.Select2(False, 0): Debug.Assert bRet
                    swModel.BlankSketch
                End If
            End If

                'Hiding planes, axis, co-ordinate system
            If "RefPlane" = swFeat.GetTypeName Or "RefAxis" = swFeat.GetTypeName Or "CoordSys" = swFeat.GetTypeName Then
                If swFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                    bRet = swFeat.Select2(False, 0): Debug.Assert bRet
                    swModel.BlankRefGeom
                End If
            End If
            
            Set swSubFeat = swFeat.GetFirstSubFeature
            Do While Not swSubFeat Is Nothing
                If "ProfileFeature" = swSubFeat.GetTypeName Or "3DProfileFeature" = swSubFeat.GetTypeName Then
                    If swSubFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                        bRet = swSubFeat.Select2(False, 0): Debug.Assert bRet
                        swModel.BlankSketch
                    End If
                End If
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Loop

            Set swFeat = swFeat.GetNextFeature

        Loop

    Next i
    
    swModel.ClearSelection2 True
    bShowConfig = swModel.ShowConfiguration2(sConfigNameInitial)

    swModel.ForceRebuild3 (False) 'Rebuild the model
    swModel.ShowNamedView2 "*Isometric", -1 ' Set the Isometric View
    swModel.ViewZoomtofit2 ' Zoom to fit
    swModel.Extension.RunCommand swCommands_Collapseallitems_Tree, ""  ' Collapse Feature Tree
    swModel.FeatureManager.EditFreeze SwConst.swMoveFreezeBarTo_e.swMoveFreezeBarToEnd, "", True ' Move freeze bar to end of the feature tree
    swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings 'Save the file
       
    MsgBox swModel.GetTitle & " is ready to Check-In!!" 'File ready to check in
  
End Sub

Koen88
Posts: 7
Joined: Tue May 18, 2021 3:26 am
Answers: 1
x 2
x 4

Re: Macro Hide all in parts and assembly

Unread post by Koen88 »

It looks like it works perfectly! Many thanks. I'll test it out properly a few times before batch running it on a bunch of files.

Apologies for the delayed response.
Post Reply