Enable a Part Configuration in an Assembly with a Macro

Hello

 

I'm looking for a macro that allows me to activate the different configurations of a part from the main assembly.

I do have a piece of code that I have tinkered with but it doesn't work.

 

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("CleanSeal WELDED-1@CleanSeal COMPLETE/Cxxx-x-1@CleanSeal WELDED", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.ShowConfiguration2("C200-0")
Part.ClearSelection2 True
boolstatus = Part.EditRebuild3()
End Sub

 

Thank you in advance for your help

Kind regards.

Hello

I have this piece of code that works at home, see at the level of your boolstatus line, in the first part. I don't understand the "/"... Aren't you editing a component?

Edit: You must have: "Configuration@nomfichier.extension"

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.EditRebuild3()
boolstatus = Part.Extension.SelectByID2("Défaut@12345678-E SUPPORT CYLINDER. SLDASM", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.ShowConfiguration2("Default")
boolstatus = Part.EditRebuild3()
boolstatus = Part.Extension.SelectByID2("WITH REHAUSSE@12345678-E SUPPORT CYLINDER. SLDASM", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.ShowConfiguration2("WITH ELEVATION")
boolstatus = Part.EditRebuild3()


End Sub

1 Like

Hello Glaffont,

First of all, thank you for your answer;)

I took your code by adding but filename but it doesn't work:(

Here's the code:

 

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.EditRebuild3()
boolstatus = Part.Extension.SelectByID2("C250-0@Cxxx-x.part@CleanSeal WELDED. SLDASM", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.ShowConfiguration2("C250-0")
boolstatus = Part.EditRebuild3()

End Sub

 

In your code, you just change the config of your assembly?

For my part, the structure of my 3D is as follows:

 * Main assembly (. SLDASM)

                                       * Subset (. SLDASM)

                                                                        * component (.part)

                                                                        * component (.part)

                                                                       * component(.part)

                                     * various components (.part)

I'd like the code to change the configuration of a component (.part)  in my subset (. SLDASM) while being in my main assembly (. SLDASM).

I hope my explanation is understandable...

Thanks in advance

 

 

 

Hello

I understood the request, it was clear from the beginning but I must have read too quickly.

Indeed in this case it doesn't work for me either. I tried to edit the play without success. I keep watching.

1 Like

Hello

How many levels are there at most?

1) the head assembly (where we will run the macro)

2) a subset

3) Parts

Is that right?

No more intermediate levels?

 

1 Like

We can take inspiration from these two macros that I posted on Lynkoa:

http://www.lynkoa.com/tutos/3d/enregistrer-une-piece-chaque-configuration

http://www.lynkoa.com/tutos/solidworks/macro-pour-toutes-les-configurations-d-un-assemblage-pour-activer-l-option-%E2%80%9Cpromouv

 

Hello PL,

Thank you for taking an interest in my problem.

 

The architecture of my model is fine as you described it.

From my main assembly I want to change the config of a component that is in a subset of my main assembly.

I'm a beginner in VBA, I don't really understand the codes you put on your tutorials :s

 

Thank you in advance for your help.

Kind regards.

PL, have you ever used this type of code to choose a configuration?

Kind regards.

Hello

I may have a clue: have you tried traversing the assembly with the traverse component function as shown in the macro below:

http://help.solidworks.com/2012/english/api/sldworksap/traverse_assembly_at_component_level_example_vb.htm

to activate the desired component and then modify its configuration with the commands listed above?

Hello Glaffont,

I don't understand everything about this code...

Kind regards.

Here is the right link:

http://help.solidworks.com/2012/english/api/sldworksapi/traverse_assembly_at_component_level_example_vb.htm

1 Like

Being a beginner in programming, I admit that I don't understand much about this code :s

 

I have made a little progress:

The code below selects a component at level -2 and modifies its configuration.

BUT there is a bug..... The configuration it  displays is not the one I ask it to ??!?

