2 Replies Latest reply on Sep 27, 2018 2:41 PM by Shuhei Kazuma

    How to get a count of Configurations in Part/Assembly?

    Shuhei Kazuma

      Dear experts,

       

      I would like to get the count of configurations given the Part or Assembly files. How can I do it? I use C# but any example is appreciated.

       

      Background:

      I want to write a code which modifies a global variable. But I need to use different methods depending on the number of configurations existing in the file.

       

      For example, if a part has multiple configurations, then I have to use

       

      swEqnMgr.SetEquationAndConfigurationOption(index, NewEquation, (int)swInConfigurationOpts_e.sw,null);

       

      However, for a part with a single configuration, then

       

      swEqnMgr.Equation[index] = NewEquation

       

       

      thank you in advance!

      SK

          • Re: How to get a count of Configurations in Part/Assembly?
            Shuhei Kazuma

            Thank you Anthony, that is what I was looking for. This seems to work well for now.

             

                    public static int FindEquationByName(ModelDoc2 swModel, string Name)

                    {

                        EquationMgr swEqnMgr = swModel.GetEquationMgr();

             

             

                        Regex pattern = new Regex(@"""(?<Name>\w+)""\s?=\s?(?<Equation>.*)");

             

             

                        for (int i=0; i<swEqnMgr.GetCount(); i++)

                        {

                            string eq = swEqnMgr.Equation[i];

                            Match match = pattern.Match(eq);

                            if (Name == match.Groups["Name"].Value)

                            {

                                return i;

                            }

                        }

                        return -1;

                    }

             

             

                    public static bool ChangeEquation(ModelDoc2 swModel, string Name, string Equation)

                    {

                        EquationMgr swEqnMgr = swModel.GetEquationMgr();

                        int ConfigCount = swModel.GetConfigurationCount();

             

             

                        int location = FindEquationByName(swModel, Name);

                        // if equation is found, change the value

                        if (location != -1)

                        {

                            string NewEquation = "\"" + Name + "\""+ " = " + Equation;

                            if (ConfigCount > 1)

                            {

                                Console.WriteLine("multiple config");

                                int result = swEqnMgr.SetEquationAndConfigurationOption(location, NewEquation, (int)swInConfigurationOpts_e.swAllConfiguration, null);

                                if (result == -1)

                                {

                                    return false;

                                }

                                else

                                {

                                    return true;

                                }

                            }else if(ConfigCount == 1)

                            {

                                swEqnMgr.Equation[location] = NewEquation;

                                return true;

                            }

                            else

                            {

                                return false;

                            }

                        }

             

             

                        return false;

                    }