11 Replies Latest reply on Jun 29, 2017 2:29 AM by Deepak Gupta

    Macro to save assembly as dxf files of standard views

    Rob Stewart

      I would like to create a macro which saves an active assembly as separate dxf files of each of the standard views, but not current or ISOs. To do this manually, I have to first save the assembly as a part, then save the part as a dxf and check some boxes. The dxf folder is a sub folder of the active file location called "SW DXFs". I have found two macros, both written by Deepak Gupta, but I'm having trouble combining them as I have a very small knowledge of VBA.

       

      Save Assembly as Part:

      3D ContentCentral

       

      Save part to DWG:

      https://forum.solidworks.com/message/436713#436713#436713

       

      Can anyone help me or give me a step in the right direction?

      Thanks in advance,

      Rob

      Deepak Gupta

        • Re: Macro to save assembly as dxf files of standard views
          Deepak Gupta

          Set up a drawing template with no title block but multiple sheet. Insert a pre-defined view on each sheet like front, top, etc.

           

          Now create drawing using that template and then save as separate DXF sheets.

            • Re: Macro to save assembly as dxf files of standard views
              Kevin Zhu

              Hi Deepak,

               

              I have one of your earlier macro that saves all drawing sheet to separate dxf files to the file name format of "filename_revision.dxf". I'd like to save them using a custom property - "model number", the file name will be in the format of "model_number_revision.dxf". I had a few attempts to modify your code but with no luck. here's the your coding below. sorry I did bit of Google search but couldn't find your original post.

               

               

              ===================================

              Option Explicit

               

               

              Dim swApp              As SldWorks.SldWorks

              Dim swModel            As SldWorks.ModelDoc2

              Dim swDrawModel        As SldWorks.ModelDoc2

              Dim swDraw              As SldWorks.DrawingDoc

              Dim swView              As SldWorks.View

              Dim swCustPrpMgr        As SldWorks.CustomPropertyManager

              Dim sFilePath          As String

              Dim sFilename          As String

              Dim vSheetName          As Variant

              Dim nErrors            As Long

              Dim nWarnings          As Long

              Dim i                  As Long

              Dim bRet                As Boolean

              Dim Value              As String

               

               

               

               

              Sub main()

               

               

              On Error GoTo error

               

               

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

               

               

              If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then

               

               

              swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")

               

               

              ' If no model currently loaded, then exit

              Exit Sub

               

               

              End If

               

               

              Set swDraw = swModel

               

               

              'Set the DXF export option to export active sheet only

              swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfMultisheet_e.swDxfActiveSheetOnly

               

               

              sFilePath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".") - 1)

               

               

              Value = swModel.CustomInfo("REVISION")

               

               

              vSheetName = swDraw.GetSheetNames

               

               

                  For i = 0 To UBound(vSheetName)

               

               

                      bRet = swDraw.ActivateSheet(vSheetName(i))

                

                  sFilename = sFilePath & "_" & i + 1 & "_" & "REV" & Value & ".dxf"

               

               

                      bRet = swModel.SaveAs4(sFilename, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)

               

               

                  Next i

               

               

                  ' Switch back to first sheet

              bRet = swDraw.ActivateSheet(vSheetName(0))

               

               

              Exit Sub

               

               

              error:

              MsgBox "An error has occured - Please close and reopen SolidWorks to try again", vbCritical, "Program Error"

              Exit Sub

               

               

              End Sub