13 Replies Latest reply on Dec 8, 2018 9:21 AM by Deepak Gupta

    Macro not Opening .sldprt for Proper .step Export

    Joseph Walden

      Edit Dec 7 2018,

      Credit for the original macro that I modified in the below thread goes to Artem Taturevych in the below thread:

      https://forum.solidworks.com/message/420553#comment-420553

      ---------------------------------------------------------------

      Running SW 2017

       

      I was excited to get a macro working (a first for me, I'm quote new) until I realized that it wasn't quite doing what I intended for .step (but it worked for .igs). Sigh...

       

      A frustrating issue I've had to deal with is making .step files from assemblies. The issue is that when you save from .sldasm files straight to .step format the resulting assembly does not keep the mechanical mates. Now the mates remain if the steps go as follows:

       

      - save .sldasm to .sldprt

      - open .sldprt

      - resave as .step

       

      It's been insisted that the mates remain and I was excited at first because the below macro did lock the mates for .igs (which I recall had a similar issue).

       

      I have tried #Task but this is having it's own issues to the Standard License, posted on a separate thread:

      #Task "Failed to Obtain a License of Solidworks Task Scheduler"

       

      First, is there a simple option for .step export that locks parts upon opening (like for Parasolid, .x_t)? I've been unable to find anything in SW or the forums. If not, then

      below is the macro at the moment:

       

      ---------------------------------------------------------------

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim fileerror As Long

      Dim filewarning As Long

       

      Sub main()

       

          Set swApp = Application.SldWorks

       

          Set swModel = swApp.ActiveDoc

       

          If Not swModel Is Nothing Then

       

              Dim path As String

              path = swModel.GetPathName

           

              If path <> "" Then

       

                  Dim filename As String

                  filename = InputBox("Enter the CAD String")

       

                  SaveFile filename & ".sldprt"

                  SaveFile filename & ".x_t"

                  SaveFile filename & ".easm"

               

                  'Second Sub is to Lock Assembly Components into Place by opening the resaving the .sldprt file'

                  Dim filename2 As String

                  filename2 = filename & ".sldprt"

                           

                  SaveFile2 filename & ".step"

       

              End If

       

          End If

       

      End Sub

       

      Sub SaveFile(filename As String)

       

          swModel.SaveAs4 filename, _

              swSaveAsVersion_e.swSaveAsCurrentVersion, _

              swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

       

      End Sub

       

      Sub SaveFile2(filename As String)

       

          Set swModel = swApp.OpenDoc6(PathEnd + filename2, swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)

       

          Set swModel = swApp.ActiveDoc

       

          swModel.SaveAs4 filename, _

              swSaveAsVersion_e.swSaveAsCurrentVersion, _

              swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

       

      End Sub

      ---------------------------------------------------------------

      The macro does properly export .step files (no errors appear) but it appears to be saving the files from the assembly and not the opened part (as the OpenDoc6 line in SaveFile2 is intended to do). How do I get the Macro to properly open the .sldprt file and resave it as a .step?

       

      Many thanks in advance,

      Joseph

        • Re: Macro not Opening .sldprt for Proper .step Export
          Danniel Sims

          Joseph,

           

          I don't think there are any mates transferred with the export process.  Those file types have no method to carry the information.  I would check your settings for importing and exporting the filetypes.  If you only want a part, you may have to program user preference settings on export.  I export IGES, STL, STEP, and Parasolids for CADCAM information.  When they are imported back in to check for the coordinate system, only the STLs are parts.  Everything else pulls in as assemblies with sub-part components.  Those are just the settings I need for what I'm doing, you may be able to do something differently, you'd need to look atthe Import/Export settings for your filetypes.  If the assembly is ok and you just don't want things to move, you could cycle through the feature tree after import and fix all the items so they can't move.

           

          Check out SetUserPreferenceToggle  There are subsettings for import/export there.

            • Re: Macro not Opening .sldprt for Proper .step Export
              Joseph Walden

              Hello Danniel,

               

              Thank you for the reply.

               

              I've looked through the SetUserPreferenceToggle Page and there is oddly no option for STEP to export the components into a single file (which is available with IGS and STL).

               

              Same goes for the settings page under Options > System Options > Export > STEP. There is no check box for exporting components into a single file.

               

               

              Might there be some other system setting that I'm missing? Could this also be a result of still using SW 2017?

               

              Your response though makes it sound like if a CAD user downloads a STEP assembly that they should expect the mates originally put in place to no longer be present and that locking components manually in the feature tree is a common routine. Would you say that in your experience that this is common?

               

              Thanks again,

              Joseph

                • Re: Macro not Opening .sldprt for Proper .step Export
                  Danniel Sims

                  What I am doing is different than what you are trying to do... I actually need the parts to  be separate.  However, any time I receive a step file from a vendor for approval it is broken up into an assembly by Solid Works.  On my end, I use a macro to fix all the components where they are before I do anything.  I usually save out the file as a part file just for safety.  As a vendor's item, I don't need to keep all the sub parts around.  When you save the assembly as a part, the step chunks usually turn into surfaces, so when you re-save out the part as a step file it essentially is a single part.

                   

                  Glad you got your answer though.

              • Re: Macro not Opening .sldprt for Proper .step Export
                Artem Taturevych

                Hi Joseph,

                 

                Regarding #TASK - just uncheck the 'Background Mode' option in settings and it will work in Standard:

                 

                Thanks,

                Artem

                  • Re: Macro not Opening .sldprt for Proper .step Export
                    Joseph Walden

                    Deepak,

                     

                    Thank you for your reply.

                     

                    Unfortunately this results in the same sort of STEP file. The file opens as an assembly and needs to open as a part.

                     

                    Attached is an example exported STEP file from this morning with the above changes implemented. Is it possible that running SW 2017 may be affecting the results?

                     

                    Thank you again,

                    Joseph,

                      • Re: Macro not Opening .sldprt for Proper .step Export
                        Deepak Gupta

                        In you macro how are you defining the path? And issue should be version related.

                        • Re: Macro not Opening .sldprt for Proper .step Export
                          Deepak Gupta

                          Joseph, try the following codes. I've tested the macro and it seems to be working fine for me.

                           

                          Option Explicit
                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim fileerror As Long
                          Dim filewarning As Long
                          Dim path As String
                          Dim filename As String
                          Dim filename2 As String
                          Sub Main()
                          
                              Set swApp = Application.SldWorks
                              Set swModel = swApp.ActiveDoc
                          
                              If Not swModel Is Nothing Then
                                  path = swModel.GetPathName
                                  If path <> "" Then
                                      path = Left(path, InStrRev(path, "\"))
                                      filename = InputBox("Enter the CAD String")
                                      SaveFile path + filename & ".sldprt"
                                      SaveFile path + filename & ".x_t"
                                      SaveFile path + filename & ".easm"
                                      
                                      'Second Sub is to save Assembly as STEP file with all components in one file.
                                      filename2 = path + filename
                                      SaveFile2 filename2            
                                 End If
                              End If
                          End Sub
                          
                          Sub SaveFile(filename As String)
                              swModel.SaveAs4 filename, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0
                          End Sub
                          
                          Sub SaveFile2(filename As String)
                              Set swModel = swApp.OpenDoc6(filename & ".sldprt", swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)
                              Set swModel = swApp.ActiveDoc
                              swModel.SaveAs4 filename & ".step", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0
                              swApp.CloseDoc filename & ".sldprt"
                              Kill filename & ".sldprt"
                          End Sub
                          
                            • Re: Macro not Opening .sldprt for Proper .step Export
                              Joseph Walden

                              Deepak,

                               

                              This works correctly and as wanted. Thank you for the help.

                               

                              One minor change I made was removing

                               

                              Kill filename & ".sldprt"

                               

                              from line 39 as I still want to keep the .sldprt file after the macro is done running. Updated macro I have looks as follows:

                              ---------------------------------------------------------------

                              Option Explicit

                              Dim swApp As SldWorks.SldWorks

                              Dim swModel As SldWorks.ModelDoc2

                              Dim fileerror As Long

                              Dim filewarning As Long

                              Dim path As String

                              Dim filename As String

                              Dim filename2 As String

                               

                               

                              Sub main()

                               

                               

                                  Set swApp = Application.SldWorks

                                  Set swModel = swApp.ActiveDoc

                               

                               

                                  If Not swModel Is Nothing Then

                               

                               

                                      path = swModel.GetPathName

                                      If path <> "" Then

                                          path = Left(path, InStrRev(path, "\"))

                                          filename = InputBox("Enter the CAD String")

                               

                               

                                          SaveFile filename & ".sldprt"

                                          SaveFile filename & ".x_t"

                                          SaveFile filename & ".easm"

                               

                               

                                          filename2 = path + filename

                                          SaveFile2 filename2

                               

                               

                                      End If

                                  End If

                              End Sub

                               

                               

                              Sub SaveFile(filename As String)

                               

                               

                                  'save .easm, .sldprt, and .x_t files from the .sldasm file.'

                                  swModel.SaveAs4 filename, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

                                

                              End Sub

                                         

                              Sub SaveFile2(filename As String)

                               

                               

                                  'Second Sub is to Lock Assembly Components into Place by opening and resaving the .sldprt file as a .STEP'

                                  Set swModel = swApp.OpenDoc6(filename & ".sldprt", swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)

                                  Set swModel = swApp.ActiveDoc

                                  swModel.SaveAs4 filename & ".step", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

                                  swApp.CloseDoc filename & ".sldprt"

                                 

                              End Sub

                              ---------------------------------------------------------------

                              Thank you again. Much appreciated.

                              Joseph

                              • Re: Macro not Opening .sldprt for Proper .step Export
                                Joseph Walden

                                Deepak,

                                 

                                I've encountered another problem. If the assembly has multiple configurations it only exports the .STEP file, which is very strange. If the assembly has only a default configuration then it works as expected.

                                 

                                Why am I not seeing the other file formats? I'm still prompted as expected for the .easm file but nothing shows up now.

                                 

                                Thank you,

                                Joseph

                                • Re: Macro not Opening .sldprt for Proper .step Export
                                  Joseph Walden

                                  Deepak,

                                   

                                  Did more digging on the help page and managed to add lines along this example macro to call out the current assembly configuration.

                                   

                                  (declaration)

                                  Dim config As SldWorks.Configuration

                                  (usage)

                                      Set config = swModel.GetActiveConfiguration

                                   

                                  Macro is now this.

                                  ---------------------------------------------------------

                                  Option Explicit

                                  Dim swApp As SldWorks.SldWorks

                                  Dim swModel As SldWorks.ModelDoc2

                                  Dim config As SldWorks.Configuration

                                  Dim fileerror As Long

                                  Dim filewarning As Long

                                  Dim path As String

                                  Dim filename As String

                                  Dim filename2 As String

                                   

                                   

                                  Sub main()

                                   

                                   

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                      Set config = swModel.GetActiveConfiguration

                                   

                                   

                                      If Not swModel Is Nothing Then

                                   

                                   

                                          path = swModel.GetPathName

                                          If path <> "" Then

                                              path = Left(path, InStrRev(path, "\"))

                                              filename = InputBox("Enter the CAD String")

                                   

                                   

                                              SaveFile path + filename & ".sldprt"

                                              SaveFile path + filename & ".x_t"

                                              SaveFile path + filename & ".easm"

                                   

                                   

                                              filename2 = path + filename

                                              SaveFile2 filename2

                                   

                                   

                                          End If

                                      End If

                                  End Sub

                                   

                                   

                                  Sub SaveFile(filename As String)

                                   

                                   

                                      'save .easm, .sldprt, and .x_t files from the .sldasm file.'

                                      swModel.SaveAs4 filename, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

                                   

                                  End Sub

                                            

                                  Sub SaveFile2(filename As String)

                                   

                                   

                                      'Second Sub is to Lock Assembly Components into Place by opening and resaving the .sldprt file as a .STEP'

                                      Set swModel = swApp.OpenDoc6(filename & ".sldprt", swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)

                                      Set swModel = swApp.ActiveDoc

                                      swModel.SaveAs4 filename & ".step", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, 0, 0

                                      swApp.CloseDoc filename & ".sldprt"

                                    

                                  End Sub

                                  ---------------------------------------------------------

                                  Thank you again for all your help.

                                  Joseph,