17 Replies Latest reply on Jul 2, 2014 8:22 AM by Wes Cox

    API - How to change configuration name?

    Michael Caulton

      Can someone please give me a hint on how to change the configuration name. My searches are drawing a blank.

       

      As an example, on a new part I want to change the active configuration name from "Default" to match the filename.

       

      Cheers, Mike

        • Re: API - How to change configuration name?
          Artem Taturevych

          Hi Mike,

           

          Please try this:

           

          Dim swApp as SldWorks.SldWorks

           

          Sub main()

           

          Set swApp = Application.SldWorks

          swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = swApp.ActiveDoc.GetTitle()

           

          End Sub

            • Re: API - How to change configuration name?
              Sanya Shmidt

              Does this activates the configuration or it just changes the name of it?

              Say if I have a part with 3 configurations if I call

               

              swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name ="7GA"

               

              should it activate that configuration? ( i tried , may be I`m doing something wrong, it doesn change anything)

               

              Thank you

                • Re: API - How to change configuration name?
                  Artem Taturevych

                  No it doesn't, furthermore the following means that the configuration already active:

                   

                  swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration

                   

                  If you want to active a configuration by name you should call ModelDoc2::ShowConfiguration2.

                   

                  swApp.ActiveDoc.ShowConfiguration2 "MyConfName"

                  ______________________________________________

                  Regards, Artem Taturevych | Snr. Developer | IC3D ANZ

                   

                  IC3DSteel – New Steel Solution for SolidWorks

                  translationXpert – SolidWorks files language translator

                  LinkedIn - SolidWorks API Group

                    • Re: API - How to change configuration name?
                      Yong Ning

                      Dim SwApp As SldWorks.SldWorks

                       

                      Sub main()

                       

                        Set SwApp = Application.SldWorks

                        SwApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = "ffff" 'swApp.ActiveDoc.GetTitle()

                       

                      End Sub

                       

                      rstult see picture

                      0.jpg

                       

                       

                       

                      Sub ll()

                        Set SwApp = Application.SldWorks

                        SwApp.ActiveDoc.ShowConfiguration2 "默认"

                      End Sub

                       

                      result see picture

                       

                      1.jpg

                       

                      ********************************************

                      0.jpg

                      *********************************************************************************************

                       

                      '----------------------------------------------
                      '
                      ' Problem:
                      '       Configurations are a means of modeling families of parts and assemblies,
                      '       or to describing different arrangements of the same part or assembly.
                      '       Configurations are powerful and flexible, but this comes
                      '       at a cost of managing the configurations. Typically,
                      '       document management systems do not handle configurations
                      '       well. Furthermore, company practices may have to be revised to allow
                      '       for configurations in the design process.
                      '
                      '       Typically, a configuration is a collection of dimensions and
                      '       features, each having a value specific to a configuration.
                      '       A dimension usually has a value whereas a feature
                      '       usually has a suppression state. The configuration is stored
                      '       inside the SolidWorks file as an Excel spreadsheet. There may also
                      '       be other types of items and the interpretation of these will be up
                      '       to the Excel spreadsheet.
                      '
                      '       This code shows how to extract and list all the parameters and their
                      '       values from a model.
                      '
                      ' Preconditions: Part or assembly is open.
                      '
                      ' Postconditions:
                      '       None
                      '
                      ' NOTES:
                      '       (1) Currently, configurations are supported in parts
                      '           and assemblies only; they are not supported in drawings.
                      '       (2) All parameters and their values are returned as strings. It
                      '           is up to the application to interpret the meaning of each
                      '           parameter and perform the appropriate conversion.
                      '
                      '----------------------------------------------
                      Option Explicit
                      Sub main()
                         Dim swApp                   As SldWorks.SldWorks
                         Dim swModel                 As SldWorks.ModelDoc2
                         Dim swConfigMgr             As SldWorks.ConfigurationManager
                         Dim vConfName               As Variant
                         Dim vConfParam              As Variant
                         Dim vConfValue              As Variant
                         Dim i                       As Long
                         Dim j                       As Long
                         Dim bRet                    As Boolean
                         Set swApp = Application.SldWorks
                         Set swModel = swApp.ActiveDoc
                         Set swConfigMgr = swModel.ConfigurationManager
                        
                         Debug.Print "File = " + swModel.GetPathName
                        
                         vConfName = swModel.GetConfigurationNames
                         For i = 0 To UBound(vConfName)
                             bRet = swConfigMgr.GetConfigurationParams(vConfName(i), vConfParam, vConfValue)
                             Debug.Assert bRet
                            
                             Debug.Print "  Config       = " & vConfName(i)
                             If Not IsEmpty(vConfParam) Then
                                 For j = 0 To UBound(vConfParam)
                                     Debug.Print "    " & vConfParam(j) & " = " & vConfValue(j)
                                 Next j
                             End If
                         Next i
                      End Sub
                      '-------------------------------------------


                      code result

                      File = E:\MyWorkSummary\BESmodel\JB4721\aa.SLDPRT
                        Config       = ffff
                          D1@草图1 = 42.000000
                          D2@草图1 = 41.999808
                          D1@拉伸1 = 10.000000
                        Config       = aaaa
                          $零件号 = $C
                          $零件号 = $C
                          D1@草图1 = 50.000000
                          D2@草图1 = 50.000000
                          D1@拉伸1 = 20.000000

                  • Re: API - How to change configuration name?
                    Wes Cox

                    This renames the configuration to "filename.sldprt". I would like to rename my configuration to "filename". How do I exclude the file extension?

                    On a somewhat similar thread I saw that you recommended using:

                     

                    "FilePath = Path.GetFileNameWithoutExtension(swDoc.GetPathName())"

                     

                    but I've been unable to find a way to make that work in this macro and am uncertain if that's the way to go. Thank you.

                      • Re: API - How to change configuration name?
                        Deepak Gupta

                        Try this:

                         

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                         

                        Sub main()

                         

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                         

                         

                           If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = Strings.Left(swModel.GetTitle, swModel.GetTitle - 7)

                            Else

                                swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = swModel.GetTitle

                            End If

                         

                        End Sub

                          • Re: API - How to change configuration name?
                            Wes Cox

                            Deepak,


                            Thank you, I wouldn't have found this answer without your help. One question, when I run this I get a "Run-Time error "13" , Mismatch.

                             

                            Any ideas on what I'm missing, or what mistake I'm making?

                             

                            Thank you!

                              • Re: API - How to change configuration name?
                                Deepak Gupta

                                I've tested this macro on SW2014 and it has worked w/out any errors. Which version you're trying the macro on.

                                  • Re: API - How to change configuration name?
                                    Wes Cox

                                    Good point, I'm driving an antique: SW2010

                                      • Re: API - How to change configuration name?
                                        Deepak Gupta

                                        Can't be sure as why thid would not work.

                                         

                                        Try this:

                                         

                                        Option Explicit

                                         

                                        Sub main()

                                         

                                            Dim swApp                       As SldWorks.SldWorks

                                            Dim swModel                     As SldWorks.ModelDoc2

                                            Dim swConf                      As SldWorks.Configuration

                                            Dim swConfMgr                   As SldWorks.ConfigurationManager

                                           

                                            Set swApp = Application.SldWorks

                                            Set swModel = swApp.ActiveDoc

                                            Set swConfMgr = swModel.ConfigurationManager

                                            Set swConf = swConfMgr.ActiveConfiguration

                                         

                                                If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                                swConf.Name = Strings.Left(swModel.GetTitle, swModel.GetTitle - 7)

                                            Else

                                                swConf.Name = swModel.GetTitle

                                            End If

                                         

                                        End Sub

                                          • Re: API - How to change configuration name?
                                            Wes Cox

                                            It must be something on my end, because for both versions of the macro I get the mismatch in both 2010 and 2014. The debugger highlights this line:

                                             

                                            swConf.Name = Strings.Left(swModel.GetTitle, swModel.GetTitle - 7)

                                             

                                            I've copied and pasted your text carefully several times, but same error. I wonder if I am missing a reference for the macro? Nop sure if that seems likely. I'll work with this and see if I can discover what is going wrong, becasue as you said, it works for you.

                                             

                                            Thank you Deepak!

                                              • Re: API - How to change configuration name?
                                                Deepak Gupta

                                                Change this line

                                                swConf.Name = Strings.Left(swModel.GetTitle, swModel.GetTitle - 7)

                                                to

                                                swConf.Name = Left(swModel.GetTitle, swModel.GetTitle - 7)

                                                 

                                                and try again.

                                                  • Re: API - How to change configuration name?
                                                    Wes Cox

                                                    I've tried this replacement:

                                                     

                                                    swConf.Name = Left(swModel.GetTitle, swModel.GetTitle - 7)

                                                     

                                                    but still get the error. Debugger points to that replacement line. I tried with both 2010 and 2014, and I have run the macro with different parts open with the same result.

                                                     

                                                    _______________

                                                     

                                                     

                                                    Option Explicit

                                                     

                                                    Sub main()

                                                     

                                                        Dim swApp                       As SldWorks.SldWorks

                                                        Dim swModel                     As SldWorks.ModelDoc2

                                                        Dim swConf                      As SldWorks.Configuration

                                                        Dim swConfMgr                   As SldWorks.ConfigurationManager

                                                       

                                                        Set swApp = Application.SldWorks

                                                        Set swModel = swApp.ActiveDoc

                                                        Set swConfMgr = swModel.ConfigurationManager

                                                        Set swConf = swConfMgr.ActiveConfiguration

                                                     

                                                            If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                                            swConf.Name = Left(swModel.GetTitle, swModel.GetTitle - 7)

                                                        Else

                                                            swConf.Name = swModel.GetTitle

                                                        End If

                                                     

                                                    End Sub

                                                     


                                                      • Re: API - How to change configuration name?
                                                        Deepak Gupta

                                                        Sorry Wes but there was a typo in the code which finally has come out. I forgot to add the Len in that. Here is the updated code.

                                                         

                                                        Option Explicit

                                                         

                                                        Sub main()

                                                         

                                                            Dim swApp                       As SldWorks.SldWorks

                                                            Dim swModel                     As SldWorks.ModelDoc2

                                                            Dim swConf                      As SldWorks.Configuration

                                                            Dim swConfMgr                   As SldWorks.ConfigurationManager

                                                          

                                                            Set swApp = Application.SldWorks

                                                            Set swModel = swApp.ActiveDoc

                                                            Set swConfMgr = swModel.ConfigurationManager

                                                            Set swConf = swConfMgr.ActiveConfiguration

                                                         

                                                                If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                                                swConf.Name = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                                                            Else

                                                                swConf.Name = swModel.GetTitle

                                                            End If

                                                         

                                                        End Sub

                                                         

                                                        Here is further more shorter version of it

                                                         

                                                        Option Explicit

                                                         

                                                        Sub main()

                                                         

                                                            Dim swApp                       As SldWorks.SldWorks

                                                            Dim swModel                     As SldWorks.ModelDoc2

                                                              

                                                            Set swApp = Application.SldWorks

                                                            Set swModel = swApp.ActiveDoc

                                                         

                                                                If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                                                swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                                                            Else

                                                                swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = swModel.GetTitle

                                                            End If

                                                         

                                                        End Sub