10 Replies Latest reply on Nov 14, 2018 3:29 AM by Dirk van de Ven

    Macro for .STEP from drwaing

    Dirk van de Ven

      Hi all,

       

      I am looking for some kind of macro.

       

      For example:

       

      I have a part named 10.12.16.SLDPRT and it has 3 configurations.

      The corresponding drawings are called: 10.12.16A.SLDDRW, 10.12.16B.SLDDRW and 10.12.16C.SLDDRW (3 configurations).

      I would really like to have a macro that when i have the drawing of one of the configurations opened (For example drawing 10.12.16C.SLDDRW is open) and i click the macro button, it saves the configurated part in the drawing as 10.12.16C.STEP. If i open drawing 10.12.16B.SLDDRW and click the button it should save the part in the right configuration as 10.12.16B.STEP in the folder where the drawing is at.

       

      Can somebody help me to make a macro like this or is it even possible?

      I have no experience in API and Macro's what so ever so i will probably need alot of help.

       

      Thanks alot in advance!

        • Re: Macro for .STEP from drwaing
          Cad Admin

          Did you search the Forum?

          Did you Google Search?

           

          Its been done many time, various ways...

          Take a look at this one...

          https://forum.solidworks.com/message/566804?&_ga=2.63216231.953582726.1542121782-73e0d440-31c6-11e8-9c04-1f97ababaa3e#co…

            • Re: Macro for .STEP from drwaing
              Dirk van de Ven

              I searched a lot bot none of them use the Drawing name as save-name for the step.

               

              And that is my whole problem. The step that is saved should have the same name as the drawing it belongs to. So if i save the drawing as 10.12.16A, and the part is named 10.12.16, the configuration is named something random, the .STEP file should still come out as 10.12.16A.STEP

              and all that straight from a button when the drawing is opened on my screen.

               

              don't know if that is possible?

                • Re: Macro for .STEP from drwaing
                  Cad Admin

                  try this:  you will need to change the path "C:\Users\XXXXX\Desktop\" to whatever you need... this is one of Deepak Gupta macros i believe, i cut and past from another forum string.

                   

                  Sub main()

                   

                  Dim swApp               As SldWorks.SldWorks

                  Dim swModel             As SldWorks.ModelDoc2

                  Dim swDrawModel         As SldWorks.ModelDoc2

                  Dim swModelPDF          As SldWorks.ModelDoc2

                  Dim swDraw              As SldWorks.DrawingDoc

                  Dim swModelDocExt       As SldWorks.ModelDocExtension

                  Dim swExportPDFData     As SldWorks.ExportPdfData

                  Dim swView              As SldWorks.View

                  Dim nErrors             As Long

                  Dim nWarnings           As Long

                  Dim dFileName           As String

                  Dim pFileName           As String

                  Const Path              As String = "C:\Users\XXXXX\Desktop\" 'Change Path here

                  Dim boolstatus          As Boolean

                   

                   

                  Set swApp = Application.SldWorks

                  Set swDrawModel = swApp.ActiveDoc

                   

                  '-----------Check to see if a drawing is loaded-----------

                  If swDrawModel Is Nothing Then

                          MsgBox "There is no active drawing document"

                          Exit Sub

                  End If

                     

                  If swDrawModel.GetType <> swDocDRAWING Then

                          MsgBox "Open a drawing first and then TRY again!"

                          Exit Sub

                  End If

                   

                  If swDrawModel.GetPathName = "" Then

                          MsgBox "Plese Save the Drawing and then TRY again!"

                          swDrawModel.Save

                          Exit Sub

                  End If

                   

                  Set swDraw = swDrawModel

                  Set swView = swDraw.GetFirstView

                  Set swView = swView.GetNextView

                   

                  '-----------Determine if there is any model view-----------

                  If swView Is Nothing Then

                          MsgBox "No View(s) found, Insert a View first and then TRY again!"

                          Exit Sub

                  End If

                   

                  '-----------Determine if there is any model view-----------

                  If swView.GetReferencedModelName = "" Then

                          MsgBox "No Model View(s) found, Insert a View first and then TRY again!"

                          Exit Sub

                  End If

                   

                  Set swModel = swView.ReferencedDocument

                   

                  '-----------Drawing File Name Without Extension-----------

                  dFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)

                  dFileName = Left(dFileName, InStrRev(dFileName, ".") - 1)

                  dFileName = UCase(Path + dFileName)

                   

                  '-----------Model File Name Without Extension-----------

                  pFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

                  pFileName = Left(pFileName, InStrRev(pFileName, ".") - 1)

                  pFileName = UCase(Path + pFileName)

                   

                  '-----------Save as STEP-----------

                  Set swModel = swApp.ActivateDoc3(swModel.GetPathName, False, swRebuildActiveDoc, nErrors)

                   

                  bRebuild = swModel.ForceRebuild3(False)

                  Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Specify Type of STEP (214 or 203)

                  swModel.SaveAs (dFileName & ".STEP")

                     

                  swApp.CloseDoc swModel.GetTitle

                   

                  End Sub

                    • Re: Macro for .STEP from drwaing
                      Dirk van de Ven

                      Awesome! The step making part works perfect!

                       

                      Only one question remains. Is it possible to let it automatically take the path of the drawing and make that the place where it puts the STEP? So that i dont have to change the path manually every time.

                        • Re: Macro for .STEP from drwaing
                          Cad Admin

                          'Option Explicit

                           

                          Sub main()

                           

                          Dim swApp              As SldWorks.SldWorks

                          Dim swModel            As SldWorks.ModelDoc2

                          Dim swDrawModel        As SldWorks.ModelDoc2

                          Dim swModelPDF          As SldWorks.ModelDoc2

                          Dim swDraw              As SldWorks.DrawingDoc

                          Dim swModelDocExt      As SldWorks.ModelDocExtension

                          Dim swExportPDFData    As SldWorks.ExportPdfData

                          Dim swView              As SldWorks.View

                          Dim nErrors            As Long

                          Dim nWarnings          As Long

                          Dim dFileName          As String

                          Dim pFileName          As String

                          Dim boolstatus          As Boolean

                           

                           

                          Set swApp = Application.SldWorks

                          Set swDrawModel = swApp.ActiveDoc

                           

                          '-----------Check to see if a drawing is loaded-----------

                          If swDrawModel Is Nothing Then

                                  MsgBox "There is no active drawing document"

                                  Exit Sub

                          End If

                            

                          If swDrawModel.GetType <> swDocDRAWING Then

                                  MsgBox "Open a drawing first and then TRY again!"

                                  Exit Sub

                          End If

                           

                          If swDrawModel.GetPathName = "" Then

                                  MsgBox "Plese Save the Drawing and then TRY again!"

                                  swDrawModel.Save

                                  Exit Sub

                          End If

                           

                          Set swDraw = swDrawModel

                          Set swView = swDraw.GetFirstView

                          Set swView = swView.GetNextView

                           

                          '-----------Determine if there is any model view-----------

                          If swView Is Nothing Then

                                  MsgBox "No View(s) found, Insert a View first and then TRY again!"

                                  Exit Sub

                          End If

                           

                          '-----------Determine if there is any model view-----------

                          If swView.GetReferencedModelName = "" Then

                                  MsgBox "No Model View(s) found, Insert a View first and then TRY again!"

                                  Exit Sub

                          End If

                           

                          Set swModel = swView.ReferencedDocument

                           

                          '-----------Drawing File Name Without Extension-----------

                          dFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1) 'filename w/ext

                          dFileName = Left(dFileName, InStrRev(dFileName, ".") - 1) 'filename wo/ext

                          dFileName = UCase(CurDir$ & "\" & dFileName)

                           

                          '-----------Model File Name Without Extension-----------

                          pFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

                          pFileName = Left(pFileName, InStrRev(pFileName, ".") - 1)

                          pFileName = UCase(Path + pFileName)

                           

                          '-----------Save as STEP-----------

                          Set swModel = swApp.ActivateDoc3(swModel.GetPathName, False, swRebuildActiveDoc, nErrors)

                           

                          bRebuild = swModel.ForceRebuild3(False)

                          Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Specify Type of STEP (214 or 203)

                          swModel.SaveAs (dFileName & ".STEP")

                            

                          swApp.CloseDoc swModel.GetTitle

                           

                          End Sub

                        • Re: Macro for .STEP from drwaing
                          Deepak Gupta

                          The only change you would have to make is to set the macro to activate the configuration being used by the drawing.

                    • Re: Macro for .STEP from drwaing
                      Dirk van de Ven

                      Thanks alot guys! I found one problem.... The save-to folder isn't always the right one. If i open a drawing and save it to step, it sometime saves it in a folder of a drawing i opened and closed earlier.

                       

                      Example:

                      I open drawing 1.2.A in folder A.

                      Save the configured parts to step.

                      The save file is now: 1.2.A.step in folder A

                      This is correct.

                       

                      I close every drawing and open drawing 2.4.A in folder B

                      Save the configured part to step.

                      The saved file is now: 2.4.A.step in folder A

                      This isn't right.

                       

                      I close every drawing and open drawing 6.3.A in folder C

                      Save the configured part to step.

                      The saved file is now: 6.3.A.step in folder A or folder B (don't see any obvious pattern in this)

                       

                      The macro is now like this:

                       

                      'Option Explicit

                       

                       

                       

                      Sub main()

                       

                       

                       

                      Dim swApp              As SldWorks.SldWorks

                       

                      Dim swModel            As SldWorks.ModelDoc2

                       

                      Dim swDrawModel        As SldWorks.ModelDoc2

                       

                      Dim swModelPDF          As SldWorks.ModelDoc2

                       

                      Dim swDraw              As SldWorks.DrawingDoc

                       

                      Dim swModelDocExt      As SldWorks.ModelDocExtension

                       

                      Dim swExportPDFData    As SldWorks.ExportPdfData

                       

                      Dim swView              As SldWorks.View

                       

                      Dim nErrors            As Long

                       

                      Dim nWarnings          As Long

                       

                      Dim dFileName          As String

                       

                      Dim pFileName          As String

                       

                      Dim boolstatus          As Boolean

                       

                       

                       

                       

                       

                      Set swApp = Application.SldWorks

                       

                      Set swDrawModel = swApp.ActiveDoc

                       

                       

                       

                      '-----------Check to see if a drawing is loaded-----------

                       

                      If swDrawModel Is Nothing Then

                       

                              MsgBox "There is no active drawing document"

                       

                              Exit Sub

                       

                      End If

                       

                       

                       

                      If swDrawModel.GetType <> swDocDRAWING Then

                       

                              MsgBox "Open a drawing first and then TRY again!"

                       

                              Exit Sub

                       

                      End If

                       

                       

                       

                      If swDrawModel.GetPathName = "" Then

                       

                              MsgBox "Plese Save the Drawing and then TRY again!"

                       

                              swDrawModel.Save

                       

                              Exit Sub

                       

                      End If

                       

                       

                       

                      Set swDraw = swDrawModel

                       

                      Set swView = swDraw.GetFirstView

                       

                      Set swView = swView.GetNextView

                       

                       

                       

                      '-----------Determine if there is any model view-----------

                       

                      If swView Is Nothing Then

                       

                              MsgBox "No View(s) found, Insert a View first and then TRY again!"

                       

                              Exit Sub

                       

                      End If

                       

                       

                       

                      '-----------Determine if there is any model view-----------

                       

                      If swView.GetReferencedModelName = "" Then

                       

                              MsgBox "No Model View(s) found, Insert a View first and then TRY again!"

                       

                              Exit Sub

                       

                      End If

                       

                       

                       

                      Set swModel = swView.ReferencedDocument

                       

                       

                       

                      '-----------Drawing File Name Without Extension-----------

                       

                      dFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1) 'filename w/ext

                       

                      dFileName = Left(dFileName, InStrRev(dFileName, ".") - 1) 'filename wo/ext

                       

                      dFileName = UCase(CurDir$ & "\" & dFileName)

                       

                       

                       

                      '-----------Model File Name Without Extension-----------

                       

                      pFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

                       

                      pFileName = Left(pFileName, InStrRev(pFileName, ".") - 1)

                       

                      pFileName = UCase(Path + pFileName)

                       

                       

                       

                      '-----------Save as STEP-----------

                       

                      Set swModel = swApp.ActivateDoc3(swModel.GetPathName, False, swRebuildActiveDoc, nErrors)

                       

                      swModel.ShowConfiguration2 swView.ReferencedConfiguration

                       

                       

                      bRebuild = swModel.ForceRebuild3(False)

                       

                      Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Specify Type of STEP (214 or 203)

                       

                      swModel.SaveAs (dFileName & ".STEP")

                       

                       

                       

                      swApp.CloseDoc swModel.GetTitle

                       

                       

                       

                      End Sub

                       

                      Thanks alot in advance for your help!!

                      • Re: Macro for .STEP from drwaing
                        Dirk van de Ven

                        Thanks alot guys! I found one problem.... The save-to folder isn't always the right one. If i open a drawing and save it to step, it sometime saves it in a folder of a drawing i opened and closed earlier.

                         

                        Example:

                        I open drawing 1.2.A in folder A.

                        Save the configured parts to step.

                        The save file is now: 1.2.A.step in folder A

                        This is correct.

                         

                        I close every drawing and open drawing 2.4.A in folder B

                        Save the configured part to step.

                        The saved file is now: 2.4.A.step in folder A

                        This isn't right.

                         

                        I close every drawing and open drawing 6.3.A in folder C

                        Save the configured part to step.

                        The saved file is now: 6.3.A.step in folder A or folder B (don't see any obvious pattern in this)

                         

                        The macro is now like this:

                         

                        'Option Explicit

                         

                         

                         

                        Sub main()

                         

                         

                         

                        Dim swApp              As SldWorks.SldWorks

                         

                        Dim swModel            As SldWorks.ModelDoc2

                         

                        Dim swDrawModel        As SldWorks.ModelDoc2

                         

                        Dim swModelPDF          As SldWorks.ModelDoc2

                         

                        Dim swDraw              As SldWorks.DrawingDoc

                         

                        Dim swModelDocExt      As SldWorks.ModelDocExtension

                         

                        Dim swExportPDFData    As SldWorks.ExportPdfData

                         

                        Dim swView              As SldWorks.View

                         

                        Dim nErrors            As Long

                         

                        Dim nWarnings          As Long

                         

                        Dim dFileName          As String

                         

                        Dim pFileName          As String

                         

                        Dim boolstatus          As Boolean

                         

                         

                         

                         

                         

                        Set swApp = Application.SldWorks

                         

                        Set swDrawModel = swApp.ActiveDoc

                         

                         

                         

                        '-----------Check to see if a drawing is loaded-----------

                         

                        If swDrawModel Is Nothing Then

                         

                                MsgBox "There is no active drawing document"

                         

                                Exit Sub

                         

                        End If

                         

                         

                         

                        If swDrawModel.GetType <> swDocDRAWING Then

                         

                                MsgBox "Open a drawing first and then TRY again!"

                         

                                Exit Sub

                         

                        End If

                         

                         

                         

                        If swDrawModel.GetPathName = "" Then

                         

                                MsgBox "Plese Save the Drawing and then TRY again!"

                         

                                swDrawModel.Save

                         

                                Exit Sub

                         

                        End If

                         

                         

                         

                        Set swDraw = swDrawModel

                         

                        Set swView = swDraw.GetFirstView

                         

                        Set swView = swView.GetNextView

                         

                         

                         

                        '-----------Determine if there is any model view-----------

                         

                        If swView Is Nothing Then

                         

                                MsgBox "No View(s) found, Insert a View first and then TRY again!"

                         

                                Exit Sub

                         

                        End If

                         

                         

                         

                        '-----------Determine if there is any model view-----------

                         

                        If swView.GetReferencedModelName = "" Then

                         

                                MsgBox "No Model View(s) found, Insert a View first and then TRY again!"

                         

                                Exit Sub

                         

                        End If

                         

                         

                         

                        Set swModel = swView.ReferencedDocument

                         

                         

                         

                        '-----------Drawing File Name Without Extension-----------

                         

                        dFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1) 'filename w/ext

                         

                        dFileName = Left(dFileName, InStrRev(dFileName, ".") - 1) 'filename wo/ext

                         

                        dFileName = UCase(CurDir$ & "\" & dFileName)

                         

                         

                         

                        '-----------Model File Name Without Extension-----------

                         

                        pFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

                         

                        pFileName = Left(pFileName, InStrRev(pFileName, ".") - 1)

                         

                        pFileName = UCase(Path + pFileName)

                         

                         

                         

                        '-----------Save as STEP-----------

                         

                        Set swModel = swApp.ActivateDoc3(swModel.GetPathName, False, swRebuildActiveDoc, nErrors)

                         

                        swModel.ShowConfiguration2 swView.ReferencedConfiguration

                         

                         

                        bRebuild = swModel.ForceRebuild3(False)

                         

                        Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Specify Type of STEP (214 or 203)

                         

                        swModel.SaveAs (dFileName & ".STEP")

                         

                         

                         

                        swApp.CloseDoc swModel.GetTitle

                         

                         

                         

                        End Sub

                         

                        Thanks alot in advance for your help!!