iProperties, read the value of a custom property

Top  Previous  Next

Here you go:

 

// Custom properties which are visible in the iProperties Custom tab have to be

// placed inside the "Inventor User Defined Properties" property set

CString GetCustomPropertyValue(CComPtr<Document>& pInventorDoc,

                              const CString& kcsPropName)

{

   CComPtr <PropertySets> pPropSets;

   pInventorDoc->get_PropertySets(&pPropSets);

 

   CComVariant varPropSetName(L"Inventor User Defined Properties");

   CComPtr<PropertySet> pPropSet;

   HRESULT hRes = pPropSets->get_Item(varPropSetName,&pPropSet);

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

       ShowCOMError(hRes, L"Could not find custom property set");

       return CString (L"");

   }

 

   CComPtr<Property> pProp;

   CComVariant varPropName(kcsPropName);

   hRes = pPropSet->get_Item(varPropName, &pProp);

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

       // A property of this name does not exist

       return CString (L"");

   }

 

   CComVariant varPropValue;

   pProp->get_Value(&varPropValue);

 

   CString csValue; // If the variant is not a string this will remain empty

   if (varPropValue.vt == VT_BSTR) {

       csValue = CString(CComBSTR(varPropValue.bstrVal));

   }

 

   return csValue;

}

 

Here is an example of a call to the function:

 

   // Upcast from a PartDocument to a general Document

   CComPtr<Document> pDoc;

   pDoc = pTuboDoc;

   const CString kcsMatName = GetCustomPropertyValue(pDoc,"OWEN_MAT");

 

 

 

 

 

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