C++ Example for AddCustomiPartMember method

By Philippe Leefsma

Q:

Is there any example code available that demonstrates using AddCustomiPartMember() using C++? I am trying to insert a custom iPart into an assembly programmatically but I am getting an error.

A:

Here is a C++ example that adds a custom iPart. The code below is found in TestDlg.cpp of the attached Visual Studio project. Please note that the paths for the iParts are hard coded and will need to be changed to match the file location on your system. Also the third parameter in AddCustomiPartMember specifies a part file. If this file already exists then this part will be placed in the assembly. (it may have different custom values then specified in the CustomInput parameter)

void CTestDlg::OnBnClickedButton1()

{

    CLSID             AppClsid;

    CComPtr<IUnknown> AppUnk = NULL;

 

    // get application

    if (FAILED(::CLSIDFromProgID(L"Inventor.Application", &AppClsid)))

        return;

 

    if (FAILED(::GetActiveObject(AppClsid, NULL, &AppUnk)))

        return;

 

    CComQIPtr<Application> App = AppUnk;

 

    AppUnk = NULL;

 

    // declare it here, so that it can be reached in catch

    CComPtr<Transaction> Txn1 = NULL;

 

    try

    {

        // get the active document

        CComPtr<Document> Doc = App->GetActiveDocument();

       

        // query the assembly doc object

        CComQIPtr<AssemblyDocument> AssDoc = Doc;

 

        // get the transaction manager from the application

        CComPtr<TransactionManager> TxnMgr = App->GetTransactionManager();

 

        // start transaction

        Txn1 = TxnMgr->StartTransaction(Doc, L"My Command");

 

        // get the assembly component definition

        CComPtr<AssemblyComponentDefinition> pCompDef;

        AssDoc->get_ComponentDefinition(&pCompDef);

       

        // get the occurrences in it

        CComPtr<ComponentOccurrences> compOccurrences;

        pCompDef->get_Occurrences(&compOccurrences);

 

        // create a placement matrix

        CComPtr<TransientGeometry> pTransGeom = App->GetTransientGeometry();

        CComPtr<Matrix> pNewTransform = pTransGeom->CreateMatrix();

 

        // create a safearray for the strings

        BSTR strBSTR[3] =

        {

                CString("10 cm").AllocSysString(),

                CString("20 cm").AllocSysString(),

                CString("30 cm").AllocSysString()

        };

 

        COleSafeArray oleSafeArray;

        oleSafeArray.CreateOneDim(VT_BSTR, 3, strBSTR);

 

        // create a custom ipart member

        compOccurrences->AddCustomiPartMember(

            "C:\\Temp\\CustomFactory.ipt",

            pNewTransform,

            "C:\\Temp\\CustomMember.ipt",

            CComVariant(1),

            oleSafeArray);

       

        // commit transaction

        Txn1->End();

        return;

    }

    catch (…)

    {

        if (Txn1 != NULL)

            Txn1->Abort();

       

        AfxMessageBox(L"Could not create iPart member!");

    }

}


Comments

2 responses to “C++ Example for AddCustomiPartMember method”

  1. Owen Ransen Avatar
    Owen Ransen

    C++ Example for AddCustomiPartMember method…
    Could you give an example for a console application which does not use MFC? I don’t have COleSafeArray available.

  2. Sorry, I don’t have another example ready without using COleSafeArray. You should be able to use that class from a console application that is MFC-enabled. Check the MSDN Help to see which header is required: http://msdn.microsoft.com/en-us/library/69dxx6wt%28v=vs.90%29.aspx
    You may want to try using CComSafeArray or SAFEARRAY classes: http://adndevblog.typepad.com/manufacturing/2012/07/using-safearrays-with-the-inventor-api.html

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading