Can empty exploded steps be automatically deleted?

Hello

I regularly work with blends that have undergone many evolutions. When I rework the exploded view, I realize that some stages of explosion are still present but are no longer attached to any element of the assembly (legacy of the past...)
This significantly complicates the understanding of the exploded view and it annoys me to open each step to see if it still makes sense...

Do you know of a way to clean up the exploded view tree (either automatically or by a macro)?

Thanks in advance!

1 Like

After a quick search, we don't find much in macro on the subject of explodes.
The only topic that was suitable in every way never received the slightest answer:

Unless one of our macro experts finds something, it's not really easy.

1 Like

I don't have too much time to dig at the moment, but I have the impression that there would be some grist to grind here:

https://help.solidworks.com/2021/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ipartexplodestep_members.html

Yes indeed counting the number of bodies (GetBodies) in a step it is surely playable.

1 Like

Hello @s.descamps ,

Listing the configurations, searching for each one if there are one or more exploded views, listing each explode step and the number of components it contains is quite possible, and then deleting the step if it contains no components.

Question: how to get a configuration with an exploded where at least one step contains no components. I can't get it, neither at creation, nor by changing the stage...
It seems that SolidWorks is cleaning up its own mess.
Can you share an example of an assembly with an exploded step empty of components?

2 Likes

Thank you for your answer.
Indeed when I test a new file, SW seems to clean up when you remove a component.
I think my problem concerns more old assemblies, created in an old version and having undergone many evolutions.
Unfortunately, for privacy reasons, I won't be able to post a concrete example here.

1 Like

The macro attachment is a way to answer your question.
In the absence of a workable example, it is impossible to go beyond this draft, with extensive testing and considering different possible situations.
Kind regards.
MacroEclate.swp (60.5 KB)

1 Like

Thank you very much m.blt :grinning: !
I'll look at that

I get a "type mismatch" message on the following line:
viewNames = swAssemb.GetExplodedViewNames2(swConfig.Name)

Yet this function must return a "string" :thinking: type?

OK

There is a lack of error handling.
When I'm on the Default config no problem because an exploded view is present.
On the other hand on the symmetry config same message incompatibility because impossible to find an exploded View in my opinion, hence the error message I think.
image

That's what I was saying when I checked before if an exploded view is present it works, it just lacked this error handling:

Option Explicit

        Dim swApp       As SldWorks.SldWorks
        Dim swModel     As SldWorks.ModelDoc2
        Dim swAssemb    As SldWorks.AssemblyDoc
        Dim swConfig    As SldWorks.Configuration
        Dim cfgNames()  As String
        Dim viewNames() As String
        Dim noCfg       As Long
        Dim explStep    As SldWorks.ExplodeStep
        Dim nbSteps     As Long
        Dim noStep      As Long
        Dim noView      As Long
        Dim nbComps     As Long
  

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssemb = swModel
    
    cfgNames = swModel.GetConfigurationNames
    
    For noCfg = 0 To UBound(cfgNames)                           'Boucle sur toutes les configurations
        swModel.ShowConfiguration2 (cfgNames(noCfg))            'Sélection de la configuration
        swModel.ForceRebuild3 False
        Set swConfig = swModel.ConfigurationManager.ActiveConfiguration
        If swAssemb.GetExplodedViewCount2(swConfig.Name) > 0 Then 'On vérifie le nb de vue éclaté
        viewNames = swAssemb.GetExplodedViewNames2(swConfig.Name)   'Vues éclatées de la config
        End If
        For noView = 0 To UBound(viewNames)                         'Boucle sur les vues éclatées
            swAssemb.ShowExploded2 True, viewNames(noView)          'Activation de la vue éclatée
        
            nbSteps = swConfig.GetNumberOfExplodeSteps              'Nombre d'étapes d'éclatement
            noStep = 1
            While noStep <= nbSteps                                 'Boucle sur les étapes d'éclatement
                Set explStep = swConfig.GetExplodeStep(noStep - 1)  'Sélection de l'étape
                If Not (explStep Is Nothing) Then
                    nbComps = explStep.GetNumOfComponents           'Nbre de composants dans l'étape
                    If nbComps = 0 Then                             'si le nbre de composants dans l'étape est nul
                        swConfig.DeleteExplodeStep (explStep.Name)      'Suppression de l'étape
                        nbSteps = swConfig.GetNumberOfExplodeSteps      'Mise à jour du nombre d'étapes
                    Else
                        noStep = noStep + 1                             'étape suivante
                    End If
                End If
            Wend
        Next noView                                             'Vue suivante
    Next noCfg                                                  'Configuration suivante
   
End Sub

Thank you! We are making progress :wink:

I get a message "The hint does not belong to the selection" on the following line:

For noView = 0 To UBound(viewNames)   'Boucle sur les vues éclatées

I don't have this error but try this instead (if no exploded view in the following config-> config):

