v2.2
How to work with the selection mechanism

Overview

A model object can either be selected or not. The ISelection interface provides methods to query the IDs of selected objects and select sets of objects by their IDs.

How to select objects

For example, to select all walls in the scene:

C++

Renga::IApplicationPtr pApplication = Renga::CreateApplication();
if (pApplication == nullptr)
return;
Renga::IProjectPtr pProject = pApplication->GetProject();
if (pProject == nullptr)
return;
Renga::IModelPtr pModel = pProject->GetModel();
Renga::IModelObjectCollectionPtr pModelObjectCollection = pModel->GetObjects();
ATL::CComSafeArray<int> modelObjectIds = pModelObjectCollection->GetIds();
std::vector<int> wallObjectIds;
for (int i = 0; i < modelObjectIds.GetCount(); ++i)
{
int objectId = modelObjectIds.GetAt(i);
Renga::IModelObjectPtr pModelObject = pModelObjectCollection->GetById(objectId);
if (pModelObject == nullptr)
continue;
if (pModelObject->GetObjectType() != Renga::ObjectTypes::Wall)
continue;
wallObjectIds.push_back(objectId);
}
CComSafeArray<int> wallObjectIdsSafeArray(wallObjectIds.size());
for (int i = 0; i < wallObjectIds.size(); ++i)
wallObjectIdsSafeArray.SetAt(i, wallObjectIds[i]);
Renga::ISelectionPtr pSelection = pApplication->GetSelection();
pSelection->SetSelectedObjects(wallObjectIdsSafeArray);

C#

Renga.IApplication application = new Renga.Application();
Renga.IProject project = application.Project;
if (project == null)
return;
Renga.IModel model = project.Model;
Renga.IModelObjectCollection modelObjectCollection = model.GetObjects();
int[] modelObjectIds = (int[])modelObjectCollection.GetIds();
var wallObjectIds = new List<int>();
foreach (int objectId in modelObjectIds)
{
Renga.IModelObject modelObject = modelObjectCollection.GetById(objectId);
if (modelObject == null)
continue;
if (modelObject.ObjectType != Renga.ObjectTypes.Wall)
continue;
wallObjectIds.Add(objectId);
}
Renga.ISelection selection = application.Selection;
selection.SetSelectedObjects(wallObjectIds.ToArray());

How to handle selection changes

Event handling, in a way, differs between the SDK languages. C++ SDK provides the SelectionEventHandler helper class, which leverages the COM event subscription itself. C# SDK provides an alternative implementation, the SelectionEvents class, to facilitate the native .NET event syntax.

For example, to handle changes in selection and do something with selected objects when the selection changes:

C++

class ModelSelectionChangedHandler : public Renga::SelectionEventHandler
{
public:
ModelSelectionChangedHandler(Renga::ISelectionPtr pSelection);
void OnModelSelectionChanged() override;
private:
Renga::ISelectionPtr m_pSelection;
};
ModelSelectionChangedHandler::ModelSelectionChangedHandler(Renga::ISelectionPtr pSelection)
: Renga::SelectionEventHandler(pSelection),
m_pSelection(pSelection)
{
}
void ModelSelectionChangedHandler::OnModelSelectionChanged()
{
CComSafeArray<int> selectedObjects(m_pSelection->GetSelectedObjects());
DoSomething(selectedObjects);
}
...
std::unique_ptr<ModelSelectionChangedHandler> pModelSelectionChangedHandler;
Renga::IApplicationPtr pApplication = Renga::CreateApplication();
Renga::ISelectionPtr pSelection = pApplication->GetSelection();
pModelSelectionChangedHandler = std::make_unique<ModelSelectionChangedHandler>(pSelection);

C#

Renga.ISelection selection;
Renga.SelectionEventSource selectionEventSource;
public bool Initialize(string pluginFolder)
{
var application = new Renga.Application();
selection = application.Selection;
selectionEventSource = new Renga.SelectionEventSource(application.Selection);
selectionEventSource.ModelSelectionChanged += OnModelSelectionChanged;
return true;
}
public void Stop()
{
selectionEventSource.Dispose();
}
private void OnModelSelectionChanged(object sender, EventArgs args)
{
int[] selectedObjects = (int[])selection.GetSelectedObjects();
DoSomething(selectedObjects);
}

See also

Related samples

  • ModelSelection