Adding a flush constraint programatically.

Top  Previous  Next

A flush constraint means two planes are aligned on the same plane. So this means that you must get hold of the two planes to make them flush. Imagine two boxes, you could select a face from each box to become flush.

 

Inventor-Constraints-Programatically

 

Here is an example of adding a flush constraint with AddFlushConstraint. AddFlushConstraint requires WorkPlanes or planar objects like faces. The example below uses faces (planar objects).

 

When you call this function you'll need two parts (with one box in each part) in the Inventor assembly document for this to work.

 

static void DoFlushConstraint (CComPtr<Application>& pInvApp)

{

    TRACE (L"DoFlushConstraint starting\n");

 

    CComPtr<Document> pDoc;

    HRESULT hRes = pInvApp->get_ActiveDocument(&pDoc);

    if (FAILED(hRes) || (pDoc == nullptr)) {

        ShowCOMError(hRes,L"Get Active Document Failed"); 

        return ;

    }

 

    TRACE (L"A document is open, is it an assemply document?\n");

 

    CComQIPtr<AssemblyDocument> pAsmDoc(pDoc);

    CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

    hRes = pAsmDoc->get_ComponentDefinition(&pAssemblyCompDef);

    if (FAILED(hRes) || (pAssemblyCompDef == nullptr)) {

        TRACE (L"Maybe this is not an assembly document?\n");

        return ;

    }

 

    TRACE (L"Getting occurrences, each occurrese is like an instance of a part\n");

 

    CComPtr<ComponentOccurrences> pOccs;

    hRes = pAssemblyCompDef->get_Occurrences(&pOccs);

    long iNumOccs ; 

    hRes = pOccs->get_Count(&iNumOccs);

 

    if (iNumOccs != 2) {

        TRACE (L"This program needs exactly 2 occurrences in the assembly to work, but the doc has %d\n",iNumOccs);

        return ;

    }

 

    // Get the list of constraints of the assembly so you can add a new one

    CComPtr<AssemblyConstraints> pConstraintList;

    hRes = pAssemblyCompDef->get_Constraints(&pConstraintList) ;

    if (FAILED(hRes) || (pConstraintList== nullptr)) {

        TRACE (L"Could not get the constraints of the assembly\n");

        return ;

    }

 

    TRACE (L"The assembly has %d existing constraints\n",pConstraintList->Count) ;

 

    CComPtr<Face> pFace1,pFace2;

    pAssemblyCompDef->Occurrences->Item[1]->SurfaceBodies->Item[1]->Faces->get_Item (1,&pFace1) ;

    pAssemblyCompDef->Occurrences->Item[2]->SurfaceBodies->Item[1]->Faces->get_Item (1,&pFace2) ;

 

    if ((pFace1 == nullptr) || (pFace2 == nullptr)) {

        TRACE (L"Could not get two faces\n");

        return ;

    }

 

    CComVariant varEmpty, varReal(0.0);

 

    // add the constraint

    CComPtr<FlushConstraint> pFC;   

    hRes = pConstraintList->AddFlushConstraint(pFace1, pFace2, varReal, varEmpty, varEmpty,&pFC);

    if (FAILED(hRes)) {

        ShowCOMError (hRes,L"Could not add flush constraint\n");

        return ;

    }

 

    TRACE (L"Added constraint\n");

}

 

Other sorts of constraints are available too.

 

 

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