Model and User parameters programatically

Top  Previous  Next

Inside a PartComponentDefinition is a Parameters list which contains two other lists, the model parameters and the user parameters:

 

Model-User-Parameters

 

Here is an example of listing the ModelParameters programatically:

 

void ListModelParameters (CComPtr<PartComponentDefinition>& pPartCompDef)

{

   // Parameters contains two lists, Model and User...

   CComPtr<Parameters> pParameters ;

   pPartCompDef->get_Parameters (&pParameters) ;

 

   // Get the model parameters list...

   CComPtr<ModelParameters> pModelParameters ;

   pParameters->get_ModelParameters (&pModelParameters) ;

 

   // Print them all out with their names...

   const UINT ikNumModelParams = pModelParameters->Count ;

   for (UINT i = 1 ; i <= ikNumModelParams ; i++) {

       CComPtr<ModelParameter> pModelParam ;

       pModelParameters->get_Item (CComVariant(i),&pModelParam) ;

 

               CComBSTR bstrParamName;

       pModelParam->get_Name (&bstrParamName) ;

 

       TRACE (L"The name of model parameter %d is %s\n",i,bstrParamName) ;

   }

}

 

From the above example you can imagine how to list the UserParameters.

 

You can change the values of the parameters like this:

 

HRESULT ChangeDoubleModelParam (const double kNewVal,

                               const wchar_t* const pszModelParamName,

                               CComPtr<PartComponentDefinition>& pPartCompDef)

/*

Change the value of a double (i.e. floating point) parameter of a part.

Remember to understand internal units before calling this!

*/

{

   // Parameters contains two lists, Model and User...

   CComPtr<Parameters> pParameters ;

   pPartCompDef->get_Parameters (&pParameters) ;

 

   // Get the model parameters list...

   CComPtr<ModelParameters> pModelParameters ;

   pParameters->get_ModelParameters (&pModelParameters) ;

 

   CComPtr<ModelParameter> pModelParam ;

   HRESULT hRes = pModelParameters->get_Item (CComVariant (pszModelParamName),&pModelParam) ;

   if (FAILED(hRes) || (pModelParam == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"ChangeDoubleModelParam, get_Item failed") ; 

   }

 

   // Change the value of the parameter

   hRes = pModelParam->put_Value (CComVariant(kNewVal)) ;

   if (FAILED(hRes) || (pModelParam == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"CUnits hangeDoubleModelParam, put_Value failed") ; 

   }

 

   TRACE (L"New Value=%.3f\n",pModelParam->Get_Value()) ;

 

   return (S_OK) ;

}

 

 

You will probably not see an immediate change in your model when you call the above function, it needs a call to Update to do that:

 

   ChangeDoubleModelParam (999.0,L"d0",pPartCompDef) ;

   ChangeDoubleModelParam (100.0,L"d5",pPartCompDef) ;

   ChangeDoubleModelParam (  5.1,L"d9",pPartCompDef) ;

   pPartDocument->Update () ; // like regen in AutoCAD

 

Having a separate Update means you can change many parameters without having Inventor (wasting time) doing an Update after every change.

 

See also parameter types and Units and reading model and user parameters programatically.

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