9 Replies Latest reply on Jun 15, 2017 5:07 AM by Alexey Kovalov

    SW macro generate excel with drawing referenced models

    Alexey Kovalov
      Hello everyone!

       

      Is it possible to make macro wich generate MS Excel file with names of SW drawings (A) and referenced models (B) from directory (and subdirectories)? SW2010

      Thanks for advance/

      Alexey

        • Re: SW macro generate excel with drawing referenced models
          Viktor Bovzdarenko

          Hi Alexey,

          there are a few ways to make such macro.
          One of the ways is to use:
          IModelDoc2 : GetDependencies2 or swApp:GetDocumentDependencies2
          It will allow to get referenced to drawing models without opening them. Hence only in a second or so you will be able recursively collect all data and output it to Excel.

          Regards

          Viktor

          • Re: SW macro generate excel with drawing referenced models
            Keith Rice

            This would probably be better handled with Document Manager API, if you want it to run as fast as possible.

             

            Keith

            SolidWorks API Training and Services

            • Re: SW macro generate excel with drawing referenced models
              Alexey Kovalov

              Hello!

              Viktor, Matt thanks for your answers.

              Now I can get  dependences for open or not open drawing.

               

              Option Explicit

               

                 

              Sub main()

               

                  ' Name of unopened document

                  Const sDefaultName      As String = "C:\Dwg 1.SLDDRW"

               

                  Dim swApp               As SldWorks.SldWorks

                  Dim swModel             As SldWorks.ModelDoc2

                  Dim sDocName            As String

                  Dim vDepend             As Variant

                  Dim bRet                As Boolean

                  Dim i                   As Long

                     

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                 

                  If Not swModel Is Nothing Then

                      sDocName = swModel.GetPathName

                  Else

                      sDocName = sDefaultName

                  End If

                 

                  vDepend = swApp.GetDocumentDependencies2(sDocName, True, True, False)

                 

                  Debug.Print sDocName

                  If IsEmpty(vDepend) Then

                      Debug.Print "    No dependencies."

                      Exit Sub

                  End If

                 

                 

                  For i = 0 To (UBound(vDepend) - 1) / 2

                      Debug.Print "    " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1)

                  MsgBox "Ìîäåëü" + ": " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1)

                  Next i

              End Sub

              '-----------------------------------------------

              Now I need to get this data for all drawings from one directory and subdirectories and output it to excel file.

              How can I do this?

              Maybe anyone have an example for similar task?

              I have an excel vba macro to make open directory dialog (choose directory) but this code doesnt work in SW

               

              With Application.FileDialog(msoFileDialogFolderPicker)

               

                      .Title = "Âûáåðèòå ïàïêó èëè äèñê"

                      .Show

                      On Error Resume Next

                      Err.Clear

                      V = .SelectedItems(1)

                      If Err.Number <> 0 Then

                          MsgBox "Âû íè÷åãî íå âûáðàëè!"

                          Exit Sub

                      End If

                  End With

                  BrowseFolder = CStr(V)

              • Re: SW macro generate excel with drawing referenced models
                Alexey Kovalov

                Hello!

                I modify macro and now it get drawing dependencies and output it to excel file.

                 

                 

                Option Explicit

                 

                  

                Sub main()

                 

                    ' Name of unopened document

                    Const sDefaultName      As String = "C:\dwg1.SLDDRW"

                 

                    Dim swApp               As SldWorks.SldWorks

                    Dim swModel             As SldWorks.ModelDoc2

                    Dim sDocName            As String

                    Dim vDepend             As Variant

                    Dim bRet                As Boolean

                    Dim i                   As Long

                    Dim sTitle              As String

                    Dim xlApp               As Excel.Application

                    Dim xlBk                As Excel.Workbook

                    Dim xlSht               As Excel.Worksheet

                    Dim curCol              As Long

                    Dim curRow              As Long

                      

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                  

                    If Not swModel Is Nothing Then

                        sDocName = swModel.GetPathName

                    Else

                        sDocName = sDefaultName

                    End If

                  

                    vDepend = swApp.GetDocumentDependencies2(sDocName, False, True, False)

                  

                    Debug.Print sDocName

                    If IsEmpty(vDepend) Then

                        Debug.Print "    No dependencies."

                        Exit Sub

                    End If

                     

                    'Run Excel

                    Set xlApp = New Excel.Application

                    xlApp.Visible = True

                    Set xlBk = xlApp.Workbooks.Add

                    Set xlSht = xlBk.Worksheets(1)

                    xlSht.Cells(1, 1).Value = "Drawing"

                    xlSht.Cells(1, 2).Value = "Referenced part/assembly"

                    curCol = 2

                    curRow = 2

                   

                    For i = 0 To (UBound(vDepend) - 1) / 2

                        'xlSht.Cells(curRow, curCol).Value = vDepend(2 * i)

                        'curCol = curCol + 1

                        xlSht.Cells(curRow, curCol).Value = vDepend(2 * i + 1)

                        'curCol = 2

                        curRow = curRow + 1

                    

                    Next i

                    Columns("A:E").AutoFit

                End Sub

                '---------------------------------------------

                Now I need to make possibility to select folder and get dependencies for all drawings in folder and subfolders and output it to excel.

                Tell me please, how can I do this?

                Regards.

                Alexey