Accessing the List of Selected Components via the C++ API

 
 
 
// C++ API -- iterating over the selected components
Application app;

// Make sure something is selected
Selection sel = app.GetSelection();
LONG lCount = sel.GetCount();
app.LogMessage(L"Found " + CString(lCount) + L" selected items.");
if ( lCount < LONG(1) )
{
	app.LogMessage( L"No components are selected" );
	return CStatus::OK;
}
for ( LONG i=0; i<lCount; i++ )
{
	if ( sel.GetItem(i).GetClassIDName() == L"X3DObject" )
	{
		X3DObject x3dobj(sel.GetItem(i));
		app.LogMessage(L"\titem[" + CString(i) + L"]: " + x3dobj.GetName() );
	}
	else
	{
		app.LogMessage(L"\titem[" + CString(i) + L"]: " + sel.GetItem(i).GetClassIDName() );
	}
}

// The collection of selected components exists in the first member
CComAPIHandler comCollItem( sel.GetItem(0) );
CComAPIHandler comSubComp( comCollItem.GetProperty(L"SubComponent") );
CValue subCompType = comSubComp.GetProperty( L"Type" );
app.LogMessage( L"Type of subcomponent: " + CString(subCompType) );

// Grab the component collection
CComAPIHandler comCompColl( comSubComp.GetProperty(L"ComponentCollection") );

// Now we can iterate over the component collection's members
LONG lCollCount( comCompColl.GetProperty(L"Count") );
app.LogMessage(L"Found " + CString(lCollCount) + L" selected components.");
for ( LONG j=0; j<lCollCount; j++ )
{
	CValue rtn;	// output from the Item property
	CValueArray idx; idx.Add(j); // set the index to use
	comCompColl.Invoke( L"Item", CComAPIHandler::PropertyGet, rtn, idx );

	// From the CRef class, we can convert it to the actual class
	CRef itm(rtn);
	if ( itm.GetClassID() == siVertexID )
	{
		Vertex vtx = Vertex(itm);
		app.LogMessage( L"Found a vertex at index " + CString(vtx.GetIndex()) );
	}
	if ( itm.GetClassID() == siSampleID )
	{
		Sample smp = Sample(itm);
		app.LogMessage( L"Found a sample at index " + CString(smp.GetIndex()) );
	}
	if ( itm.GetClassID() == siEdgeID )
	{
		Edge edg = Edge(itm);
		app.LogMessage( L"Found a edge at index " + CString(edg.GetIndex()) );
	}
	if ( itm.GetClassID() == siControlPointID )
	{
		ControlPoint ctrl = ControlPoint(itm);
		app.LogMessage( L"Found a control point at index " + CString(ctrl.GetIndex()) );
	}
	// etc.
}