SW2020 Macro / Issues in VBA Code

Hi all

Being a designer/editor by trade and a little coder of passion, I tackled stronger than me in VBA macro coding. 

My goal is to be able to lower the recovery bar in all parts of an assembly. To do this, I would like the macro to select and open, one by one, the parts (with or without an on-screen display) and then move down the resume bar in the tree and save the part. If in my set there were to be a subset, I would like him to open this subset (same as for the parts, with display or not) and start opening the parts one by one again to start the first step again.

Here is a little diagram:

SW ASSEMBLY-----> Open part and ass (which are not deleted) -------> If it's a part --------> then go down the resume bar + save + close

                                                                                                                                I

                                                                                                                                I-----> If it is an assembly, do the same as box 2

here is the code and where I am, it's a code that I found on the Web, but which apparently was coded a little "in a hurry" on this forum: https://forum.solidworks.com/thread/159206

My code corrected as best I could :                            The problem with my code is that there are errors everywhere... error 91, then sometimes 13.... I am lost and would need the help of one of                                                                                       you who knows about it!! Thanks in advance;)

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swModel1 As SldWorks.ModelDoc2

Dim swAssembly As SldWorks.AssemblyDoc

Dim CompNum As Integer

Dim CompNum1 As Integer

Dim components As SldWorks.Component2

Dim components1 As SldWorks.Component2

Dim component As SldWorks.Component2

Dim component1 As SldWorks.Component2

Sub main()


    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swAssembly = swModel
        
    ' Get number of components, not including child components in subassemblies

    CompNum = swAssembly.GetComponentCount(True)
    
    components = swAssembly.GetComponents(True)
    
    Dim i as Integer
    
    For i = 0 To CompNum
 
        component = components(i)

swModel1 = component. GetModelDoc2

          'open part from assembly function (if subassembly, create another for-loop with same procedure)

If swModel1.GetType = swDocPART Then

   Set swModel = swApp.ActivateDoc3(component. Name2, False, swRebuildOnActivation_e.swUserDecision, 0)

    boolstatus = share. FeatureManager.EditRollback(swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "")
    boolstatus = share. EditRebuild3()

swModel.SaveAs (component. GetPathName)

 swApp.CloseDoc component. GetPathName

Set swModel = swApp.ActiveDoc

 ElseIf swModel1.GetType = swDocASSEMBLY Then

CompNum1 = swModel1.GetComponentCount(True)

components1 = swModel1.GetComponents(True)

    Sun j As Integer
    
    For j = 0 TB CompNum1

            component1 = components1(j)

swModel1 = component1. GetModelDoc2


   Set swModel = swApp.ActivateDoc3(component. Name2, False, swRebuildOnActivation_e.swUserDecision, 0)

    boolstatus = share. FeatureManager.EditRollback(swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "")
    boolstatus = share. EditRebuild3()
    

swModel.SaveAs (component. GetPathName)

 swApp.CloseDoc component. GetPathName

Next

End If

Next

End Sub

 

Hello. Try this:

Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
    Dim swModel As SldWorks.ModelDoc2
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swConf = swModel.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
    TraverseComponent swRootComp
End Sub

Sub TraverseComponent(swComp As SldWorks.Component2)
    Dim vChildComp As Variant
    Dim swChildComp As SldWorks.Component2
    Dim swCompConfig As SldWorks.Configuration
    Dim i As Long
    vChildComp = swComp.GetChildren
    For i = 0 To UBound(vChildComp)
        Set swChildComp = vChildComp(i)
        TraverseComponent swChildComp
    Next i
    RollBack swComp
End Sub

Sub RollBack(swComp As SldWorks.Component2)
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swComp.GetModelDoc2
    swApp.ActivateDoc3 swComp.Name2, False, swRebuildOnActivation_e.swRebuildActiveDoc, Empty
    If swModel.GetType <> swDocumentTypes_e.swDocPART Then Exit Sub
    swModel.FeatureManager.EditRollback swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, Empty
    swModel.EditRebuild3
    swModel.Save3 swSaveAsOptions_e.swSaveAsOptions_Silent, Empty, Empty
    swApp.CloseDoc swModel.GetPathName
End Sub

 

3 Likes

Hello JeromP,

Your code works perfectly fine!! It's shorter than mine and uses functions as far as I can see, where did you find the necessary resources if it's not too intrusive?

In any case, thank you very much for your quick and efficient help!!  

Hello @JeromeP ,

really nice macro; Would it be possible to modify it so that it goes down the blocking bar?

If so, I open a new question for this one?

@theo.hecht: No problem. I adapted the code found here: Traverse_Assembly_at_Component_and_Feature_Level_Example

@ac cobra 427: I'm not sure what the lock bar is. This macro moves down the recovery bar. Is the blocking bar a different bar from this one?

Edit: I just found the blocking bar.

To lower it, replace: swModel.FeatureManager.EditRollback swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, Empty

by: swModel.FeatureManager.EditFreeze2 swMoveFreezeBarTo_e.swMoveFreezeBarToEnd, Empty, True, True

 

Hello

The block bar is the yellow bar found on parts files, the function to use is EditFreeze2.

Kind regards

1 Like

Yes, I just saw that. I edited my previous answer.

@JeromeP Thank you! :)

Kind regards

  @JeromeP yes it's a bar that locks the functions, which prevents the reconstruction of the parts. These save time when opening ASMs. On the other hand I replaced the snippet of code and it doesn't work. I'm going to open a question, so it'll be easier if someone is looking for this type of macro