AnsweredAssumed Answered

VBA API: Using EditRebuild3 while Traversing Assembly

Question asked by Cad Admin on Jun 28, 2017
Latest reply on Jul 6, 2017 by Cad Admin

I have a macro that a coworker wrote it worked fine, i went back to it a few days later and it is not completing its task.  Its supposed to traverse through an assembly find all of the customer parts (in this case models with 256666 in the name, then run EditRebuild3.  While it work when he saved it a few days ago, it is failing now.  It traverses fine, fails on the EditRebuild3 on the qualifying models.


Option Explicit


Sub main()


    Dim swApp                     As SldWorks.SldWorks

    Dim swModel                   As SldWorks.ModelDoc2

    Dim swConfigMgr               As SldWorks.ConfigurationManager

    Dim swAssy                    As SldWorks.AssemblyDoc

    Dim swConf                    As SldWorks.Configuration

    Dim swRootComp                As SldWorks.Component2


    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swConfigMgr = swModel.ConfigurationManager

    Set swConf = swConfigMgr.ActiveConfiguration

    Set swRootComp = swConf.GetRootComponent3(True)


    TraverseComponent swRootComp, 1


End Sub


Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)


    Dim vChildComp                  As Variant

    Dim swChildComp                 As SldWorks.Component2

    Dim swCompConfig                As SldWorks.Configuration

    Dim i                           As Long

    Dim bRet                        As Boolean

    Dim swCompModel                 As SldWorks.ModelDoc2

    Dim boolstatus                  As Boolean

    Dim Part                        As Object

    Dim PartName                    As String


    Set swCompModel = swComp.GetModelDoc2


    If swCompModel.GetType() = swDocPART Then

    'If swCompModel.GetType = swDocASSEMBLY Then

    Debug.Print "Component: " & swComp.Name2


        bRet = swComp.Select4(True, Nothing, True)

    End If


    vChildComp = swComp.GetChildren


    For i = 0 To UBound(vChildComp)

        Set swChildComp = vChildComp(i)


        TraverseComponent swChildComp, nLevel + 1


        If InStr(swChildComp.Name2, "256666") Then

            'Debug.Print "Component: " & swChildComp.Name2

            Set Part = swCompModel

            PartName = swChildComp.Name2

            boolstatus = Part.Extension.SelectByID2(PartName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

            boolstatus = Part.EditRebuild3()

        End If


        Next i


    swCompModel.ClearSelection2 True


End Sub