4 Replies Latest reply on Oct 1, 2013 8:08 AM by Emil Svallingson

    How to insert macro into EPDM task?

    Emil Svallingson

      Hi,

       

      I am using a macro inside of Solidworks which works perfectly and now I am trying to insert this macro into a EDPM task with fatal results. How do I do this?

       

      This is the macro I am using:

       

       

       

      ' Save Assy As Part (Exterior Surfaces).swp ---------------------------06/16/12

      '

      'Description: Macro to save assembly to save as part (Exterior Surfaces).

      '             Then saved part file is further saved as "STEP" file in the same location.

      '

      'Precondition: Any active assembly with minimum one part.

      '

      'Postcondition: Assembly is saved as part and part is saved as "STEP" file.

      '

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

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

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

       

       

      Dim swApp               As SldWorks.SldWorks

      Dim swModel             As SldWorks.ModelDoc2

      Dim swModelDocExt       As SldWorks.ModelDocExtension

      Dim FilePath            As String

      Dim sFilePath           As String

      Dim PathSize            As Long

      Dim PathNoExtension     As String

      Dim NewFilePath         As String

      Dim nErrors             As Long

      Dim nWarnings           As Long

       

       

      Sub main()

       

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swModelDocExt = swModel.Extension

       

       

      FilePath = swModel.GetPathName

      PathSize = Strings.Len(FilePath)

      PathNoExtension = Strings.Left(FilePath, PathSize - 6)

      NewFilePath = PathNoExtension & "SLDPRT"

       

       

      swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, swSaveAsmAsPart_ExteriorFaces

      swModelDocExt.SaveAs NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings

       

       

      swApp.CloseDoc swModel.GetTitle

       

       

      Set swModel = swApp.OpenDoc(NewFilePath, swDocPART)

      Set swModel = swApp.ActiveDoc

       

       

      sFilePath = PathNoExtension & "STEP"

       

      swModel.SaveAs (sFilePath)

       

       

      swApp.CloseDoc swModel.GetTitle

       

       

      End Sub

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

        • Re: How to insert macro into EPDM task?
          Jeremiah Davis

          There are a couple of things that need to change in the code to make it work.

           

          First, replace this line:

           

          Set swModel = swApp.ActiveDoc

           

          With this line:

           

          Set SwModel = swApp.OpenDoc6("<FilePath>", SwConst.swDocASSEMBLY, 3, "", Errors, Warnings)

           

          This assumes you are working with an assembly file, but it could be modified to work here.

           

          Next, remove the following line:

           

          swApp.CloseDoc swModel.GetTitle

           

          EPDM will take care of closing the document and calling it in the code will cause problems.

           

          But if you are trying to save the document as a STEP file, why not use the built in convert function?

            • Re: How to insert macro into EPDM task?
              Emil Svallingson

              Hi Jeremiah,

               

              Thank you for trying to help me. Made the changes but still it doesn't work. It starts off doing something but never finishes and no error messages either?

               

              I want to be able to save an assembly as a part with surfaces only in step-format, I don't think I can do this with the built in convert function.

                • Re: How to insert macro into EPDM task?
                  Jeremiah Davis

                  Try this:

                   

                  Dim swApp           As SldWorks.SldWorks
                  Dim swModel         As SldWorks.ModelDoc2
                  Dim swModelDocExt   As SldWorks.ModelDocExtension
                  Dim FilePath        As String
                  Dim sFilePath       As String
                  Dim PathSize        As Long
                  Dim PathNoExtension As String
                  Dim NewFilePath     As String
                  Dim nErrors         As Long
                  Dim nWarnings       As Long

                   

                  Sub main()

                   

                  Set swApp = Application.SldWorks

                  'Set swModel = swApp.ActiveDoc

                  Set swModel = swApp.OpenDoc6("<Filepath>", SwConst.swDocASSEMBLY, 3, "", nErrors, nWarnings)

                   

                  Set swModelDocExt = swModel.Extension

                   

                  FilePath = swModel.GetPathName

                  PathSize = Strings.Len(FilePath)

                  PathNoExtension = Strings.Left(FilePath, PathSize - 6)

                  NewFilePath = PathNoExtension & "SLDPRT"

                   

                  swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, swSaveAsmAsPart_ExteriorFaces

                  swModelDocExt.SaveAs NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings

                  'swApp.CloseDoc swModel.GetTitle

                   

                  Set swModel = swApp.OpenDoc6(NewFilePath, SwConst.swDocPART, 3, "", nErrors, nWarnings)

                  Set swModel = swApp.ActiveDoc

                   

                  sFilePath = PathNoExtension & "STEP"

                  swModel.SaveAs (sFilePath)

                  swApp.CloseDoc swModel.GetTitle

                   

                  End Sub