How to change the tree display via a macro

Hi all.

We currently have our solidworks files with an assembly or part tree that is not very readable because the name of the configuration is visible.
image

I found how to correct this by hand on each file but it's super long because it's saved in the file options and not those of SW so to do for each file
So I'd like to do a macro that does this on the assembly that I'm opening up as well as all of its components. This would allow me to be much more efficient.

Does anyone have an idea or clue for this macro to arrive after the macro at these options regardless of the options selected or not in the base file (because not all files necessarily have the same options selected.
image

Here's an example that ticks the description:

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim SelMgr As SldWorks.SelectionMgr
Dim swFeatMgr As SldWorks.FeatureManager
Sub main()
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    Set SelMgr = Part.SelectionManager
    Set swFeatMgr = Part.FeatureManager
    
    ' Show Component Descriptions is set to true
    swFeatMgr.ShowComponentDescriptions = True
    ' Show Component Configuration Names is set to false
    swFeatMgr.ShowComponentConfigurationNames = True
     ' Show Component Configuration Descriptions is set to false
    swFeatMgr.ShowComponentConfigurationDescriptions = False
    ' Show Component Names
    swFeatMgr.ShowComponentNames = True
     
End Sub

For the options that are checked for us:
image
And the example of SW:
https://help.solidworks.com/2021/english/api/sldworksapi/Show_Components_and_Component_Configurations_Names_and_Descriptions_Example_VB.htm
With this you should be able to move forward.

5 Likes

Ok thank you I'll set this up and come back to close if I have everything I need.
But already a big thank you!

Well I can actually almost do what I want by slightly adjusting your macro
(I can hide and show what I want.

The thing is, I'd like this to act on all the files contained in the assembly that I'm opening.

Let me explain:

I open an assembly and run this macro, it gives me this:
image

But if I open the highlighted file, it gives me the following:
image

whereas I would like to have the same display as for assembly.

So the macro would have to run for each child file in the assembly.

Any idea of how to go through all these children?
sorry I have ideas but no knowledge or almost...

To browse each subassembly and part of your assembly you have to use Traverse components and insert your code where you need to (see if you need to open the part or not):
https://help.solidworks.com/2020/English/api/sldworksapi/Traverse_Assembly_at_Component_Level_Example_VB.htm

or again:

Always the same type of code:

Or another solution take a macro that does batch processing such as Integration:

3 Likes

Well, with your help, I managed to do something that is probably not optimal but seems to work. The openness of children is indeed necessary.

if it can help

'**********************
'Copyright(C) 2023 Xarial Pty Limited
'Reference: Traversing the components tree using SOLIDWORKS API
'License: License
'**********************
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim Part As SldWorks.ModelDoc2
Sun SelMgr As SldWorks.SelectionMgr
Dim swFeatMgr As SldWorks.FeatureManager
Dim path_complete As String' Full Way of the Piece
Dim myError As Long
Dim myWarning As Long

Dim quotation mark As String
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Dim ext As String
Const INDENT_SYMBOL As String = " "

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Set swFeatMgr = Part.FeatureManager


' Show Component Descriptions is set to true
swFeatMgr.ShowComponentDescriptions = True
' Show Component Configuration Names is set to false
swFeatMgr.ShowComponentConfigurationNames = True
 ' Show Component Configuration Descriptions is set to false
swFeatMgr.ShowComponentConfigurationDescriptions = False
' Show Component Names
swFeatMgr.ShowComponentNames = False

swFeatMgr.ShowDisplayStateNames = False

    Dim swRootComp As SldWorks.Component2

    Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent

    TraverseComponent swRootComp, ""
    


Else

    MsgBox "Please open assembly"

End If

End Sub

Sub TraverseComponent(comp As SldWorks.Component2, indent As String)

Dim vChildComps As Variant

vChildComps = comp.GetChildren

Dim i As Integer

For i = 0 To UBound(vChildComps)

    Dim swChildComp As SldWorks.Component2
    Set swChildComp = vChildComps(i)
        
    Debug.Print indent & swChildComp.Name2 & " (" & swChildComp.GetPathName() & ")"
    
    TraverseComponent swChildComp, indent & INDENT_SYMBOL
   
   
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

' Set SelMgr = Part.SelectionManager
' Set swFeatMgr = Part.FeatureManager

' Opens the loop file

ext = Right(swChildComp.GetPathName(), 6)

'If it's a part
If ext = "sldprt" Then

Set Part = swApp.OpenDoc6(swChildComp.GetPathName(), 1, 0, "", longstatus, longwarnings)
Else
'It's not a loan'
If ext = "sldasm" Then

'////aSM
Set Part = swApp.OpenDoc6(swChildComp.GetPathName(), 2, 0, "", longstatus, longwarnings)

Else
MsgBox "pas d'extension trouvée"
End If

End If

''Modifies the display of the SW tree

Set SelMgr = Part.SelectionManager
Set swFeatMgr = Part.FeatureManager

' Show Component Descriptions is set to true
swFeatMgr.ShowComponentDescriptions = True
' Show Component Configuration Names is set to false
swFeatMgr.ShowComponentConfigurationNames = True
 ' Show Component Configuration Descriptions is set to false
swFeatMgr.ShowComponentConfigurationDescriptions = False
' Show Component Names
swFeatMgr.ShowComponentNames = False

swFeatMgr.ShowDisplayStateNames = False

' MsgBox ' before closing the file

''ferme le fichier de la boucle

swApp.CloseDoc Part.GetPathName

Set Part = Nothing

Next

End Sub

1 Like