5 Replies Latest reply on May 30, 2017 6:46 AM by Cad Admin

    VBA - Macro Replace Component Function Question

    Cad Admin

      I putting together a macro that i can run on an assembly to automatically replace parts.  Basically im replacing a specific named part with a identical part with a different name from a defined standards library location.

       

      I can get the components & their configurations being used.  However the "replace" is failing

      my problem lies in:  bRet = swAssy.ReplaceComponents2(FileName, CfgName, True, 0, True), need it to return true & im getting false

       

      See Code Below:

       

      Option Explicit

       

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

       

          Dim swApp                   As SldWorks.SldWorks

          Dim swModel                 As SldWorks.ModelDoc2

          Dim swAssy                  As SldWorks.AssemblyDoc

          Dim swSelMgr                As SldWorks.SelectionMgr

          Dim swSelComp               As SldWorks.Component2

          Dim swSelModel              As SldWorks.ModelDoc2

          Dim vChildComp              As Variant

          Dim swChildComp             As SldWorks.Component2

          Dim sPadStr                 As String

          Dim i                       As Long

          Dim CfgName                 As String

          Dim FileName                As String

          Dim bRet                    As Boolean

          Dim nErrors                 As Long

       

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

          Set swAssy = swModel

          Set swSelMgr = swModel.SelectionManager

           

          For i = 0 To nLevel - 1

              sPadStr = sPadStr + "  "

          Next i

          vChildComp = swComp.GetChildren

          For i = 0 To UBound(vChildComp)

              Set swChildComp = vChildComp(i)

              CfgName = swChildComp.ReferencedConfiguration

              Debug.Print sPadStr & "Component name: " & swChildComp.Name2 & ", Component ID: " & swChildComp.GetID

              Debug.Print sPadStr & "Component name: " & CfgName

       

      '-----------------------------------Start Standard INCH Parts---------------------------------------

                              If swChildComp.Name2 Like "*INCH-9752*" Then

                                  FileName = "D:\Inch\INCH-9752-15665.sldprt"

                              End If

       

                              If swChildComp.Name2 Like "*INCH-9566*" Then

                                  FileName = "D:\Inch\INCH-9566-15756.sldprt"

                              End If

      '-----------------------------------End Standard INCH Parts---------------------------------------

                          bRet = swAssy.ReplaceComponents2(FileName, CfgName, True, 0, True)

                          Debug.Print "Replacement component = " & swChildComp.Name2

                          Debug.Print "All instances of old component replaced? " & bRet

                

              TraverseComponent swChildComp, nLevel + 1

       

          Next i

      End Sub

       

      Sub main()

       

          Dim swApp                       As SldWorks.SldWorks

          Dim swModel                     As SldWorks.ModelDoc2

          Dim swConfMgr                   As SldWorks.ConfigurationManager

          Dim swConf                      As SldWorks.Configuration

          Dim swRootComp                  As SldWorks.Component2

          Dim swSelMgr                    As SldWorks.SelectionMgr

       

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

          Set swConfMgr = swModel.ConfigurationManager

          Set swConf = swConfMgr.ActiveConfiguration

          Set swRootComp = swConf.GetRootComponent3(True)

          'Debug.Print "File = " & swModel.GetPathName

          If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then

              TraverseComponent swRootComp, 1

          End If

       

      End Sub

        • Re: VBA - Macro Replace Component Function Question
          Peter Brinkhuis
          • Are you sure the file in the path exists and that SolidWorks can see it? You can use the DIR command to find files.
          • Are you sure a match is found for the file name? You now have two independent if statements (that could both be true) and you replace the component regardless of the outcome of those statements. You'd better create a better structure for this.
          • Is the config name exactly the same in the new and the old part?
          • Are the part names different? It can not replace parts with identical names.
          • You selected 0 as the fourth parameter, which means SW will find an identical configuration anyway. You can probably pass an empty string as the second argument, the docs don't mention what happens if you pass the combination of arguments like you do.
          • Last but not least: the docs mention you can only replace parts one layer deep. Otherwise you would need to open the subassembly first.
          • Re: VBA - Macro Replace Component Function Question
            Cad Admin
            • Are you sure the file in the path exists and that SolidWorks can see it? You can use the DIR command to find files.

            Yes path exist, all my standards are located there.  Filename resolves fine

             

            • Are you sure a match is found for the file name? You now have two independent if statements (that could both be true) and you replace the component regardless of the outcome of those statements. You'd better create a better structure for this.

            Yes, Filename resolves the part filename correct. In the array, i would only evaluate one true to one if statement per file.  I'm a little light on efficient structures...lol

             

            • Is the config name exactly the same in the new and the old part?

            Yes, Files are Identical except there was a prefix added at sometime.

             

            • Are the part names different? It can not replace parts with identical names.

            Yes part names are different due to the "prefix"

             

            • You selected 0 as the fourth parameter, which means SW will find an identical configuration anyway. You can probably pass an empty string as the second argument, the docs don't mention what happens if you pass the combination of arguments like you do.

            Yes i replaced CfgName with "", and it works better.  If i define CfgName & Replace all instances it replaces all with the same Configuration.  If i use "" it replaces the instance with the correct configuration. so i will be removing the CfgName

             

            • Last but not least: the docs mention you can only replace parts one layer deep. Otherwise you would need to open the subassembly first.

            Unfortunately I read that too...LOL

            • Re: VBA - Macro Replace Component Function Question
              Cad Admin

              ok,

               

              I rolled it back to a simpler macro based off a help file...& it works.

               

              Sub main()

               

                  Dim FileName            As String

                  Dim CfgName             As String

                  Dim swApp               As SldWorks.SldWorks

                  Dim swModel             As SldWorks.ModelDoc2

                  Dim swAssy              As SldWorks.AssemblyDoc

                  Dim swSelMgr            As SldWorks.SelectionMgr

                  Dim swSelComp           As SldWorks.Component2

                  Dim status              As Boolean

               

                  Set swApp = CreateObject("SldWorks.Application")

                  Set swModel = swApp.ActiveDoc

                  Set swAssy = swModel

                  Set swSelMgr = swModel.SelectionManager

                  Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)

               

              '-----------------------------------Start Standard INCH Parts---------------------------------------

                                      If swSelComp.Name2 Like "*INCH-9752*" Then

                                          FileName = "D:\Inch\INCH-9752-15665.sldprt"

                                      End If

               

                                      If swSelComp.Name2 Like "*INCH-9566*" Then

                                          FileName = "D:\Inch\INCH-9566-15756.sldprt"

                                      End If

              '-----------------------------------End Standard INCH Parts---------------------------------------

                  status = swAssy.ReplaceComponents2(FileName, "", True, 0, True)

               

              End Sub

               

              So it looks like the difference between 1 & 2 is the array & how the part is selected in the assembly.

               

              Any thoughts on how to get the array to work...