14 Replies Latest reply on Sep 19, 2016 7:25 AM by Maxime Berget

    How to export a part from an assembly in step for each configuration of this part?

    Maxime Berget

      Hi every body,

       

      I'm trying to use VBA to write some part of an assembly in step, unsuccessfully!!

       

      My part has many configuration, and I want to save each of them in step, with a special coordinate system I have create.. I have trye to record a macro and then edit it, as I simply do in other software, but here it seems to be al little more commplicated... Do you have any ideas?

       

      Many thanks

       

      Regards

        • Re: How to export a part from an assembly in step for each configuration of this part?
          Amen Allah Jlili

          Hello Maxime!

           

          I hope you're doing well.

           

          This code will traverse through all of your parts' configurations and saves them in step format in the folder of your original part!
          It will prompt a message at the end whether you want to open the folder or not. You can change the variable CoordinateSystemStr's value with the name of your coordinate system from the feature tree manager.


          Your feedback will be appreciated!

           

           

          Bonjour Maxime!

           

          J'espère que tout va bien!

           

          Le code ci-dessus parcourt les configurations de votre modèle et puis sauvegarde chaque configuration, en format step, dans le dossier du fichier du modèle original! A la fin de l’exécution du code, un message vous proposera d'ouvrir le dossier après la conversion. Vous pouvez changer la valeur variable CoordinateSystemStr avec le nom du votre système de coordonnées dans votre arabe de création.

           

           

           

          Regards/Best,

           

          Code:

          '' Code par JLILI Amen / Jliliamen@gmail.com CSWA/CSWP QA-Mechanical Engineer
          '' https://tn.linkedin.com/in/djelly

          Dim swApp As SldWorks.SldWorks

          Sub main()

          '' Nom de systeme de coordonnes

          Dim CoordinateSystemStr As String: CoordinateSystemStr = "Coordinate System1"

          '' Application SolidWorks

          Set swApp = Application.SldWorks

          '' Variable Modèle

          Dim swModelDoc As SldWorks.ModelDoc2

          '' Affectation de la modèle active

          Set swModelDoc = swApp.ActiveDoc

          '' Liste de noms des configurations

          Dim VConfiguration As Variant

          VConfiguration = swModelDoc.GetConfigurationNames

          '' Test sur la liste de noms (si elle est vide)

          If Not IsEmpty(VConfiguration) Then

          '' Parcours de la liste de noms des configurations

          For i = LBound(VConfiguration) To UBound(VConfiguration)

          'Activer la configuration

          Dim Bol As Boolean

          Bol = swModelDoc.ShowConfiguration2(VConfiguration(i))

          'Cochez l'option du systeme de coordonnes

          Dim swModelExtension As SldWorks.ModelDocExtension

          Set swModelExtension = swModelDoc.Extension

          Bol = swModelExtension.SetUserPreferenceString(swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CoordinateSystemStr)

          'Sauvegarder

          Bol = swModelDoc.SaveAs(VConfiguration(i) + ".step")

          Next i

          Dim result As VbMsgBoxResult: result = MsgBox("Conversion terminée!", vbYesNo, "Conversion à step")

          If result = vbYes Then

          Shell "cmd /C start """" /max """ & swModelDoc.GetPathName & """", vbHide

          End If

          End If

          End Sub

           

            • Re: How to export a part from an assembly in step for each configuration of this part?
              Maxime Berget

              Dear Jlili,

               

              Thanks a lot for your quick and efficient answer.

               

              However I won't be able to explain what I wanted . I won't save the entire assembly in STEP, but only 1 piece wich have several configuration, build

              with a table: this is a familly of piece: the hilighted blue one.

              project.bmp

               

              Is this as easy for you, or it's a new challenge

               

              Many thanks in advance.

               

              Regards

               

              Maxime

                • Re: How to export a part from an assembly in step for each configuration of this part?
                  Amen Allah Jlili

                  Understood (Correct me if I'm wrong). You want to be able to save an assembly component while utilizing the assembly's system of coordinate as the component's default coordinate system. I'll take a look at it sometime tonight. My desk is clouded with paperwork right now.

                   

                  As for the challenge, rien est impossible !

                    • Re: How to export a part from an assembly in step for each configuration of this part?
                      Maxime Berget

                      I want to save an assembly component while utilizing a system of coordinates I created (called "speos" in the preview image) in step format. In other word, to avoid all misunderstanding:

                       

                      Without a macro, I will do this:

                      1) Put the higlighted in blue component in the right configuration.

                      2) Select with the mouse this component in this configuration

                      3) select the "save as" command

                      enregistrer sous.bmp

                       

                      4) Select the "step" format, and then with "option" button select the coordinate system.

                       

                      step option.bmp

                       

                      5) do it till I saved all my component configuration in step.

                       

                      If Y could save them with the name of component configuration name, it would be "the cherry on the cake"!!

                       

                      Thanks

                       

                      Maxime

                        • Re: How to export a part from an assembly in step for each configuration of this part?
                          Amen Allah Jlili

                          Hello Maxime!

                           

                          I'm so sorry for my late response.


                          Please try this following code and give me your feedback.

                          There is only one pre-condition for this code to execute without error:
                          Your designated part must be the only selected object in your assembly. To perform a proper selection, click on the component from the feature tree manager.

                          Change the coordinateSystemStr's value accordingly.

                           

                          Best,

                           

                          PS: I don't like cherry but cake would be fine

                           

                           

                          '' Code par JLILI Amen / Jliliamen@gmail.com CSWA/CSWP QA-Mechanical Engineer
                          '' https://tn.linkedin.com/in/djelly
                          Dim swApp As SldWorks.SldWorks
                          Sub main()
                          On Error GoTo Handler:
                          '' Nom de systeme de coordonnes
                          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                          Dim CoordinateSystemStr As String: CoordinateSystemStr = "Coordinate System1"
                          ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                          '' Application SolidWorks
                          Set swApp = Application.SldWorks
                          '' Variable Modèle
                          Dim swModelDoc As SldWorks.ModelDoc2
                          '' Affectation de la modèle active
                          Set swModelDoc = swApp.ActiveDoc
                          '' Selection Manager
                          Dim swSelectionManager As SldWorks.SelectionMgr
                          Set swSelectionManager = swModelDoc.SelectionManager
                          '' Affection du fichier d'assemblage
                          Dim swAssemblyDoc As SldWorks.AssemblyDoc
                          Set swAssemblyDoc = swModelDoc
                          '' Dossier de sauvegarde
                          Dim Folder As String: Folder = swModelDoc.GetPathName
                          '' Selection de la pièce
                          Dim swComponent As Component2
                          Set swComponent = swSelectionManager.GetSelectedObject(1)
                          Set swModelDoc = swComponent.GetModelDoc2
                          '' Liste de noms des configurations
                          Dim VConfiguration As Variant
                          VConfiguration = swModelDoc.GetConfigurationNames
                          '' Test sur la liste de noms (si elle est vide)
                          If Not IsEmpty(VConfiguration) Then
                          '' Parcours de la liste de noms des configurations
                          For i = LBound(VConfiguration) To UBound(VConfiguration)
                          'Activer la configuration
                          Dim Bol As Boolean
                          Bol = swModelDoc.ShowConfiguration2(VConfiguration(i))
                          'Cochez l'option du systeme de coordonnes
                          Dim swModelExtension As SldWorks.ModelDocExtension
                          Set swModelExtension = swModelDoc.Extension
                          Bol = swModelExtension.SetUserPreferenceString(swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CoordinateSystemStr)
                          'Sauvegarder
                          Bol = swModelDoc.SaveAs(VConfiguration(i) + ".step")
                          Next i
                          Dim result As VbMsgBoxResult: result = MsgBox("Conversion terminée!", vbYesNo, "Conversion à step")
                          If result = vbYes Then
                          Shell "cmd /C start """" /max """ & Folder & """", vbHide
                          End If
                          End If
                          Exit Sub
                          Handler:
                          swApp.SendMsgToUser ("Une erreur s'est produite lors de l'éxecution du macro.")
                          End Sub
                          
                            • Re: How to export a part from an assembly in step for each configuration of this part?
                              Maxime Berget

                              Ok thanks a lot,

                               

                              This is almost perfect.

                              The bad thing is: my coordinate system is not taken into account.

                               

                              When I run the debug mode and the spy window, I can see after this line:

                               

                              "Bol = swModelExtension.SetUserPreferenceString(swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CoordinateSystemStr)" that the Bol variable is set to "False"..

                               

                              I search in the API help but didn't find anything which can help me...

                               

                              One more question: where do you think can I learn the most efficiency the Solidworks API? Are there books, good tutoriel?

                               

                              Thank you

                               

                              Maxime

                               

                              NB: For the cake, my friends tell that I am "black belt" in tiramisu!!

                                • Re: How to export a part from an assembly in step for each configuration of this part?
                                  Amen Allah Jlili

                                  Hello Maxime!

                                   

                                  Please excuse me! (I still want the cake though)

                                   

                                  I was in such a rush to answer your question this morning that I made the mistake of assigning the system coordinate system to the component rather than the assembly file. That explains why the boolean was returning false. Here's the corrected code:

                                   

                                  '' Code par JLILI Amen / Jliliamen@gmail.com CSWA/CSWP QA-Mechanical Engineer
                                  '' https://tn.linkedin.com/in/djelly
                                  Dim swApp As SldWorks.SldWorks
                                  Sub main()
                                  On Error GoTo Handler:
                                  '' Nom de systeme de coordonnes
                                  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                                  Dim CoordinateSystemStr As String: CoordinateSystemStr = "Coordinate System1"
                                  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                                  '' Application SolidWorks
                                  Set swApp = Application.SldWorks
                                  '' Variable Modèle
                                  Dim swModelDoc As SldWorks.ModelDoc2
                                  '' Affectation de la modèle active
                                  Set swModelDoc = swApp.ActiveDoc
                                  '' Selection Manager
                                  Dim swSelectionManager As SldWorks.SelectionMgr
                                  Set swSelectionManager = swModelDoc.SelectionManager
                                  '' Affection du fichier d'assemblage
                                  Dim swAssemblyDoc As SldWorks.AssemblyDoc
                                  Set swAssemblyDoc = swModelDoc
                                  '' Dossier de sauvegarde
                                  Dim Folder As String: Folder = swModelDoc.GetPathName
                                  '' Selection de la pièce
                                  Dim swComponent As Component2
                                  Set swComponent = swSelectionManager.GetSelectedObject(1)
                                  Dim SwModelFromComp As ModelDoc2
                                  Set SwModelFromComp = swComponent.GetModelDoc2
                                  '' Liste de noms des configurations
                                  Dim VConfiguration As Variant
                                  VConfiguration = swModelDoc.GetConfigurationNames
                                  '' Test sur la liste de noms (si elle est vide)
                                  If Not IsEmpty(VConfiguration) Then
                                  '' Parcours de la liste de noms des configurations
                                  For i = LBound(VConfiguration) To UBound(VConfiguration)
                                  'Activer la configuration
                                  Dim Bol1 As Boolean
                                  Bol1 = swModelDoc.ShowConfiguration2(VConfiguration(i))
                                  'Cochez l'option du systeme de coordonnes
                                  Dim swModelExtension As SldWorks.ModelDocExtension
                                  Set swModelExtension = swModelDoc.Extension
                                  Dim Bol2 As Boolean
                                  Bol2 = swModelExtension.SetUserPreferenceString(swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CoordinateSystemStr)
                                  'Sauvegarder
                                  Dim Bol3 As Boolean
                                  Bol3 = SwModelFromComp.SaveAs(VConfiguration(i) + ".step")
                                  Next i
                                  Dim result As VbMsgBoxResult: result = MsgBox("Conversion terminée!", vbYesNo, "Conversion à step")
                                  If result = vbYes Then
                                  Shell "cmd /C start """" /max """ & Folder & """", vbHide
                                  End If
                                  End If
                                  Exit Sub
                                  Handler:
                                  swApp.SendMsgToUser ("Une erreur s'est produite lors de l'éxecution du macro.")
                                  End Sub
                                  

                                  The boolean is now returning true.

                                  DISPLAY.png

                                   

                                   

                                   

                                   

                                  Want to learn the API?

                                   

                                   

                                  There's a great book titled automating Solidworks using macros. That's the only book I know (and read). It's a good introduction to the world of CAD automation (VBA and VSTA macros). You can find tons of good answers here too. If you want to go to the extreme professional side of SW then you should learn about add-ins which are complements that extend SW functionanlities. The possibilities are limitless. (Notable mention goes to CAMWorks, DriveWorks...). They require a strong background on the API, .NET and COM objects. The best way to learn is to ask questions so do not be afraid to ask. I, hereby, extend you the invitation to ask me questions on my personal email (jliliamen@gmail.com), no problem.

                                   

                                  Best,
                                  Amen

                                    • Re: How to export a part from an assembly in step for each configuration of this part?
                                      Maxime Berget

                                      I really Thank you for your time and advise.

                                       

                                       

                                      This code still doesn't have the good behavior, although the 3 boolean variables take the "true" value!

                                       

                                      I perhaps saw some mistake, correct me if I'm wrong:

                                       

                                      line 31. VConfiguration = swModelDoc.GetConfigurationNames <=> VConfiguration = SwModelFromComp.GetConfigurationNames ?

                                      line 38. Bol1 = swModelDoc.ShowConfiguration2(VConfiguration(i))  <=> Bol1 = SwModelFromComp.ShowConfiguration2(VConfiguration(i)) ?

                                       

                                      line 41. Set swModelExtension = swModelDoc.Extension => no idea!!

                                       

                                      Moreover, I don't understand when you use the "Folder" string to set the folder where to save the part.

                                       

                                      Thank you

                                       

                                      Maxime

                                        • Re: How to export a part from an assembly in step for each configuration of this part?
                                          Amen Allah Jlili

                                          Hello,

                                           

                                          Need not to worry about the folder variable. The last command merely opens the folder of the your assembly file because that's where your step files are saved. The SwModelDoc is your assembly file. We need that in order to change the coordinate in the export window since we're doing the saving from the assembly file as our active doc (This might be where the problem resides and there's a painful solution). The swModelFromComp is the selected part from the feature tree manager. It's an assembly component2 object from which an modeldoc is taken and saved as using the saveas method as a step file.

                                           

                                          I might be missing something?

                                           

                                          What I want to know is does the step files have the assembly's coordinate system when you open them?


                                          There's the solution of hiding the rest of the parts and exporting as step. I'll code something tomorrow if I can.
                                          Export only “Selected Parts” from SolidWorks Assembly | Boxer's CAD CAM Blog

                                           

                                          Best,

                          • Re: How to export a part from an assembly in step for each configuration of this part?
                            Maxime Berget

                            Hello,

                             

                            My step file must have the coordinate system I have created in my assembly, and not the original assembly coordinate system!

                            Thank you again for your time!

                             

                            Maxime