6 Replies Latest reply on Mar 17, 2017 10:08 AM by Ray Calhoon

    need help with a macro

    Ray Calhoon

      I want to create a macro to save the flat pattern as a .dxf in a specified location (user entry) with the name of the part file that is open.  Here is what I've recorded, but it's not working, and I know basically nothing of VB nor API.  Any help would be greatly appreciated.

       

      ' ******************************************************************************

      ' C:\Users\raycal\AppData\Local\Temp\swx8544\Macro1.swb - macro recorded on 03/16/17 by raycal

      ' ******************************************************************************

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Flat-Pattern", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

      longstatus = Part.Save2(False)

      Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      myModelView.FrameLeft = 0

      myModelView.FrameTop = 33

      Set myModelView = Part.ActiveView

      myModelView.FrameState = swWindowState_e.swWindowMaximized

      Set Part = swApp.NewDocument("S:\Custom Setup Files\True Templates\SHEET METAL DRW DEC-16.drwdot", 0, 0, 0)

      swApp.ActivateDoc2 "Draw1 - Sheet1", False, longstatus

      Set Part = swApp.ActiveDoc

      Set Part = swApp.ActiveDoc

      Set myModelView = Part.ActiveView

      myModelView.FrameLeft = 0

      myModelView.FrameTop = 0

      Set myModelView = Part.ActiveView

      myModelView.FrameState = swWindowState_e.swWindowMaximized

      swApp.ActivateDoc2 "Part1", False, longstatus

      Set Part = swApp.ActiveDoc

      Part.EditUnsuppress2

      boolstatus = Part.Extension.SelectByID2("Flat-Pattern", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Part.BlankSketch

      Part.BlankSketch

      Part.BlankSketch

      Part.BlankSketch

      Part.BlankSketch

      Part.BlankSketch

      Dim myView As Object

      Set myView = Part.CreateDrawViewFromModelView3("Part1", "DxfOutputModelView", 0, 0, 0)

      boolstatus = Part.DeleteNamedView(DxfOutputModelView)

      Part.BlankSketch

      Part.BlankSketch

      Part.UnblankSketch

      Part.UnblankSketch

      Part.EditSuppress2

      boolstatus = Part.Extension.SelectByID2("Flat-Pattern", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Part.ClearSelection2 True

      End Sub

        • Re: need help with a macro
          Deepak Gupta

          Ray, give a try to #TASK tool and you would be happily using this utility for lot of similar stuff. Most of the inbuilt tools are free to use.

           

            • Re: need help with a macro
              Ray Calhoon

              Deepak,

               

              I already use #TASK.  Yes, very versatile tool.

               

              However, in this case, I don't need to export mass quantities of files, but rather, one at a time, thus the need for a macro button inside of solidworks.  When we create a new sheet metal part, we save the .dxf to a particular file location, with a particular naming convention and have two options (forming tools & library features) checked in the entities to export list (under DXF/DWG Output property manager).  I just need this functionality to eliminate the repetitive nature of this task.  By using #TASK, we would only be adding more process steps (not taking them away).

               

              So, here is what I need: 

               

              Invoke export to DXF/DWG for flat pattern

               

              locate in specific file location (same every time) 

               

              change file name to that of the part file, excluding the "Flat-Pattern - " prefix 

               

              add the two checks next to Library features & Forming tools

              keep the review

              save

                • Re: need help with a macro
                  Deepak Gupta

                  Ray, try these codes. With API, I do not think you can show  the review window

                   

                  Option Explicit 
                  Dim swApp As SldWorks.SldWorks
                  Dim swModel As SldWorks.ModelDoc2
                  Dim swPart As SldWorks.PartDoc
                  Dim sModelName As String
                  Dim Path As String
                  Dim sPathName As String
                  Dim varAlignment As Variant
                  Dim dataAlignment(11) As Double
                  Dim options As Long
                  
                  Sub main()
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc  
                      Path = "D:\Test Export\DXF\" ' Change the path here
                    
                      sModelName = swModel.GetPathName
                      sPathName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
                      sPathName = Left(sPathName, InStrRev(sPathName, ".") - 1)
                      sPathName = Path + sPathName + ".DXF"
                      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   
                      options = 97 ' This value determines what would be exported in the DXF/DWG file.   
                      swPart.ExportToDWG2 sPathName, sModelName, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, options, Null
                  End Sub