Getting the current document of Inventor

Top  Previous  Next

It can be done like this:

 

CComPtr<Application> pApplication;

hr = pInvAppUnk->QueryInterface (__uuidof(Application), (void **) &m_pApplication);

if (FAILED(hr)) return hr;

 

CComPtr<Document> pDoc;

hr=pApplication->get_ActiveDocument(&pDoc); 

 

get_ActiveDocument will put NULL in pDoc if there is no document open. You have to check that.

 

You can find out what sort of document it is by looking at the type, for example:

 

if (pDoc->DocumentType==kDrawingDocumentObject)...

 

by looking at DocumentType. You can use this function to get a description of the document type

 

Valid enumerators are:

 

kAssemblyDocumentObject      12291 Assembly Document. 

kDesignElementDocumentObject 12294 Design Element Document. 

kDrawingDocumentObject       12292 Drawing Document. 

kForeignModelDocumentObject  12295 Foreign Model Document. 

kNoDocument                  12297 No Document. 

kPartDocumentObject          12290 Part Document. 

kPresentationDocumentObject  12293 Presentation Document. 

kSATFileDocumentObject       12296 SAT File Document. 

kUnknownDocumentObject       12289 Unknown Document. 

 


 

Apart from looking at the pDoc->DocumentType you can also try to cast the doc into the doc type you are looking for:

 

// Get the component definition for the document if its a part or assembly.

// Return an error for all other document types.

CComQIPtr<ComponentDefinition> pCompDef;

if (CComQIPtr<PartDocument> pPartDoc = pDoc) // try to cast

{

    TRACE("This is a part\n");

    CComPtr<PartComponentDefinition> pPartCompDef;

    Result = pPartDoc->get_ComponentDefinition(&pPartCompDef);

    ...and so on...

 

} else if (CComQIPtr<AssemblyDocument> pAssemblyDoc = pDoc) { // try to cast

    TRACE("This is an assembly\n");

    CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

    Result = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);

    ...and so on...

 

}

 


 

There is another way of getting the part document:

 

// convert generic document to partdocument

CComPtr<PartDocument> pPartDocument;

hr = pDocument->QueryInterface(DIID_PartDocument,(void**)&pPartDocument);

if (FAILED(hr))

    return ReturnAndShowCOMError (hr,L" get part document failed ") ; 

 

// Now you can use pPartDocument...

 

 

 

 

 

 

 

 

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