Macro to configure a component

Hello, I'm looking for a macro that allows me to configure a selected room.

This is an operation that  we do  very often and that takes us time.

My assembly and my part have the same configuration names (01,02,03,04...).

I would like the macro to configure my component so that its configurations match those of  the assembly.

Thank you very much.

Hello

Normally, the code below should meet the need.

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swConfMgr                   As SldWorks.ConfigurationManager
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swChildComp                 As SldWorks.Component2
    Dim sConf                       As String
    Dim sChildConf                  As String

    
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swConfMgr = swModel.ConfigurationManager
    sConf = swConfMgr.ActiveConfiguration.Name
    
    Set swChildComp = swSelMgr.GetSelectedObject6(1, -1)
    sChildConf = swChildComp.ReferencedConfiguration
    If sChildConf <> sConf Then
    
    swChildComp.ReferencedConfiguration = sConf
    swModel.EditRebuild3
    End If
    
End Sub

 

Hello Cyril,

Thank you very much, your code works, but only on the active configuration.

There is a little something missing to sweep all the configurations of the assembly I think.

Do you have an idea?

Thank you very much.

Depending on the number of configurations to be generated, there is not necessarily a need for a macro.
In the window to open the assembly file, choose the asm, click on "Configuration", choose "advanced"

, a new window opens:

Use Last Choice

2 Likes

To try

   Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swConfMgr                   As SldWorks.ConfigurationManager
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swChildComp                 As SldWorks.Component2
    Dim sConf                       As String
    Dim sChildConf                  As String
    Dim boolstatus                  As Boolean

    
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swConfMgr = swModel.ConfigurationManager
    
    vConfigNameArr = swModel.GetConfigurationNames
    For Each vConfigName In vConfigNameArr
        Set swConfig = swModel.GetConfigurationByName(vConfigName)
            sConf = vConfigName
            Set swChildComp = swSelMgr.GetSelectedObject6(1, -1)
            swModel.ShowConfiguration2 (vConfigName)
                boolstatus = swModel.Extension.SelectByID2(swChildComp.GetSelectByIDString, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
                Set swChildComp = swSelMgr.GetSelectedObject6(1, -1)
                sChildConf = swChildComp.ReferencedConfiguration
                If sChildConf <> sConf Then
                swChildComp.ReferencedConfiguration = sConf
                swModel.EditRebuild3
                End If
                
        Next
    
End Sub

 

2 Likes

Thank you for this answer.

I didn't know about this option.

However, in my case, I am not generating a configuration, but I want one of the components to take the same configuration name as the assembly in which it is located.

I need a macro that, like the screenshot below, configures the component based on the assembly's configurations.

I don't know if I'm clear.

Thank you very much in any case.


capture.png

FORMIDABLE CYRIL, 

It works perfectly.

A big thank you to you and Stef.

1 Like

And to finish on a high note, is there a solution to make it work by selecting several pieces?

Thank you

Hello

We should close on the content of the assembly and no longer rely on the selection.

On the other hand, it requires a little more processing to check if the file contains a configuration plus any other particular case related to your assemblies. A bit more complex.

Okay, your method is already working very well,  we thank you very much. You have saved us precious time and I will study  your  macro in order to make others.

Is there a proper tutorial available?

Hello

The only "tutorial" is the online help of the API.

Then there is the macro recorder (limited because it doesn't always use the latest API functions and only does "simple" code) and the Solidworks forums which are a gold mine on these subjects (often in English though).