7 Replies Latest reply on Dec 22, 2015 12:51 PM by Amen Allah Jlili

    Macro to open all parts (child) from an assembly

    Marc-Xavier Bilodeau

      Hi!

       

      I'm trying to make some kind of  assembly analysis macro that go throught all child of an assembly.

      The fact is that I work mostly with step file, so they are all empty when opened.

       

      I would like the macro to (once at the time):

      1- Open a part

      2- Open a Userform to help the user analyse the part and fill some custom propreties

      3- Save

      4- Close

      5- Open next part (and repeat)

       

      The help I need right now is just how to go throught and open every child from an assembly.

       

      Here's two references I found, but it's either because I can't search properly, either nobody asked it, it's all I got:

      API Opening each part file in an assembly. - DASSAULT: SOLIDWORKS 3D Design - Eng-Tips

      API, Assembly traversal, grab children attribute - DASSAULT: SOLIDWORKS 3D Design - Eng-Tips

       

      If anyone can guide me a bit, that would be greatly appreciated.

       

      Thanks!

        • Re: Macro to open all parts (child) from an assembly
          Marc-Xavier Bilodeau

          I have this working find until now:

           

          -------------------

          Sub ShowAllOpenFiles()

          Dim swDoc As SldWorks.ModelDoc2

          Dim swAllDocs As EnumDocuments2

          Dim FirstDoc As SldWorks.ModelDoc2

          Dim dummy As Boolean

          Dim NumDocsReturned As Long

          Dim DocCount As Long

          Dim i As Long

          Dim sMsg As String

          Dim swApp As SldWorks.SldWorks

          Dim bDocWasVisible As Boolean

          Dim OpenWarnings As Long

          Dim OpenErrors As Long

          Dim DwgPath As String

          Dim myDwgDoc As SldWorks.ModelDoc2

           

           

          Set swApp = Application.SldWorks

          Set swAllDocs = swApp.EnumDocuments2

          Set FirstDoc = swApp.ActiveDoc

           

           

          DocCount = 0

          swAllDocs.Reset

          swAllDocs.Next 1, swDoc, NumDocsReturned

          While NumDocsReturned <> 0

              bDocWasVisible = swDoc.Visible

              swApp.ActivateDoc swDoc.GetPathName

              DwgPath = swDoc.GetPathName

              If (LCase(Right(DwgPath, 3)) <> "drw") And (DwgPath <> "") Then

                  DwgPath = Left(DwgPath, Len(DwgPath) - 3) & "drw"

                  Set myDwgDoc = swApp.OpenDoc6(DwgPath, swDocDRAWING, swOpenDocOptions_Silent, "", OpenErrors, OpenWarnings)

                  If Not myDwgDoc Is Nothing Then

                      swApp.ActivateDoc myDwgDoc.GetPathName

                      Set myDwgDoc = Nothing

                  End If

              End If

              swAllDocs.Next 1, swDoc, NumDocsReturned

              DocCount = DocCount + 1

           

           

          MsgBox ("Show UserForm here")

           

           

          Wend

           

           

          swApp.ActivateDoc FirstDoc.GetPathName

          End Sub

           

          -------------------