Open an assembly programatically |
Top Previous Next |
Here's a function to do it:
bool OpenAssembly (CComPtr<AssemblyComponentDefinition>& pAsmCompDef, // Output CComPtr<AssemblyDocument>& pAsmDoc, // Output const CString& kcsFullAsmFileName, // Input const bool kbOpenVisible/*=true*/) // Input { CComBSTR strAssemblyFilename (kcsFullAsmFileName) ;
// Get the list of documents, we use this to oprn another CComPtr<Documents> pDocuments; HRESULT hRes = theApp.GetInvAppPtr()->get_Documents (&pDocuments) ; if (FAILED(hRes)) { ShowCOMError(hRes, L"get_Documents called from OpenAssembly"); return false; }
VARIANT_BOOL vbOpenVisible; if (kbOpenVisible) { vbOpenVisible = VARIANT_TRUE;
} else { vbOpenVisible = VARIANT_FALSE; }
CComPtr<Document> pDoc = nullptr ; hRes = pDocuments->Open (strAssemblyFilename, // what to open vbOpenVisible, &pDoc) ; if (FAILED(hRes)) { ShowCOMError (hRes,L"OA, could not open assembly, pDocuments->Open (%s)",kcsFullAsmFileName); return false ; }
// Try to cast from a general doc to an assembly pAsmDoc = pDoc; if (pAsmDoc == nullptr) { ShowCOMError (hRes,L"OA, failed to cast to AssemblyDocument"); return false ; }
hRes = pAsmDoc->get_ComponentDefinition(&pAsmCompDef); if (FAILED(hRes)) { ShowCOMError (hRes,L"OA, could not open assembly %s",kcsFullAsmFileName); return false ; }
return true; }
Note the kbOpenVisible. Another example of invisible opening:
CComPtr<Document> pDoc ; hRes = pDocuments->Open (strAssemblyFilename, // what to open VARIANT_FALSE, // open it invisible &pDoc) ; // result
Note that if you just want to interrogate the data of a part or assembly opening it invisibly will be faster.
If you don't like that function...:
CComBSTR strAssemblyFilename ; strAssemblyFilename = L"C:\\George\\Best.iam" ;
CComPtr<Document> pDoc ; hRes = pDocuments->Open (strAssemblyFilename,VARIANT_TRUE,&pDoc) ; if (FAILED(hRes) || (pDoc == nullptr)) { ShowCOMError (hRes,L"OpenAssembly, Open failed "); return ; }
// Try to cast from a general doc to an assembly CComQIPtr<AssemblyDocument> pAsmDoc (pDoc); if (pAsmDoc == nullptr) { ShowCOMError (hRes,L"Failed to cast to AssemblyDocument"); return ; }
// Now you can use pAsmDoc, the assembly document // ...
|
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)