Create an assembly and add a part to it

Top  Previous  Next

Here is a function for creating a new assembly:

 

 

// Create a new assembly and return the document and assembly component definition.

// AssemblyComponentDefinition is a list of parts and constraints.

bool CreateNewAssembly (CComQIPtr<AssemblyDocument>& pAssemblyDoc,

                       CComPtr<AssemblyComponentDefinition>& pAssemblyCompDef,

                       const CString& kcsTemplateFile)

{

   CComPtr<Documents> pDocuments=nullptr;

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

   if (FAILED(hRes)) {

       ShowCOMError(ekErrMsg,hRes,L"CreateNewAssembly, could not get documents");

       return false ;

   }

 

   CComBSTR strTemplateFilename(kcsTemplateFile);

 

   // create a new assembly document from a standard template

   CComPtr<Document> pDocument=nullptr;

   hRes = pDocuments->Add(kAssemblyDocumentObject, strTemplateFilename, VARIANT_TRUE, &pDocument);

   if (FAILED(hRes)) {

       gLogger.Printf(ekLogMsg, L"Errore in CreateNewAssemply template = <%s>", kcsTemplateFile.GetString());

       ShowCOMError(ekErrMsg,hRes,L"CreateNewAssembly, Could not make new assembly document");

       return false ;

   }

 

   // Try to cast to an assembly document, this should not ever fail...

   pAssemblyDoc = pDocument ;

 

   // Get hold of the assembly component definition too...

   pAssemblyCompDef = nullptr ;

   hRes = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);

   if (FAILED(hRes)) {

       ShowCOMError(ekErrMsg,hRes,L"CreateNewAssembly, Could not get hold of assembly component definition");

       return false ;

   }

 

   return true ;

}

 

And here is how the above function is used:

 

   CComQIPtr<AssemblyDocument> pAssemblyDoc ;

   CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

   const bool kbAsmOk = CreateNewAssembly (pAssemblyDoc,pAssemblyCompDef,theApp.GetLuveIAMTemplate()) ;

   if (!kbAsmOk) {

       theApp.PrintfLedMessage (RGB(255,0,0),CCT_DLG,L"Errore durante creazione IAM...") ;

       return false;

   }

 

Here is the code to do this and add a part, with no error checking:

 

static void CreateAssembly (const CComPtr<Application>& pInvApp) 

{

    // Get the list of documents currently in Inventor...

    CComPtr<Documents> pDocuments;

    pInvApp->get_Documents (&pDocuments) ;

 

 

    // Get assembly document template, what is used as a default for .iam files

    // To do that you need to get the file manager

    CComPtr<FileManager> pFileManager;

    HRESULT hr = pInvApp->get_FileManager(&pFileManager);

 

    // So get hold of the user's default template file...

    CComBSTR strTemplateFilename; 

    hr = pFileManager->GetTemplateFile(kAssemblyDocumentObject, kDefaultSystemOfMeasure,

                                       kDefault_DraftingStandard,

                                       CComVariant(),

                                       &strTemplateFilename);

 

    // Create a new assembly document from a standard template

    CComPtr<Document> pDocument; // This is a return value from the following call

    hr = pDocuments->Add(kAssemblyDocumentObject, strTemplateFilename, VARIANT_TRUE, &pDocument);

 

    // New need some transient geometry to add in a part...

    CComPtr<TransientGeometry> pTransGeom;

    hr = pInvApp->get_TransientGeometry(&pTransGeom);

 

    // Create a matrix

    CComPtr<Matrix> pMatrix;

    hr = pTransGeom->CreateMatrix(&pMatrix);

 

    // This part must exist, because we are going to add an occurrence (instance) of it

    // to the assemply document

    CComBSTR csFileName = L"c:\\temp\\cylinder.ipt" ;

 

    // Convert pDocument (returned by the call to Add previously) into an assembly document

    CComQIPtr<AssemblyDocument> pAssemblyDoc = pDocument ;

 

    // Get the assembly component definition

    CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

    hr = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);

 

    // Get the ComponentOccurrences collection for the assembly document

    CComPtr<ComponentOccurrences> pOccs;

    hr = pAssemblyCompDef->get_Occurrences(&pOccs);

 

    // Now we can add an occurrence (instance) of cylinder.ipt into the assembly... 

    CComPtr<ComponentOccurrence> pOcc1;

    hr = pOccs->Add(csFileName,pMatrix,&pOcc1) ;

}

 

It is long winded but clear.

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