4 Replies Latest reply on Jun 29, 2018 6:54 AM by Damir Galic

    Export all opened Sheetmetal parts to DWG

    X. Men

      I've made a script from the code collected from different sources. It exports a flatten dwg file if there is only one opened file, but when there are 2 files, nothing get exported.

       

      Here is the code

       

      Dim swApp As SldWorks.SldWorks

       

      Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

       

       

      Sub main()

          Set swApp = Application.SldWorks

         

          Dim FileSavedCount As Long

          Dim FailedToSaveCount As Long

         

          Dim swModel As SldWorks.ModelDoc2

          Dim options As Long

          Dim fso As New FileSystemObject

          options = 2053

         

          Set swModel = swApp.GetFirstDocument

          While Not swModel Is Nothing

          'If swModel.Visible = 1 Then

              If swModel.GetType = swDocPART Then

                  If Not swModel.GetBendState = 0 Then 'if part is sheet metal

                       Dim targetFilePath As String

                       Dim modelPath As String

                      

                       modelPath = swModel.GetPathName

                       targetFilePath = fso.BuildPath(fso.GetParentFolderName(modelPath), fso.GetBaseName(modelPath) + ".DWG")

                     

                      

                       swModel.ExportToDWG2 targetFilePath, modelPath, swExportToDWG_ExportSheetMetal, True, 0, False, False, options, Nothing

                       ' Sleep 5000 ' Tried to sleep but doesn't solve the problem

                       If fso.FileExists(targetFilePath) = False Then

                           FailedToSaveCount = FailedToSaveCount + 1

                       Else

                           FileSavedCount = FileSavedCount + 1

                       End If

                  Else

                      MsgBox (swModel.GetTitle + "Part is not sheet metal")

                  End If

              Else

                  MsgBox (swModel.GetTitle + "Document is not a Part.")

              End If

          'End If

         

          Set swModel = swModel.GetNext

         

          Wend

          MsgBox (CStr(FileSavedCount) + " dwg file(s) exported, " + CStr(FailedToSaveCount) + " file(s) failed")

      End Sub

        • Re: Export all opened Sheetmetal parts to DWG
          Fifi Riri

          Hello,

          You almost got it, You just had to activate each model with ActivateDoc2 before exporting

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swPart As SldWorks.PartDoc

          Dim boolstatus As Long

          Dim errors As Long

           

          Sub main()

              Set swApp = Application.SldWorks

              Dim FileSavedCount As Long

              Dim FailedToSaveCount As Long

              Set swModel = swApp.GetFirstDocument

            

              While Not swModel Is Nothing

                  If swModel.GetType = swDocPART Then

                      If Not swModel.GetBendState = 0 Then 'if part is sheet metal

           

                          Set swPart = swApp.ActivateDoc2(swModel.GetTitle, True, errors)

                         

                          Dim targetFilePath As String

                          Dim modelPath As String

                          modelPath = swModel.GetPathName

                          targetFilePath = Left(modelPath, InStr(modelPath, ".") - 1) & ".DWG"

                    

                          boolstatus = swPart.ExportToDWG2(targetFilePath, modelPath, swExportToDWG_ExportSheetMetal, True, 0, False, False, 2053, Null)

                         

                          If boolstatus Then

                              FileSavedCount = FileSavedCount + 1

                          Else

                              FailedToSaveCount = FailedToSaveCount + 1

                          End If

                      Else

                          MsgBox (swModel.GetTitle & "Part is not sheet metal")

                      End If

                  Else

                      MsgBox (swModel.GetTitle & "Document is not a Part.")

                  End If

           

                  Set swModel = swModel.GetNext

              Wend

              MsgBox (FileSavedCount & " dwg file(s) exported, " & FailedToSaveCount & " file(s) failed")

          End Sub