Create a Flush Constraint between Two Faces Using C++

By Barbara Han

Issue
How to create a flush constraint between two faces using C++?

Solution
The following code creates a flush constraint for the first faces of the first two occurrences.
Please make sure that these faces are planar, not e.g. the side of a cylinder.

static HRESULT GetInventorInformation()
{
    HRESULT Result = NOERROR;

    TCHAR Str[_MAX_PATH];
   
    CLSID InvAppClsid;
    Result = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);
    if (FAILED(Result)) return Result;

    // Either create a new instance of the application or latch on to the currently active one.
    _tprintf_s (_T("Would you like to create a new Inventor application? (y/n) _: "));
    _tscanf_s (_T("%ls"), Str, _MAX_PATH);

    CComPtr<IUnknown> pInvAppUnk;
    if (toupper (Str[0]) == _T(‘Y’))
    {
        Result = CoCreateInstance (InvAppClsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IUnknown), (void **) &pInvAppUnk);
        if (FAILED (Result))
            _tprintf_s (_T("*** Failed to create a new Inventor application ***\n"));
    }
    else
    {
        Result = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);
        if (FAILED (Result))
            _tprintf_s (_T("*** Could not get hold of an active Inventor application ***\n"));
    }
    if (FAILED(Result)) return Result;

    CComPtr<Application> pInvApp;
    Result = pInvAppUnk->QueryInterface (__uuidof(Application), (void **) &pInvApp);
    if (FAILED(Result)) return Result;

    CComPtr<Document> pDoc;
    Result = pInvApp->get_ActiveDocument(&pDoc);
    if (FAILED(Result)) return Result;

    CComQIPtr<AssemblyDocument> pAsmDoc(pDoc);
    CComPtr<AssemblyComponentDefinition> pCD;
    Result = pAsmDoc->get_ComponentDefinition(&pCD);
    if (FAILED(Result)) return Result;

    CComPtr<AssemblyConstraints> pACs(pCD->Constraints);

    CComPtr<Face> pFace1(pCD->Occurrences->Item[1]->SurfaceBodies->Item[1]->Faces->Item[1]);
    CComPtr<Face> pFace2(pCD->Occurrences->Item[2]->SurfaceBodies->Item[1]->Faces->Item[1]);

    CComVariant varEmpty, varReal(0.0);

    // add the constraint
    CComPtr<FlushConstraint> pFC;   
    Result = pACs->AddFlushConstraint(pFace1, pFace2, varReal, varEmpty, varEmpty,&pFC);
    if (FAILED(Result)) return Result;
    return true;
}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading