4 Replies Latest reply on Feb 19, 2015 10:39 AM by Charles Robin

    Edit Configurations Properties Alternates Name of all the component in assy

    Charles Robin

      Hi everyone

       

       

      i would like to create a macro who change the alternate name of all the composant in an assembly.

       

      The alternate name is the "part number displayed when used in a bill of materials".

       

      I got a macro to change the active document alternates names of all config..

       

      How to get the alternate name of a child (component in a assembly)?

       

      Thank you very much!

          • Re: Edit Configurations Properties Alternates Name of all the component in assy
            Charles Robin

            Thank you Deepak Gupta

             

            I reminds you that i m not asking how to get the config name, but the alternate name in config properties.

             

            Here an example to get the alternate name  in the case of the Active Document:

            2012 SOLIDWORKS API Help - Change Configuration Properties Example (VBA)

             

            but i dont found what is the instance in the case of a Children Component.

             

            instance.AlternateName = value

             

            value = instance.AlternateName

             

             

             

            i've try this but its not working:

             

            swApp.ActiveDoc.GetConfigurationByName(vConfigName).AlternateName

            become....

            svChildComp.GetConfigurationByName(vConfigName).AlternateName

             

             

            I wonder if it is possible to get the alternatename without open the child component file..?

             

            thank you

              • Re: Edit Configurations Properties Alternates Name of all the component in assy
                Deepak Gupta

                I wonder if it is possible to get the alternate name without open the child component file..?

                 

                thank you

                Here are the codes you can use to get alternate name of selected component's configuration used in assembly.

                 

                Sub main()

                 

                 

                    Dim swApp                   As SldWorks.SldWorks

                    Dim swSelMgr                As SldWorks.SelectionMgr

                    Dim swModel                 As SldWorks.ModelDoc2

                    Dim swComp                  As SldWorks.Component2

                    Dim swConf                  As SldWorks.Configuration

                   

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    Set swSelMgr = swModel.SelectionManager

                 

                 

                    Set swComp = swSelMgr.GetSelectedObject5(1)

                    Set swConf = swComp.GetModelDoc.GetConfigurationByName(swComp.ReferencedConfiguration)

                   

                    Debug.Print swConf.AlternateName

                   

                End Sub

                  • Re: Edit Configurations Properties Alternates Name of all the component in assy
                    Charles Robin

                    Thanks a lot!

                     

                    I miss ".GetModelDoc."

                     

                     

                    Here the code VBA for all config of all components in an assembly:

                    Sub Main()

                     

                     

                     

                     

                    Debug.Print "_____________________________________________________________________ "

                        Dim swApp                  As SldWorks.SldWorks

                     

                     

                        Dim swModel                As SldWorks.ModelDoc2

                     

                     

                        Dim swConfigMgr            As SldWorks.ConfigurationManager

                     

                     

                        Dim swConfig                As SldWorks.Configuration

                     

                     

                        Dim swRootComp              As SldWorks.Component2

                     

                     

                        Dim Children                As Variant

                     

                     

                        Dim swChild                As SldWorks.Component2

                     

                     

                        Dim ChildCount              As Integer

                     

                     

                        Dim OldName                As String

                      

                        Dim ViewOldName As String

                     

                     

                        Dim NewName                As String

                        Dim textebox1 As String

                     

                     

                        Dim bOldSetting            As Boolean

                     

                     

                        Dim bRet                    As Boolean

                     

                     

                        Dim i                      As Long

                        Dim III                      As String

                        Dim sPathName                  As String

                     

                     

                      

                        Set swApp = Application.SldWorks

                     

                     

                        Set swModel = swApp.ActiveDoc

                     

                     

                        Set swConfigMgr = swModel.ConfigurationManager

                     

                     

                        Set swConfig = swConfigMgr.ActiveConfiguration

                     

                     

                      

                      

                        If swModel Is Nothing Then

                        MsgBox ("Pas de document ouvert")

                        Else

                            If swModel.GetType <> 2 Then

                                MsgBox ("Il ne s'agît pas d'un assemblage")

                                Exit Sub

                            Else

                          

                        Set swRootComp = swConfig.GetRootComponent

                        bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)

                     

                     

                        swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

                     

                     

                      

                        Children = swRootComp.GetChildren

                     

                     

                        ChildCount = UBound(Children)

                     

                     

                        For i = 0 To ChildCount

                      

                        III = i

                     

                     

                            Set swChild = Children(i)

                          

                            sPathName = swChild.GetPathName

                            sPathName = Mid(sPathName, InStrRev(sPathName, "\") + 1)

                            sPathName = Left(sPathName, Len(sPathName) - 7)

                          

                            Debug.Print "  "

                     

                     

                            Debug.Print "COMPONENENT NAME  =      " + sPathName

                     

                     

                            bRet = swChild.Select2(False, 0)

                     

                     

                            change_alternatename swChild, sPathName

                     

                     

                      

                     

                     

                        Next i

                      

                     

                     

                      

                        End If

                        End If

                     

                     

                    End Sub

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                    Sub change_alternatename(swChild As SldWorks.Component2, sPathName As String)

                     

                     

                        Dim swComp                  As SldWorks.Component2

                        Dim swConf                  As SldWorks.Configuration

                     

                        Dim params                  As Variant

                     

                     

                        Dim vName                  As Variant

                     

                     

                        Dim Name                    As String

                     

                     

                        Dim thisConfig              As Configuration

                      

                      

                      

                     

                        params = swChild.GetModelDoc.GetConfigurationNames

                     

                        For Each vName In params

                     

                     

                        Name = vName

                     

                     

                        Set thisConfig = swChild.GetModelDoc.GetConfigurationByName(Name)

                     

                     

                        Debug.Print "Congig Name      =        " + Name

                     

                     

                        Debug.Print "Old AlternateName =          " + thisConfig.AlternateName

                      

                        thisConfig.AlternateName = sPathName

                        

                        Debug.Print "NeW AlternateName =          " + thisConfig.AlternateName

                      

                        thisConfig.BOMPartNoSource = swBOMPartNumber_UserSpecified

                     

                     

                        thisConfig.UseAlternateNameInBOM = False

                     

                     

                     

                     

                    Next vName

                     

                     

                    End Sub