Getting the value of a custom iProperty

Top  Previous  Next

Here is a function which gets a custom iProperty value, returning false if the value does not exist in the document.

 

So it can be used to test for existence as well as getting the actual value.

 

// Returns true if the custom iProperty exists, with the value in csRetValue Needs a DOC not a PART or ASSEMBLY

// Always returns a string in csRetValue

boolean DocHasCustomiProperty(CComPtr<Document>& pInventorDoc, const CString& kcsiPropName, CString& csRetValue)

{

   csRetValue = CString ("") ;

 

   CComPtr <PropertySets> pPropSets;

   pInventorDoc->get_PropertySets(&pPropSets);

 

   const long ikNumSets = pPropSets->Count;

 

   // There are several sets of properties, loop over the sets...

   for (long iSet = 1; iSet <= ikNumSets; iSet++) {

       CComPtr<PropertySet> pPropSet;

       pPropSets->get_Item(CComVariant(iSet), &pPropSet);

 

       CComBSTR bstrPropSetName;

       pPropSet->get_Name(&bstrPropSetName);

       CString csPropSetName(bstrPropSetName);

 

       if (csPropSetName.CompareNoCase(L"Inventor User Defined Properties") == 0) {

           // Loop over the properties of this property set...

           const long ikNumProps = pPropSet->Count;

 

           for (long iProp = 1; iProp <= ikNumProps; iProp++) {

               CComPtr<Property> pThisProp;

               pPropSet->get_Item(CComVariant(iProp), &pThisProp);

 

               _bstr_t bstrPropName = pThisProp->GetDisplayName();

 

               const CString kcsThisPropName(bstrPropName.GetBSTR());

 

               if (kcsThisPropName.CompareNoCase(kcsiPropName) == 0) {

                   // We've found the property we are interested in...

                   // ...convert it into a string

                   CComVariant varValue;

                   pThisProp->get_Value(&varValue);

 

                   // Add more VT_... values for more detail...

                   if (varValue.vt == VT_BSTR) {

                       csRetValue = CString(varValue.bstrVal);

 

                   } else if (varValue.vt == VT_I4) {

                       csRetValue.Format(L"%d", varValue.intVal);

 

                   } else if (varValue.vt == VT_R8) {

                       csRetValue.Format(L"%.3f", varValue.dblVal);

 

                   } else {

                       csRetValue.Format(L"(?vt=%d?)", varValue.vt);

                   }

 

                   return true;

               }

           }

       }

   }

 

   return false ;

}

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