4 Replies Latest reply on Jun 7, 2018 11:02 AM by Marija Ackovska

    part by part selection macro

    Marija Ackovska

      Hello, I have been reading a lot of your post about macro creation and I have been trying to find my solution but I can't seem to manage so I decided to ask the question directly. Nedless to say I'm a newbie in the macro/VBA universe but I have been given a task to create a macro that would contain a loop in which the reaccuring events would be selecting a part from the current assembly, opening another macro and validating it and then moving on to the next part. As far as the opening of the macro and validating it I have found the solution, but I can't seem to succeed to make the code for selecting the parts one by one before validating them. Can someone help me with this issue ?

      P.S. I have explored the SelectByID2 option, but since I need to use this code on assemblies with 100+ parts and subassemblies , this option wouldn't work.

        • Re: part by part selection macro
          Marija Ackovska

          Thanks for the prompt reply.

          I have seen examples simmilar to this, but if I understand correctly (if not fell free to correct me ) this macro would require to have a precise path of a certain assembly, and I need an 'universal' code option that would be executable on whatever assembly we open.

          Thanks once again

            • Re: part by part selection macro
              Ivana Kolin

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swAssy As SldWorks.AssemblyDoc

              Dim swConf As SldWorks.Configuration

              Dim swRootComp As SldWorks.Component2

              Dim bRet As Boolean

              Dim fileName As String

              Dim errors As Long

              Dim warnings As Long

              Set swApp = CreateObject("SldWorks.Application")

              Set swApp = Application.SldWorks

               

              ' Open assembly

              fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\tutorial\smartcomponents\stepped_shaft.sldasm"

              Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

               

              Set swModel = swApp.ActiveDoc

                • Re: part by part selection macro
                  Marija Ackovska

                  This doesnt seem to work too, or I al doing something wrong. Here is the code that I have put together , so far it opens and validates the second macro, it counts the exact number of assemblies and parts but i cant seem to get it to select the parts one by one and the second macro works only if the part is selected; validated and closed and then another part is selected and so on....

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim vModels As Variant

                  Dim count As Long

                  Dim i As Long

                  Dim index As Long

                   

                      Sub main()

                   

                      Set swApp = Application.SldWorks

                  count = swApp.GetDocumentCount

                      MsgBox count

                  Debug.Print "Number of open documents in this SolidWork session: " & count

                  vModels = swApp.GetDocuments

                  For index = LBound(vModels) To UBound(vModels)

                      Set swModel = vModels(index)

                      Debug.Print "Path and name of open document: " & swModel.GetPathName

                      Next index

                     

                   

                  For i = 0 To count

                   

                   

                      Dim swDCAddIn               As Object

                      Dim strDllFileName          As String

                      Dim strExecutablePath       As String

                      Dim strRegFileName          As String

                      Dim lStatus                 As Long

                      Dim retval                  As Long

                      Dim ToolbarId               As Long

                   

                   

                         

                      On Error Resume Next

                     

                      ToolbarId = 59427

                     

                     

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      If swModel Is Nothing Then

                          MsgBox ("Pas de document actif")

                          End

                        Else

                   

                   

                  Dim swAssy As SldWorks.AssemblyDoc

                  Dim swConf As SldWorks.Configuration

                  Dim swRootComp As SldWorks.Component2

                  Dim bRet As Boolean

                  Dim errors As Long

                  Dim warnings As Long

                  Set swApp = CreateObject("SldWorks.Application")

                  Set swModel = swApp.ActiveDoc

                   

                     

                          Dim stPathMacro As String

                  stPathMacro = swApp.GetCurrentMacroPathName

                  strRegFileName = Replace(stPathMacro, ".swp", ".dll")

                  strDllFileName = strRegFileName

                   

                   

                  lStatus = swApp.LoadAddIn(strDllFileName)

                          Set swDCAddIn = swApp.GetAddInObject("{1CE8DFA5-5D69-4CFA-AED0-822A00D2F01E}"   swDCAddIn.OpenAndValidateSmartProperties

                         

                          On Error Resume Next

                         

                       End If

                      

                  Next

                   

                  Set swApp = Application.SldWorks

                  End Sub