32 Replies Latest reply on Jul 6, 2017 10:05 AM by Cad Admin

    VBA API: Using EditRebuild3 while Traversing Assembly

    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

        • Re: VBA API: Using EditRebuild3 while Traversing Assembly
          John Alexander

          Does it fail on the same models that it was tested on originally? If so, do the models that it fails on contain subassemblies?

           

          I'm wondering this because the remarks section of the EditRebuild3 documentation page says:

          This method only works in-context of the active document.

          • Re: VBA API: Using EditRebuild3 while Traversing Assembly
            Roland Schwarz

            I wouldn't run the rebuild in the middle of the traverse. I would use the traverse to collect the desired models, purge duplicates, then rebuild each of the models in the collection.

            • Re: VBA API: Using EditRebuild3 while Traversing Assembly
              Josh Brady

              Umm, I'm with Peter on this one.  What's the goal of this macro?  Seems you could achieve the same result just by hitting "rebuild" on your top-level assembly?

               

              Alternately, if you really want to rebuild these files individually you'd be much more efficient by looking at the assembly references rather than traversing the feature tree.  The feature tree method will rebuild everything as many times as it finds it.

              • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                Amen Allah Jlili

                John Alexander EditRebuild3 will only rebuild feature that requires rebuilding.

                  • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                    Cad Admin

                    I'll have to check to see when it runs and when it doesnt. Is there a way to check if it requires rebuild, to know if it needs to be run?

                     

                    I know it's not working as stated above.  I tested it by changing the NC part, then saved it.  I then went to the solid works model...and the macro didn't update it.  it did update when I manually update it.  I believe it is because of the way part name is formated, between the recorded macro and my coworkers.

                      • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                        Matt Camacho-Cook

                        This is a Function that I call from another macro to check for rebuild errors.  No guarantee it will catch rebuild errors on an interconnect model, but you can give it a try.  You'll want to do something useful at the appropriate Debug.Print lines if/when a rebuild error is found.

                         

                        Private Sub IsPartErrorFree(swModel As SldWorks.ModelDoc2)
                            '
                            ' Checks to make sure no rebuild errors exist.
                            '
                            
                            Dim swFeature As SldWorks.Feature
                            Dim featureName As String
                            Dim isWarning As Boolean
                            Dim error As Long
                            Dim errorCount As Integer
                            
                            errorCount = 0
                            
                            ' Get the first feature in part.
                            Set swFeature = swModel.FirstFeature
                            
                            ' While we have a valid feature
                            Do While Not swFeature Is Nothing
                                ' Get the name of the feature
                                featureName = swFeature.Name
                                
                                ' Check for errors.
                                error = swFeature.GetErrorCode2(isWarning)
                                
                                ' Only examine if the error code is not zero.
                                If error <> 0 Then
                                    ' Error exists.
                                    
                                    errorCount = errorCount + 1
                                    
                                    Debug.Print "Error:          " & error
                                    Debug.Print "Warning or not: " & isWarning
                                End If
                                
                                ' Get the next feature.
                                Set swFeature = swFeature.GetNextFeature()
                                
                            ' Continue until no more features exist.
                            Loop
                            
                            If errorCount = 0 Then
                                Debug.Print "No rebuild errors found."
                            Else
                                Debug.Print "Qty " & errorCount & " rebuild errors found."
                            End If
                        End Sub
                        
                        
                        
                        

                         

                         

                        -Matt

                        • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                          Amen Allah Jlili

                          After you create/edit a feature, call EditRebuild3.

                      • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                        Cad Admin

                        Still having issues with just getting this to run.

                         

                        I think i need to return the part name the same as the Recorded macro

                         

                        I noticed that swDoc.GetTitle returns:

                        "Copy of model256666.prt^test.sldprt"

                         

                        while the Recorded macro is:

                        "Copy of model256666.prt^test-1@test"

                         

                        Just a thought

                          • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                            Peter Brinkhuis

                            If the name's different, the result's different. In programming everything has to be exactly right, or you have to catch all possible options.

                             

                            Try to break the macro into tiny pieces and test those subs or functions independently. Read the help for the problematic function very carefully (yes again, I keep reading over many important details), add break points and see what happens in every line. That is the only way to get code working properly.

                              • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                Cad Admin

                                Almost have it, i forced it to run after matching the naming format...

                                 

                                My question now is on the recorded macro there is a "-1" before the @...How can i retrieve that...is that the instance number?

                                I got the macro to work by adding it in manually....this fixes it in the test, but ill need to fix it to run it on anything else.

                                  • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                    Peter Brinkhuis

                                    I hope that I'm wrong, but it feels like you're just trying to change something and see if it magically works. I really urge you to look in the help first, that's what I did just now.

                                     

                                    If you're working with an assembly, you need the instance number for your code to know which component instance to select. You can get the name using Name2 on a component object.

                                    2012 SOLIDWORKS API Help - Name2 Property (IComponent2)

                                      • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                        Cad Admin

                                        Peter,

                                        I tried to use the Name2 previously but couldn't get it to return anything..  I did what i did in the code to prove that the naming was the issues.  My current "dirty" code is below.  I believe, like you said the name2 would be a solution...but it escapes me.

                                         

                                        Const MAGICWORD     As String = "256666"
                                        Dim swApp           As SldWorks.SldWorks
                                        Dim swDoc           As SldWorks.ModelDoc2
                                        Dim aDocs           As Variant
                                        Dim i               As Long

                                         

                                        Dim RetFile         As String
                                        Dim RetAsm          As String
                                        Dim NewRetFile      As String
                                        Dim NewRetASM       As String

                                         

                                        Dim DocName         As String
                                        Dim Model           As Object
                                        Dim CombineStr      As String
                                              

                                        Sub main()

                                         

                                        Set swApp = Application.SldWorks
                                        Set swDoc = swApp.ActiveDoc
                                        Dim swAssy      As SldWorks.AssemblyDoc
                                        Set swAssy = swDoc
                                        Set Model = swApp.ActiveDoc

                                           

                                        swAssy.ResolveAllLightWeightComponents False
                                        aDocs = swApp.GetDocuments

                                           

                                        For i = 0 To UBound(aDocs)
                                            Set swDoc = aDocs(i)
                                            DocName = swDoc.GetTitle
                                              
                                            If InStr(1, DocName, MAGICWORD, vbTextCompare) > 0 Then
                                          
                                                RetAsm = Model.GetTitle
                                                RetFile = swDoc.GetTitle
                                                NewRetASM = Left(Model.GetTitle, InStrRev(Model.GetTitle, ".") - 1)
                                                NewRetFile = Left(swDoc.GetTitle, InStrRev(swDoc.GetTitle, ".") - 1)
                                              
                                                CombineStr = NewRetFile & "-1@" & NewRetASM
                                                Debug.Print "Combined Name Selection: " & CombineStr
                                              
                                                Set Part = swApp.ActiveDoc
                                                boolstatus = Model.Extension.SelectByID2(CombineStr, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
                                                boolstatus = Model.EditRebuild3()
                                                Debug.Print "Combined Name Selection: " & CombineStr
                                            End If
                                        Next i

                                        End Sub

                                          • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                            Josh Brady

                                            I think the underlying question that SolidWorks needs to answer is how to tell an Interconnect part through the API to re-check the source.  This should be a consistent method that works on a feature or a modeldoc2, regardless of context.  As Peter said, you're currently shooting in the dark. 

                                             

                                            I don't see anything at all in the API help regarding interconnect.  This is either a hole in the documentation or a hole in the API.

                                             

                                            I believe the correct route would be to contact your VAR.  Or you could contact APIsupport at solidworks dot com directly if your VAR does nothing but R rather than A any V.

                                              • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                                Cad Admin

                                                I agree, nothing in the API on Interconnect yet.  Rebuild will rebuild all regular models on the tree, but it will only "check" the interconnect models and will show a image prompting it needs updating.  However, i believe the only option is for the user manually update it...either by right click update or as i am trying to do...through a macro.

                                                 

                                                <<<Out of Date, Needs Updating..."refresh" icon on an interconnect model in an assembly

                                    • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                      Josh Brady

                                      Soo... That's what I'm saying.  The things that your recorded macro does are not special.  The code in your recorded macro only selects a component in the feature tree and then rebuilds the assembly.  It doesn't rebuild the part.  It doesn't "update" the part.  It  doesn't do anything to the part except select it in the assembly's feature tree.  If you want to be confident that your code fixes out of date interconnect parts, you need some word/documentation/verification that you're doing the proper thing that tells SolidWorks to update interconnect parts.

                                        • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                          Cad Admin

                                          Josh,

                                          Yes...agreed..this is a dirty rebuild for interconnect parts only.

                                           

                                          The macro currently works, the hangup is the proper "formatting" of the SelectionID name.  In this case it needs to carry an instance id which i would normally get with Name2.  I haven't got Name2 to work, hence hard-coding it in using " & "-1@" &".  But this most likely work 100% of the time

                                           

                                          A coworker originally made it to update his models due to some of the UG NX models are still be developed.

                                           

                                          And FYI, EditRebuild3() when run on an interconnect part ...Updates the model.

                                        • Re: VBA API: Using EditRebuild3 while Traversing Assembly
                                          Cad Admin

                                          My dirty little solution, thanks everyone:

                                           

                                          Const MAGICWORD   As String = "256666"
                                          Dim swApp         As SldWorks.SldWorks
                                          Dim swModel       As SldWorks.ModelDoc2
                                          Dim swAssy         As SldWorks.AssemblyDoc
                                          Dim swComp         As SldWorks.Component2
                                          Dim i             As Integer
                                          Dim vComps         As Variant

                                           

                                          Dim RetAsm         As String
                                          Dim NewRetASM     As String
                                          Dim PartName       As String

                                           

                                          Dim model         As Object
                                          Dim CombineStr     As String
                                              

                                          Sub main()

                                           

                                          Set swApp = Application.SldWorks
                                          Set swModel = swApp.ActiveDoc
                                          Set swAssy = swModel
                                          Set model = swApp.ActiveDoc

                                            

                                          swAssy.ResolveAllLightWeightComponents False
                                          vComps = swAssy.GetComponents(True)

                                            

                                          For i = 0 To UBound(vComps)
                                             Set swComp = vComps(i)
                                             PartName = swComp.Name2
                                             If InStr(1, PartName, MAGICWORD, vbTextCompare) > 0 Then
                                                 RetAsm = model.GetTitle
                                                 NewRetASM = Left(model.GetTitle, InStrRev(model.GetTitle, ".") - 1)
                                              
                                                 CombineStr = PartName & "@" & NewRetASM
                                              
                                                 boolstatus = model.Extension.SelectByID2(CombineStr, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
                                                 'boolstatus = model.Extension.RunCommand(2726, "")  <<<<<<<<<<<<<<<Isolate
                                                 boolstatus = model.EditRebuild3()
                                                     Debug.Print "NX Interconnect Model: " & CombineStr
                                             End If
                                          Next i
                                          model.ClearSelection2 True

                                          End Sub