AnsweredAssumed Answered

Export all opened Sheetmetal parts to DWG

Question asked by X. Men on Jun 26, 2018
Latest reply on Jun 29, 2018 by Damir Galic

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


                     FileSavedCount = FileSavedCount + 1

                 End If


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

            End If


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

        End If

    'End If


    Set swModel = swModel.GetNext



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

End Sub