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