Changing parameters with C#

Top  Previous  Next

Here is an example of changing model parameters of a part:

 

       private void ChangeCyl_Click(object sender, EventArgs e)

       {

           Inventor.PartDocument PartDoc = m_Inventor.ActiveDocument as Inventor.PartDocument;

           if (PartDoc == null)

           {

               MessageBox.Show("This is not a part");

               return;

           }

 

           ModelParameters ModParams = PartDoc.ComponentDefinition.Parameters.ModelParameters;

           foreach (ModelParameter ModParam in ModParams)

           {

               if (ModParam.Name == "Diametro")

               {

                   ModParam.Value = 111.0; // .Value is in cm .Expression would be in mm

               }

               else if (ModParam.Name == "Altezza")

               {

                   ModParam.Value = 55.0; // .Value is in cm .Expression would be in mm

               }

           }

 

           PartDoc.Update();

       }

 

Note also the comment about .Value and .Expression, as well as the call to update the part. See also get_Units.

 

I have this set of functions for changing parameters:

 

       public enum ParamType_e

       {

           ekModel,

           ekUser,

       };

 

 

       public static void SetAsmParam (Inventor.AssemblyComponentDefinition AsmDef,

                                       ParamType_e eParamType,

                                       string sParamName, double NewVal)

       {

           if (AsmDef == null)

           {

               MessageBox.Show("SetAsmParam null AsmDef  (" + sParamName + ")" + eParamType.ToString ());

               return;

           }

           if (!AsmCompDefHasParam (AsmDef,eParamType,sParamName))

           {

               MessageBox.Show("Assembly does not have parameter <" + sParamName + ">, " + eParamType.ToString());

               return;

           }

 

           // This will become a User or Model Parameter

           Parameter Param = null;

 

           if (eParamType == ParamType_e.ekModel)

           {

               ModelParameters ModelParams = AsmDef.Parameters.ModelParameters;

               ModelParameter ThisParam = ModelParams[sParamName];

               if (ThisParam == null)

               {

                   MessageBox.Show("Cannot find assembly model param: " + sParamName);

                   return;

               }

               Param = (Parameter)ThisParam;

           }

           else if (eParamType == ParamType_e.ekUser)

           {

               UserParameters ModelParams = AsmDef.Parameters.UserParameters;

               UserParameter ThisParam = ModelParams[sParamName];

               if (ThisParam == null)

               {

                   MessageBox.Show("Cannot find assembly user param: " + sParamName);

                   return;

               }

               Param = (Parameter)ThisParam;

           }

 

           if (Param != null)

           {

               Param.Value = NewVal;

               MessageBox.Show(Param.get_Units());

           }

       }

 

 

       public static void SetAsmUlParam(Inventor.AssemblyComponentDefinition AsmDef,

                                        ParamType_e eParamType,

                                        string sParamName, double NewVal)

       {

           SetAsmParam(AsmDef, eParamType, sParamName, NewVal);

       }

 

 

       public static void SetAsmMmParam(Inventor.AssemblyComponentDefinition AsmDef,

                                        ParamType_e eParamType,

                                        string sParamName, double mmUserParamNewVal)

       {

           // Note the division by 10 because of cm native uints in Inventor SI

           SetAsmParam(AsmDef, eParamType, sParamName, mmUserParamNewVal / 10.0);

       }

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)