Creating a new Part Document

Top  Previous  Next

Here's a function for doing it:

 

HRESULT CreateNewPartDoc(CComPtr<PartDocument>& pPartDoc,

                        const wchar_t* const pszPartName)

/*

Given a name a new part document is created in Inventor with that name.

*/

{

   CComPtr<Application> pInventorApp = theApp.GetInvAppPtr() ;

 

       CComVariant pSubDocType;

       CComBSTR sTemplate;

       HRESULT hr = pInventorApp->GetTemplateFile (kPartDocumentObject, // We are creating a PartDocument

                                               kMetricSystemOfMeasure, // mm

                                               kDefault_DraftingStandard,

                                               pSubDocType,

                                               &sTemplate);

 

       if (hr != S_OK) {

               return hr;

       }

 

   CComPtr<Document> pDoc;

   hr = pInventorApp->Documents->Add (kPartDocumentObject,

                                      sTemplate,

                                      VARIANT_TRUE,

                                      &pDoc);

   if (hr != S_OK) {

       return hr;

   }

 

   pPartDoc = CComQIPtr<PartDocument>(pDoc);

 

   if ((pszPartName != nullptr) && (wcslen (pszPartName) > 0)) {

       BSTR bstrName ;

       pDoc->get_DisplayName (&bstrName) ;

       TRACE (L"\nFull document name was %s\n",bstrName) ;

 

       pPartDoc->DisplayName = pszPartName ;

 

       pDoc->get_DisplayName (&bstrName) ;

       TRACE (L"\nFull document name now is %s\n",bstrName) ;

 

       ::SysFreeString (bstrName) ;

   }

 

   return hr;

}

 

 

Which can be called like this:

 

   CComPtr<PartDocument> pPartDocument ;

   HRESULT hRes = CreateNewPartDoc (pPartDocument,L"MAIN_TUBE") ;

   if (FAILED(hRes)) {

       return ReturnAndShowCOMError (hRes,L"CreateMainTube, CreateNewPart failed") ;

   }

 

 

 

 

 

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