3 Replies Latest reply on Mar 6, 2015 3:11 PM by Solidworks Selim

    Export DWG macro rotates view and adds un-needed text to filename

    Paul Flores

      The macro below saves the current view of a part as a DWG file, and it also adds " (Current)" to the end of the file name.  For example,  "12345.sldprt" is saved as "12345 (Current).dwg".  I would like this macro to only save it as filename.dwg without adding the word current to it.

       

      The other issue is that if a part is parallel to the X-Y plane, the exported DWG view matches the current view and is not rotated.  However, if the part is parallel to the Z-Y or X-Z plane then the exported DWG file is rotated 90 degrees.  Is it possible to save the view as a DWG without having it rotated, regardless of what plane the part view is?

       

      Not sure if this matters, but these parts were not created with the sheet metal feature tool, the sketches were simply extruded. 

       

      'Export Part View as DWG.swp -------------10/18/13

      'Description: Macro to export part view as DWG.

      'Precondition: Any active part file with some feature(s)

      ' Postconditions: DWG file of the current view orientation will be saved in the same location with same file name as the part.

      '                 The file name may contains "(Current)"

      ' Please back up your data before use and USE AT OWN RISK

       

      ' This macro is provided as is.  No claims, support, refund, safety net, or

      ' warranties are expressed or implied.  By using this macro and/or its code in

      ' any way whatsoever, the user and any entities which the user represents,

      ' agree to hold the authors free of any and all liability.  Free distribution

      ' and use of this code in other free works is welcome.  If any portion of

      ' this code is used in other works, credit to the authors must be placed in

      ' that work within a user viewable location (e.g., macro header).  All other

      ' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited

      ' without the expressed written consent by the authors.  Use at your own risk!

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

      ' Written by: Deepak Gupta (http://gupta9665.com/)

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

      Option Explicit

       

      Dim swApp                   As SldWorks.SldWorks

      Dim swModel                 As SldWorks.ModelDoc2

      Dim swPart                  As SldWorks.PartDoc

      Dim sModelName              As String

      Dim sPathName               As String

      Dim varAlignment            As Variant

      Dim dataAlignment(11)       As Double

      Dim varViews                As Variant

      Dim dataViews(0)            As String

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swPart = swModel

       

      dataAlignment(0) = 0#

      dataAlignment(1) = 0#

      dataAlignment(2) = 0#

      dataAlignment(3) = 1#

      dataAlignment(4) = 0#

      dataAlignment(5) = 0#

      dataAlignment(6) = 0#

      dataAlignment(7) = 1#

      dataAlignment(8) = 0#

      dataAlignment(9) = 0#

      dataAlignment(10) = 0#

      dataAlignment(11) = 1#

       

      varAlignment = dataAlignment

       

      dataViews(0) = "*Current"

      varViews = dataViews

       

      sPathName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 6)

       

      swPart.ExportToDWG sPathName & "dwg", swModel.GetPathName, 3, False, varAlignment, False, False, 0, varViews

       

      End Sub

        • Re: Export DWG macro rotates view and adds un-needed text to filename
          Deepak Gupta

          Here are the changes you need to make

           

          Set data alignment values to 0 like shown below:

              dataAlignment(0) = 0#

              dataAlignment(1) = 0#

              dataAlignment(2) = 0#

              dataAlignment(3) = 0#

              dataAlignment(4) = 0#

              dataAlignment(5) = 0#

              dataAlignment(6) = 0#

              dataAlignment(7) = 0#

              dataAlignment(8) = 0#

              dataAlignment(9) = 0#

              dataAlignment(10) = 0#

              dataAlignment(11) = 0#

           

          And set data view to nothing ("") like shown below:

          dataViews(0) = ""

            • Re: Export DWG macro rotates view and adds un-needed text to filename
              Paul Flores

              Thanks Deepak, I've updated the macro as noted below.

               

              'Export Part View as DWG.swp -------------10/18/13

               

              'Updated ---------------------------------3/6/15

              '   Removed " (Current)" from saved filename

              '   Saved DWG view matches Solidworks view; it's not rotated

              '   If macro is used on assembly, warns user to only use on parts

              '   Messages user that DWG was created or updated

               

              'Description: Macro to export part view as DWG.

              'Precondition: Any active part file with some feature(s)

              ' Postconditions: DWG file of the current view orientation will be saved in the same location with same file name as the part.

              '                 The file name may contains "(Current)"

              ' Please back up your data before use and USE AT OWN RISK

               

              ' This macro is provided as is.  No claims, support, refund, safety net, or

              ' warranties are expressed or implied.  By using this macro and/or its code in

              ' any way whatsoever, the user and any entities which the user represents,

              ' agree to hold the authors free of any and all liability.  Free distribution

              ' and use of this code in other free works is welcome.  If any portion of

              ' this code is used in other works, credit to the authors must be placed in

              ' that work within a user viewable location (e.g., macro header).  All other

              ' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited

              ' without the expressed written consent by the authors.  Use at your own risk!

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

              ' Written by: Deepak Gupta (http://gupta9665.com/)

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

              Option Explicit

               

              Dim swApp                   As SldWorks.SldWorks

              Dim swModel                 As SldWorks.ModelDoc2

              Dim swPart                  As SldWorks.PartDoc

              Dim sModelName              As String

              Dim sPathName               As String

              Dim varAlignment            As Variant

              Dim dataAlignment(11)       As Double

              Dim varViews                As Variant

              Dim dataViews(0)            As String

               

              Sub main()

               

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

               

                  ' Check the document is a Part

                  If (swModel.GetType <> swDocPART) Then

                  MsgBox "Please open a single Part file"

                  Exit Sub

                  End If

                 

              Set swPart = swModel

                 

              dataAlignment(0) = 0#

              dataAlignment(1) = 0#

              dataAlignment(2) = 0#

              dataAlignment(3) = 0#

              dataAlignment(4) = 0#

              dataAlignment(5) = 0#

              dataAlignment(6) = 0#

              dataAlignment(7) = 0#

              dataAlignment(8) = 0#

              dataAlignment(9) = 0#

              dataAlignment(10) = 0#

              dataAlignment(11) = 0#

               

              varAlignment = dataAlignment

               

              dataViews(0) = ""

              varViews = dataViews

               

              swModel.EditRebuild3

               

              sPathName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 6)

               

              swPart.ExportToDWG sPathName & "dwg", swModel.GetPathName, 3, False, varAlignment, False, False, 0, varViews

               

              swApp.SendMsgToUser2 "A DWG of the current view has been created or updated.", swMbInformation, swMbOk

              End Sub