Name of part in a component occurrence

Top  Previous  Next

Here is a function which will return the component occurrence (within an assembly) of a given name.

 

HRESULT GetComponentOccurrenceByName (CComPtr<ComponentOccurrence>& pCompOccRet,

                                     const wchar_t* const pszOccName,

                                     CComQIPtr<AssemblyDocument> pAssemblyDoc)

{

   // Init for safety

   pCompOccRet = nullptr ;

 

   // Get assembly component definition

   CComPtr<AssemblyComponentDefinition> pAssemblyCompDef = nullptr;

   HRESULT hRes = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);

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

       return ReturnAndShowCOMError(hRes,L"GetComponentOccurrenceByName, get_ComponentDefinition failed ");

   }

 

   // Get the ComponentOccurrences collection for the assembly document

   CComPtr<ComponentOccurrences> pOccs;

   hRes = pAssemblyCompDef->get_Occurrences(&pOccs);

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

       return ReturnAndShowCOMError (hRes,L"GetComponentOccurrenceByName, get_Occurrences failed ");

   }

 

   long lNoOccs;

   hRes = pOccs->get_Count(&lNoOccs);

   if (FAILED(hRes)) {

       ReturnAndShowCOMError (hRes,L"GetComponentOccurrenceByName, get_Count failed ");

   }

 

   TRACE (L"GetComponentOccurrenceByName, there are %d occurrences\n",lNoOccs);

 

       //iterate through the current level of occurrences

       for (long lOccCount=1; lOccCount <= lNoOccs; ++lOccCount) {

 

       CComPtr<ComponentOccurrence> pThisCompOcc = nullptr ;

       hRes = pOccs->get_Item(lOccCount, &pThisCompOcc);

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

           ShowCOMError(hRes,L"GetComponentOccurrenceByName, get_Item failed ");

           return hRes ;

       }

               

       CComPtr<ReferencedFileDescriptor> pRefFileDesc = nullptr ;

       pThisCompOcc->get_ReferencedFileDescriptor (&pRefFileDesc) ;

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

           ShowCOMError(hRes,L"GetComponentOccurrenceByName, get_ReferencedFileDescriptor failed ");

           return hRes ;

       }

 

       CComBSTR bstrDisplayName; // ::SysFreeString not required because CComBSTR, not BSTR

       hRes = pRefFileDesc->get_DisplayName (&bstrDisplayName) ;

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

           ShowCOMError(hRes,L"GetComponentOccurrenceByName, get_DisplayName failed ");

           return hRes ;

 

               } else {

                       TRACE (L"Component occurrence %02d <%s>\n",lOccCount,bstrDisplayName);

           if (_wcsicmp(bstrDisplayName,pszOccName)==0) {

               pCompOccRet = pThisCompOcc ;

           }

       }

 

 

       if (pCompOccRet != nullptr) {

           // We can get out

           return (S_OK) ;

       }

   }

 

   TRACE (L"Could not find an occurrence called <%s>\n",pszOccName) ;

 

   return (E_FAIL) ;

}

 

Basically you get the name of the occurrence's part using the occurrences ReferencedFileDescriptor .

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