2 Replies Latest reply on Jun 15, 2016 6:50 AM by Chiheb Gouta

    Opening Parts in an Assembly using VBA?

    Michael Chamerski

      Hey everyone!

       

      I have a question as far as opening components within an assembly using VBA. Is it possible, if at all, to create a macro that will open parts, in order, from the tree of an assembly, and perform a function on that part, and finally saving and closing that part then moving on to the next part?

       

      I was thinking I could start a For Loop, and taking the number of runs to be the total amount of components, like this:

       

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swAssembly As SldWorks.AssemblyDoc
      Dim CompNum As String
      
      Sub main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swAssembly = swApp.ActiveDoc
      
          ' Get number of components, not including child components in subassemblies
          CompNum = swAssembly.GetComponentCount(True)
      
           BEGIN LOOP USING CompNum amount of loops
                *****open part from assembly function (if subassembly, create another for-loop with same procedure)
                *****perform function on part
                *****save and close part, back to main assembly
           END LOOP
      
      End main
      

       

      Is there any simple way of doing this? Essentially, I would like to automate something that needs to be applied to each part / component within an assembly or subassembly.

       

      Thanks in advance

       

      Regards,

      Michael

        • Re: Opening Parts in an Assembly using VBA?
          Chiheb Gouta

          Hi Michael

          Here is what you need exactly :

          If there is an error on the code, it may be something written with VB.NET because I work essentially with VB.NET and not in VBA and I tried to convert all functions to VBA.

           

          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 Object
          Dim components1 as Object
          Dim component as Component2
          Dim component1 as 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)
          
              For i As Integer = 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, Errors)
          
          *****perform function on part 
          -----------------------
          
          *****save and close part, back to main assembly 
          swModel.SaveAs(component.GetPathName)
           swApp.CloseDoc component.GetPathName
          Set swModel = swApp.ActiveDoc
          
           Else  If swModel1.GetType = swDocASSEMBLY Then
          
          CompNum1 = swModel1.GetComponentCount(True)  
          Components1 = swModel1.GetComponents(True)
              For j As Integer = 0 To CompNum1
                      component1 = Components1(j)
          swModel1 = component1.GetModelDoc2
                    *****open part from subassembly 
             Set swModel = swApp.ActivateDoc3(component.Name2, False, swRebuildOnActivation_e.swUserDecision, Errors)
          
          -----------------------
            *****perform function on part 
          -----------------------
          
            *****save and close part, back to main assembly 
          swModel.SaveAs(component.GetPathName)
           swApp.CloseDoc component.GetPathName
          
          Next j
          Next i
          End main
          
          • Re: Opening Parts in an Assembly using VBA?
            Tom Smith

            Check out Luke's books at http://angelsix.com/solidworks He shows how to traverse assemblies