JGeerWM
JGeerWM

Reputation: 870

Forge Viewer Select in a multi-model context

We have extensions that currently leverage viewer.select() with a list of dbIds from the model.

Our customers would like to see secondary models in the same viewer, and we’re giving them the ability to load reference models after the first model has been loaded.

We’re running into a problem with multiple models, however, where the viewer is selecting from one of the models other than the first model loaded when we call viewer.select().

It seems like we may want to stop using viewer.select() but instead start using model.selector.select() after keeping a reference to the first model loaded. This would mean changing quite a bit of code.

Is there a way to set the context of viewer.select() so that it always uses the first model we load?

Upvotes: 0

Views: 1673

Answers (1)

Eason Kang
Eason Kang

Reputation: 7070

Before Forge Viewer v3.3, Viewer3D#select( dbIds, selectionType) didn't be exposed for the multi-model use case unfortunately. The 2nd argument of Viewer3D#select has been changed to Viewer3D#select( dbIds, model ). So, the below code snippets will changed to:

var scene = viewer.impl.modelQueue();
var models = scene.getModels();

var targetIndex = ...;
var targetModel = models[targetIndex];
var selectionType = ...;

// Method 1:
viewer.impl.selector.setSelection( dbIds, targetModel, selectionType );

// Method 2:
model.selector.select( dbIds, selectionType );

// Method 3: (After Forge Viewer v4)
viewer.select( dbIds, targetModel );

// Method 4: (After Forge Viewer v4)
var selections = [
    {
       model: targetModel,
       ids: dbIds
    }
];
viewer.impl.selector.setAggregateSelection( selections );

==== Update End ====

Unfortunately, Viewer3D#select didn't be exposed for the multi-model use case. However, there are few ways to select items via the API in multi-model environment:

var scene = viewer.impl.modelQueue();
var models = scene.getModels();

var targetIndex = ...;
var targetModel = models[targetIndex];
var selectionType = ...;

// Method 1:
viewer.impl.selector.setSelection( dbIds, targetModel, selectionType );

// Method 2:
model.selector.select( dbIds, selectionType );

// Method 3: (After Forge Viewer v4)
var selections = [
    {
       model: targetModel,
       ids: dbIds
    }
];
viewer.impl.selector.setAggregateSelection( selections );

Or, you can write your own Viewer class which extends Autodesk.Viewing.Viewer3D or Autodesk.Viewing.Private.GuiViewer3D to private a select function that supports passing model argument.

Upvotes: 4

Related Questions