Option Explicit

        Dim swApp       As SldWorks.SldWorks
        Dim swModel     As SldWorks.ModelDoc2
        Dim swAssemb    As SldWorks.AssemblyDoc
        Dim swConfig    As SldWorks.Configuration
        Dim cfgNames()  As String
        Dim viewNames() As String
        Dim noCfg       As Long
        Dim explStep    As SldWorks.ExplodeStep
        Dim nbSteps     As Long
        Dim noStep      As Long
        Dim noView      As Long
        Dim nbComps     As Long
  

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssemb = swModel
    
    cfgNames = swModel.GetConfigurationNames
    
    For noCfg = 0 To UBound(cfgNames)                           'Boucle sur toutes les configurations
        swModel.ShowConfiguration2 (cfgNames(noCfg))            'Sélection de la configuration
        swModel.ForceRebuild3 False
        Set swConfig = swModel.ConfigurationManager.ActiveConfiguration
        If swAssemb.GetExplodedViewCount2(swConfig.Name) > 0 Then   'Si une vue éclaté est trouvée
            viewNames = swAssemb.GetExplodedViewNames2(swConfig.Name)   'Vues éclatées de la config
            For noView = 0 To UBound(viewNames)                         'Boucle sur les vues éclatées
                swAssemb.ShowExploded2 True, viewNames(noView)          'Activation de la vue éclatée
            
                nbSteps = swConfig.GetNumberOfExplodeSteps              'Nombre d'étapes d'éclatement
                noStep = 1
                While noStep <= nbSteps                                 'Boucle sur les étapes d'éclatement
                    Set explStep = swConfig.GetExplodeStep(noStep - 1)  'Sélection de l'étape
                    If Not (explStep Is Nothing) Then
                        nbComps = explStep.GetNumOfComponents           'Nbre de composants dans l'étape
                        If nbComps = 0 Then                             'si le nbre de composants dans l'étape est nul
                            swConfig.DeleteExplodeStep (explStep.Name)      'Suppression de l'étape
                            nbSteps = swConfig.GetNumberOfExplodeSteps      'Mise à jour du nombre d'étapes
                        Else
                            noStep = noStep + 1                             'étape suivante
                        End If
                    End If
                Wend
            Next noView                                             'Vue suivante
        End If
    Next noCfg                                                  'Configuration suivante
   
End Sub

Just moved the end if to include all the rest of the code if no exploded view in the config

Good news: Great, it's running!
Bad news: on my test subset, the empty step is not deleted :exploding_head:

I put a spy on nbComps, and he sees a component on the step in question

1 Like

I specify that on the other stages, I have the right number of components

1 Like

I am indeed reproducing the problem under SW2020
If I delete a component from my chain 2, it finds it for me in the chain step 2 at N°1
While in 2 he can't find it anymore. And in 3 the part is no longer in the tree view since it was deleted after the creation of the step.

On the other hand, it's not a code problem but a solidworks problem since manually we reproduce the part bug visible in the step...
Not sure if I could do anything more.

While testing, I realized that "GetComponentName" returns the value "–NA–" if there is no component attached.

So I added this condition to the test:

Option Explicit

        Dim swApp       As SldWorks.SldWorks
        Dim swModel     As SldWorks.ModelDoc2
        Dim swAssemb    As SldWorks.AssemblyDoc
        Dim swConfig    As SldWorks.Configuration
        Dim cfgNames()  As String
        Dim viewNames() As String
        Dim noCfg       As Long
        Dim explStep    As SldWorks.ExplodeStep
        Dim nbSteps     As Long
        Dim noStep      As Long
        Dim noView      As Long
        Dim nbComps     As Long
        Dim NbSup       As Integer
        Dim Comp As Variant
              

Sub main()

Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssemb = swModel
    
    NbSup = 0
    cfgNames = swModel.GetConfigurationNames
    
    For noCfg = 0 To UBound(cfgNames)                           'Boucle sur toutes les configurations
        swModel.ShowConfiguration2 (cfgNames(noCfg))            'Sélection de la configuration
        swModel.ForceRebuild3 False
        Set swConfig = swModel.ConfigurationManager.ActiveConfiguration
        If swAssemb.GetExplodedViewCount2(swConfig.Name) > 0 Then 'On vérifie le nb de vue éclaté
        viewNames = swAssemb.GetExplodedViewNames2(swConfig.Name)   'Vues éclatées de la config
        For noView = 0 To UBound(viewNames)                         'Boucle sur les vues éclatées
            swAssemb.ShowExploded2 True, viewNames(noView)          'Activation de la vue éclatée
        
            nbSteps = swConfig.GetNumberOfExplodeSteps              'Nombre d'étapes d'éclatement
            noStep = 1
            While noStep <= nbSteps                                 'Boucle sur les étapes d'éclatement
                Set explStep = swConfig.GetExplodeStep(noStep - 1)  'Sélection de l'étape
                If Not (explStep Is Nothing) Then
                    nbComps = explStep.GetNumOfComponents           'Nbre de composants dans l'étape
                    Comp = explStep.GetComponentName(0)
                    If nbComps = 0 Or Comp = "--NA--" Then              'si le nbre de composants dans l'étape est nul
                        swConfig.DeleteExplodeStep (explStep.Name)      'Suppression de l'étape
                        nbSteps = swConfig.GetNumberOfExplodeSteps      'Mise à jour du nombre d'étapes
                        NbSup = NbSup + 1                               'Incrémente le compteur
                    Else
                        noStep = noStep + 1                             'étape suivante
                    End If
                End If
            Wend
        Next noView                                             'Vue suivante
    End If
    Next noCfg                                                  'Configuration suivante

   MsgBox NbSup & " étapes supprimées"                          'Affiche le nombre d'étapes supprimées

 End Sub

To be tested but it could maybe do the job

Edit:
I changed the line

                    Comp = explStep.GetComponentName(0)

because the value (1) returned the 2nd component, and we deleted the steps with only one component attached.
I'm testing with different assemblies but for the moment it looks good.

1 Like

Thank you sbadenis and m.blt for your contributions :+1: !