Open an Inventor part programatically

Top  Previous  Next

This function opens an Inventor part, and returns the PartDocument and the PartComponentDefinition, both of which are usually useful to the caller.

 

HRESULT OpenPart (CComPtr<PartComponentDefinition>& pPartCompDef, // output

                 CComPtr<PartDocument>& pPartDoc,                // output

                 const CString& csFullPartFileName,              // input

                 CComPtr<Application>& pInvApp)                  // input

/*

Opens a part in a separate Inventor document

*/

{

   // Read in the correct file.

   CComPtr<Documents> pDocs = nullptr ;

   HRESULT hRes = pInvApp->get_Documents (&pDocs) ;

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

       return ReturnAndShowCOMError (hRes,L"OpenPart,get_Documents failed ");

   }

 

   CComPtr<Document> pDoc = nullptr ;

   hRes = pDocs->Open (CComBSTR (csFullPartFileName),VARIANT_TRUE,&pDoc) ;

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

       return ReturnAndShowCOMError (hRes,L"OpenPart, Open failed ");

   }

 

   pPartDoc = pDoc ;

   if (pPartDoc == nullptr) {

       return ReturnAndShowCOMError (hRes,L"OpenPart, failed to cast to PartDocument");

   }

 

   // Get assembly component definition

   pPartCompDef = nullptr ;

   hRes = pPartDoc->get_ComponentDefinition(&pPartCompDef) ;

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

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

   }

 

   return (S_OK) ;

}

 

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