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
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"
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
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.
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.
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).