3 Replies Latest reply on Oct 7, 2016 7:25 AM by Nilesh Patel

    Macro to save ALL drawing sheets to one dwg file into a specific folder.

    Justin Nelson

      Hi.   I was wondering if anyone had a macro to save ALL drawings sheets to one dwg file into a specific folder using our custom mapping options.   Thanks

        • Re: Macro to save ALL drawing sheets to one dwg file into a specific folder.
          Nilesh Patel

          Hi Justin,

           

          I believe what you are trying to do is to save SolidWorks drawing file ".slddrw" which contains more than 1 drawing sheets into one ".dwg" file into a specific location using custom mapping options. If it is the case try this:

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim dwgName As String

          Dim lErrors As Long

          Dim lWarning As Long

          Dim bSave As Boolean

           

           

          Sub main()

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

            

              If Not swModel Is Nothing Then

                  dwgName = swModel.GetPathName                                                                       'Gets the path of the drawing document.

                

                  If Not dwgName = Empty Then

                      dwgName = Right(dwgName, Len(dwgName) - InStrRev(dwgName, "\"))                                 'Saves the drawing name including extension ".slddrw".

                    

                      If swModel.GetType = swDocumentTypes_e.swDocDRAWING Then

                          dwgName = Left(dwgName, Len(dwgName) - (Len(dwgName) - InStrRev(dwgName, ".") + 1))         'Saves the drawing name without extension ".slddrw".

                      Else

                          'Warning message if the active document is not a Drawing Document.

                          swApp.SendMsgToUser2 "'" + dwgName + "' is not a Drawing Document. Open or Activate Drawing Document.", swMbStop, swMbOk

                          Exit Sub

                      End If

                  Else

                      'Warning message if the document is not saved.

                      swApp.SendMsgToUser2 "Drawing Document must be saved before running the macro.", swMbStop, swMbOk

                      Exit Sub

                  End If

              Else

                  'Warning message if there is no ducoment open.

                  swApp.SendMsgToUser2 "No document is open. Open Drawing Document.", swMbStop, swMbOk

                  Exit Sub

              End If

            

              bSave = swModel.Extension.SaveAs("YOUR LOCATION" + dwgName + ".dwg", swSaveAsVersion_e.swSaveAsCurrentVersion, _

                                              swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, lErrors, lWarning)

              If bSave = False Then

                  'Warning message if failed to save drawing document as DWG file.

                  swApp.SendMsgToUser2 "Failed to save drawing document as DWG.", swMbStop, swMbOk

                  Exit Sub

              End If

            

          End Sub

           

          Just change the words "YOUR LOCATION" in the macro with your the location where you want to save the dwg file. Set up all your DWG options before running the macro e.g. custom mapping, scale output etc. I have tried the macro and works OK. The macro uses Solidworks drawing file name and saves the dwg file with the same name.

           

          Regards,

            • Re: Macro to save ALL drawing sheets to one dwg file into a specific folder.
              Justin Nelson

              Thanks.  It worked great.  Thanks again.   I do have one more question though.   If i want to be able to have it go to certain folder then be able to choose what sub sub-folder location to go to is this possible also.  There is a guy that saves to all kinds of different folders here and its just has to do with what project he works on.

                • Re: Macro to save ALL drawing sheets to one dwg file into a specific folder.
                  Nilesh Patel

                  Hi Justin,

                   

                  Just replace 'bSave = swModel.Extension.SaveAs....' with this 'bSave = swModel.Extension.RunCommand(swCommands_SaveAs, Empty)'.

                   

                  It will open native Solidworks Save As dialog box & you can choose the folder where you want to save the DWG file. But by doing this, you defeat the purpose of the macro. You should just use 'File --> Save As' instead.

                   

                  Is the folder path where you save the DWG document related to Solidworks drawing document path? I mean, you can use the path of the Solidworks drawing document and find the path of the folder to save DWG document, if it is located in the same project folder and if it is in same manner all times.

                   

                  Regards,