Creating a Doc and an Assembly

Top  Previous  Next

HRESULT CreateNewAssembly (CComQIPtr<AssemblyDocument>& pAssemblyDoc,

                          CComPtr<AssemblyComponentDefinition>& pAssemblyCompDef,

                          const CString& kcsTemplateFile)

/*

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

AssemblyComponentDefinition is a list of parts and constraints.

*/

{

   CComPtr<Documents> pDocuments=nullptr;

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

   if (FAILED(hRes)) {

       return ReturnAndShowCOMError (hRes,L"CreateNewAssembly, could not get documents");

   }

 

   CComBSTR strTemplateFilename(kcsTemplateFile);

 

   // create a new assembly document using the template passed to us

   CComPtr<Document> pDocument=nullptr;

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

   if (FAILED(hRes)) {

       return ReturnAndShowCOMError (hRes,L"CreateNewAssembly, Could not make new assembly document");

   }

 

   // 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)) {

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

   }

 

   //theApp.PrintfLedMessage (RGB(255,255,155),CCT_DLG,L"Assieme OK...") ;

 

   return S_OK ;

}

 

Here is another example of how to do it in C++

 

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

{

    CComPtr<Documents> pDocuments;

    pInvApp->get_Documents (&pDocuments) ;

 

    CComBSTR strTemplateFilename; 

    // Get assembly document template

    CComPtr<FileManager> pFileManager;

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

    if (FAILED(hr)) {

        TRACE (_"Could not get file manager\n");

        return ;

    }

 

    hr = pFileManager->GetTemplateFile(kAssemblyDocumentObject, kDefaultSystemOfMeasure,

                                       kDefault_DraftingStandard,

                                       CComVariant(),

                                       &strTemplateFilename);

    if (FAILED(hr)) {

        TRACE ("Could not get template file\n");

        return ;

    }

 

    TRACE ("Template file is:\n%s\n",strTemplateFilename);

 

    // create a new assembly document from a standard template

    CComPtr<Document> pDocument;

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

    if (FAILED(hr)) {

        TRACE ("Could not create new assembly document\n");

        return ;

    }

 

    CComPtr<TransientGeometry> pTransGeom;

    hr = pInvApp->get_TransientGeometry(&pTransGeom);

    if (FAILED(hr)) {

        TRACE ("Could not get transient geometry\n");

        return ;

    }

 

    CComPtr<Matrix> pMatrix;

    hr = pTransGeom->CreateMatrix(&pMatrix);

    if (FAILED(hr)) {

        TRACE ("Could not create matrix\n");

        return ;

    }

 

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

 

    // Try to cast to the document to an assembly document

    CComQIPtr<AssemblyDocument> pAssemblyDoc = pDocument ;

    if (!pAssemblyDoc) {

        TRACE ("Could not get assembly doc\n");

        return ;

    }

 

    // Get assembly component definition

    CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

    hr = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);

    if (FAILED(hr)) {

        TRACE ("Could not get assembly component definition\n");

        return ;

    }

 

    // Get the ComponentOccurrences collection for the assembly document

    CComPtr<ComponentOccurrences> pOccs;

    hr = pAssemblyCompDef->get_Occurrences(&pOccs);

    if (FAILED(hr)) {

        TRACE ("Could not get assembly occurrences\n");

        return ;

    }

 

    CComPtr<ComponentOccurrence> pOcc1;

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

    if (FAILED(hr)) {

        TRACE ("Could not add assembly occurrence 1\n");

        return ;

    }

 

    CComPtr<Vector> pVector;

    hr = pTransGeom->CreateVector(20,2,2,&pVector);

    if (FAILED(hr)) {

        TRACE ("Could not create vector\n");

        return ;

    }

    pMatrix->SetTranslation (pVector,VARIANT_TRUE) ;

 

    CComPtr<ComponentOccurrence> pOcc2;

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

    if (FAILED(hr)) {

        TRACE ("Could not add assembly occurrence 2\n");

        return ;

    }

 

    CComPtr<Face> pCylFace1 = GetCylinderSurface (pOcc1) ;

    CComPtr<Face> pCylFace2 = GetCylinderSurface (pOcc2) ;

 

    TRACE ("pCylFace1 = %p pCylFace2 = %p\n",pCylFace1,pCylFace2);

}

 

Of course if you know and want to use a specific template file you can simply do this:

 

strTemplateFilename = L"C:\\Users\\Public\\Documents\\Autodesk\\Inventor 2013\\Templates\\LuVe\\RanStd.iam" ;

 

without calling GetTemplateFile .

 

See also creating a drawing, DWG IDW.

 

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