3 Replies Latest reply on May 6, 2016 10:09 AM by Joe Pickens

    Loop macro for creating virtual ASM Structure

    Joe Pickens

      I am working on a macro the uses an indented BOM to create an assembly and creates virtual sub assembles that don't yet exist.

      the macro creats the top assembly and level two virtual subassembles but i am having issues figuring out how to create a functioning loop that will make further subassemles.

       

      this is the part of the code that seems to be failing when it loops to create a subassembly in my subassembly i get an error

       

      Run Time Error '-2147417851(80010105)'

      Automation Error

      The Server Thew an Exception

       

       

      status = SWAsm.InsertNewVirtualAssembly(swcomp)

       

      My macro runs from Module1 then when a component isnt found then runs module2 which loops on itself as further components are not found.

       

      I am new to writing macros so i am assuming i am probably doing something wrong in trying to loop.

        • Re: Loop macro for creating virtual ASM Structure
          John Alexander

          If they aren't too large, can you post everything in module1 and module2?

            • Re: Loop macro for creating virtual ASM Structure
              Joe Pickens

              the are vary big but there is the important parts

              Here is the top portion leading to the sub assembly

              Moduel1

              'Open new session of SolidWorks

                  Set swApp = CreateObject("SldWorks.Application")

               

              'Define assembly template location

                  TLAsmTemp = "C:\_MO_Vault\Admin\Templates\Title Blocks\MO_TOP_LEVEL_ASM.asmdot"

               

              'Open new Solidworks Assembly with specified Template

                  Set SWModel = swApp.NewDocument(TLAsmTemp, 0, 0, 0)

               

              'Show Solidworks assembly

                  SWModel.Visible = True

               

              'Set up events

                  Set SWAsm = SWModel

                 

              'Get the title of the assembly document

                  SWAsmTitle = SWModel.GetTitle

                         

              'Split the title into two strings using the period(.) as the delimiter

                  Str = Split(SWAsmTitle, ".")

               

              'Set assemby name for use in mating in module5

                  SWAsmName = Str(0)

               

              'Login to EPDM Vault

                  VaultApp.LoginAuto "_MO_Vault", 0

                 

              'Do a loop search till there is a blank value in column A

                  While Sheets(1).Range("A6").Offset(i, 0).Value <> ""

               

                  'Use Values that only have the number 1 in column E of the JDE BOM Sheet

                      If Sheets(1).Range("E6").Offset(i, 0).Value = 1 Then

               

                      'If the statement is true use the value in column "A" as the name of the file

                        'and use the value found in column "C" for the operation sequence

                          compname = Sheets(1).Range("A6").Offset(i, 0).Value

                          OpSeq = Sheets(1).Range("C6").Offset(i, 0).Value

               

                      'Create a search in the EPDM Vault and set its parameters

                          Set EPDMSearch = VaultApp.CreateSearch()

                          EPDMSearch.FindFiles = True

                          EPDMSearch.FindFolders = False

                          EPDMSearch.FindHistoricStates = False

                          EPDMSearch.Recursive = True

                          EPDMSearch.FileName = "%" & compname & "%"

               

                      'Get the first file in the EPDM Vault

                          Set EPDMSearchR = EPDMSearch.GetFirstResult

               

                          If EPDMSearchR Is Nothing Then

               

                              Call Module2.Open_New_Asm_and_Srt_Search(SWModel, SWAsm, compname, 1, i)

               

                        Else

               

              "it builds the assembly with found components"

               

              Moduel 2

               

              'Open new session of SolidWorks

                  Set swApp = CreateObject("SldWorks.Application")

               

                  Set swcomp = Nothing

                  status = SWAsm.InsertNewVirtualAssembly(swcomp)   

                  Set SWAddComp = swcomp.GetModelDoc2

                  Set SWAsmComp = SWAddComp

                 

              'Get the title of the assembly document

                  SWAsmTitle = SWModel.GetTitle

                         

              'Split the title into two strings using the period(.) as the delimiter

                  Str = Split(SWAsmTitle, ".")

               

              'Set assemby name for use in mating in module5

                  SWAsmName = Str(0)

               

                  swcomp.Name2 = compname

               

              'Login to EPDM Vault

                  VaultApp.LoginAuto "_MO_Vault", 0

                 

              If Not Sheets(1).Range("E6").Offset(i, 0).Value = level Then

              'Do a loop search till there is a blank value in column A

                  While Sheets(1).Range("E6").Offset(i, 0).Value <> level And Sheets(1).Range("A6").Offset(i, 0).Value <> ""

                     

                      'If the statement is true use the value in column "A" as the name of the file

                        'and use the value found in column "C" for the operation sequence

                          compname = Sheets(1).Range("A6").Offset(i, 0).Value

                          OpSeq = Sheets(1).Range("C6").Offset(i, 0).Value

               

                      'Create a search in the EPDM Vault and set its parameters

                          Set EPDMSearch = VaultApp.CreateSearch()

                          EPDMSearch.FindFiles = True

                          EPDMSearch.FindFolders = False

                          EPDMSearch.FindHistoricStates = False

                          EPDMSearch.Recursive = True

                          EPDMSearch.FileName = "%" & compname & "%"

                                 

                      'Get the first file in the EPDM Vault

                          Set EPDMSearchR = EPDMSearch.GetFirstResult

                                     

                                      If EPDMSearchR Is Nothing Then

               

                              'Call Module1.Open_New_Asm_and_Srt_Search(SWAddComp, SWAsmComp, compname, level + 1, i)

                          else

              "it builds the assembly with found components"

               

               

              as you can see they are very similar and they run fine unless module2 gos into a second loop then i get the error when trying to make the new subassembly inside the subassembly