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!");
}
}

Leave a Reply