See what it looks like at home with the right paths and tell me. (it's also a mess in the declarations, not taken the time to clean up....)

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

    Dim swApp As SldWorks.SldWorks
    Dim swAssy As SldWorks.assemblyDoc
    Sun swSelMgr As SldWorks.selectionMgr
    Dim swModel As SldWorks.ModelDoc
    Dim swComp As SldWorks.Component
    Dim ConfigMgr As ConfigurationManager
    Dim instance As ISelectionMgr
    Dim Index As Integer
    Dim Mark As Integer
    Dim boolstatus As Boolean
    Dim modelDoc2 As SldWorks.modelDoc2
Dim assemblyDoc As SldWorks.assemblyDoc
Dim featureMgr As SldWorks.FeatureManager
Dim modelDocExt As SldWorks.ModelDocExtension
Sun selectionMgr As SldWorks.selectionMgr
Dim feature As SldWorks.feature
Dim selObj As Object
Dim feat As SldWorks.feature
Dim folderFeat As SldWorks.feature
Dim errors As Long
Dim warnings As Long
Dim status As Long
Dim count As Long
Dim Value As SldWorks.Component2
Dim componentsToMove() As Object
Sun i As Long
Dim retVal As Boolean
    Dim RefCfg As String

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
    Set ConfigMgr = swModel.ConfigurationManager
    Set modelDoc2 = swApp.ActiveDoc
    Set assemblyDoc = modelDoc2
    
    'Select component
    Set modelDocExt = modelDoc2.Extension
    Set selectionMgr = modelDoc2.SelectionManager
    status = modelDocExt.SelectByID2("testEUT15B0178'015 S-E SUPPORT CYLINDER-1@Assemblage1/testO-RING STD-2@testEUT15B0178'015 S-E SUPPORT CYLINDER", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    Set selObj = selectionMgr.GetSelectedObject6(1, -1)
    Set Value = selectionMgr.GetSelectedObjectsComponent4(1, -1)

    ' Show Config1 and make it the active configuration
    Value.ReferencedConfiguration = "O-RING Ø24xØ1.5"

    ' For changes to take effect
    swModel.EditRebuild3

End Sub

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

1 Like

On a misunderstanding it can work: the configuration it displays is that of the component when you open it alone.

 

How should I fill in the line of code with the names of my own files?

Here are my file names

 

Main Assembly: CleanSeal COMPLETE

                    Sub-assembly: CleanSeal WELDED

                                     Component within the subassembly : Cxxx-x      (Not of one of the "C250-0" confided

Thank you very much for your help

Kind regards.

1 Like

Very simple: you copy in the code you put in your question...... ;-)

Below is the code with my file names.

It's almost that but it doesn't call for the right configuration...

 

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc


Set swApp = _
Application.SldWorks

  Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
       
 
Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
    Set ConfigMgr = swModel.ConfigurationManager
    Set ModelDoc2 = swApp.ActiveDoc
    Set AssemblyDoc = ModelDoc2
    
    'Select component
    Set modelDocExt = ModelDoc2.Extension
    Set SelectionMgr = ModelDoc2.SelectionManager
    Status = modelDocExt.SelectByID2("CleanSeal COMPLETE-1@Assemblage1/Cxxx-x-2@CleanSeal COMPLETE", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    Set selObj = SelectionMgr.GetSelectedObject6(1, -1)
    Set Value = SelectionMgr.GetSelectedObjectsComponent4(1, -1)

    ' Show Config1 and make it the active configuration
    Value.ReferencedConfiguration = "Cxxx-x C250-0"

    ' For changes to take effect
    swModel.EditRebuild3


End Sub

 

 

1 Like

Open your Cxxx-x-2 and see what configuration it is in.

Put it in the "C250-0" configuration and try again.

Let me clarify: it's DIY! We will try to find a solution in VBA.

Edit: There is an error in the name of the configuration you are calling, you should only put "C250-0" and not ""Cxxx-x C250-0"

1 Like

it works you made a happy  one :-)  !!

Compared to the very 1st code you gave me, what else does this one do?

When you say "it's DIY! We'll try to find a solution in VBA." what do you mean by that?

In any case, thank you very much for your help.

 

 

Can you check one thing: open your Cxxx-x-1 part, change its configuration and restart the macro.

Does it activate the configuration you want?