AnsweredAssumed Answered

Changing configurations in nested assembly

Question asked by Jana Stahn on Dec 7, 2016
Latest reply on Aug 2, 2017 by Alexander Schaaf

I have a nested assembly:

nestedAssembly.png

 

All components have two configurations. I want to change all configurations via API.

 

For the root component I can use IModelDoc2.ShowConfiguration2(<name>).

For the first layer, i.e. part1 and assy1, it works with IComponent2.ReferencedConfiguration = <name>.

 

But IComponent2.ReferencedConfiguration does not work for the lower layers (i.e. part2, assy2, part3, assy3). You can try this yourself if you like.

Here is the code in C#:

 

        public void ToggleAllConfigsDoesntWork()

        {

            IModelDoc2 doc = _swApp.ActiveDoc;      // _swApp is the ISldWorks object

 

            string activeConf = doc.ConfigurationManager.ActiveConfiguration.Name;

            Debug.WriteLine("    active configuration: " + activeConf);

            string[] configNames = doc.GetConfigurationNames();

 

            string newConfig = null;

            foreach (string configName in configNames)   // find a non-active configuration of root...

                if (configName != activeConf)

                {

                    newConfig = configName;

                    break;

                }

 

            Debug.WriteLine("    target configuration: " + newConfig);

            doc.ShowConfiguration2(newConfig);   // ... and switch to that configuration.

            Debug.WriteLine("    new configuration: " + doc.ConfigurationManager.ActiveConfiguration.Name);

 

 

 

            object[] children = activeDoc.GetComponents(false);  // get all child components

            string[] newConfigNames = new string[children.Length];

 

            for (int i = 0; i < children.Length; i++)   // find non-active configurations for all children...

            {

                IComponent2 child = children[i] as IComponent2;

                IModelDoc2 childDoc = child.GetModelDoc2();

                string[] configNames = childDoc.GetConfigurationNames();

 

                foreach (string configName in configNames)

                    if (configName != child.ReferencedConfiguration)

                    {

                        newConfigNames[i] = configName;

                        break;

                    }

            }

 

            for (int i = 0; i < children.Length; i++)   // ...and switch to these configurations.

            {

                IComponent2 child = children[i] as IComponent2;

 

                Debug.WriteLine("component name: " + child.Name2);

                Debug.WriteLine("    active configuration: " + child.ReferencedConfiguration);

                Debug.WriteLine("    target configuration: " + newConfigNames[i]);

                child.ReferencedConfiguration = newConfigNames[i];

 

                Debug.WriteLine("    new configuration: " + child.ReferencedConfiguration);

            }

 

            bool success = doc.EditRebuild3();

        }

 

You can use the attached assembly for testing. In the debug output you can see that the active configurations are correctly determined, and the target configurations as well. But changing the configuration just doesn't have any effect!

 

Any hints on how I can make it work?

 

 

I already noticed that using the method IAssemblyDoc.CompConfigProperties4 DOES work for all levels (except the root one). However, this is a method where things need to be selected in order to call it, and I have performance issues (selection is slow!). I want to avoid selecting if possible.

Attachments

Outcomes