Autodesk Forge Viewer - Click Events on models created through SceneBuilder - autodesk-forge

I am working on adding geometry to the viewer using the new SceneBuilder extension (using this guide). I have been able to show the geometry in the viewer and also been able to add TextGeometry to the same dbId as some of the models.
The viewer is allowing me to click and isolate the geometry, but when I use viewer.getSelection(), it returns an empty array. I even added a click event listener to the Geometry itself, but no luck there as well.
Would any one know of a way to add a listener to this newly created fragment or geometry.

Did you set dbid to the mesh appended to the scene? Otherwise the selector wouldn't be able to pick up their dbids as it would for the document(original model)'s bubble:
sceneBuilder.addNewModel({conserveMemory: false,
modelNameOverride: 'My Model Name233'})
.then(modelBuilder => {
//...
mesh = new THREE.Mesh(geom, phongMaterial);
mesh.dbId=23333;
modelBuilder.addMesh(mesh);
})
Then you would be able to call the selector on the custom model to get the current selection - since we are getting selection status from the custom model as opposed to the one loaded by Viewer (exposed via viewer.model):
modelBuilder.model.selector.getSelection() //[23333]
EDIT
Forgot to mention you may also subscribe to the AGGREGATE_SELECTION_CHANGED_EVEN since we are dealing with multiple models here:
NOP_VIEWER.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT,e=>{
//...
})

Related

Query properties using the forge-viewer library without rendering model

I'm using the forge-viewer library to display models. I'm wondering if it is possible to at the same time query properties from another model, without rendering the other model or altering the state of the original viewer instance?
Preferably by obtaining a new instance of Autodesk.Viewing.Model, in order to use methods like model.getProperties(...).
Loading with the original viewer instance causes the other model to display in the browser
const document: Autodesk.Viewing.Document = await myLoadDocumentFunction("urn:another-model-urn");
const defaultModel = document.getRoot().getDefaultGeometry();
const model = await viewer.loadDocumentNode(document, defaultModel);
As the documentation states, the options input variable for loadDocumentNode() is passed on to loadModel() so you can check the documentation of that function to see what options are available.
One of them is loadAsHidden which seems to do exactly what you need:
FYI: it should also be possible to create another Viewer instance, make it invisible (e.g. placing it off the screen) and load extra models there: Multiple instances of Autodesk Forge Viewer

Access viewer methods from AggregatedView for more than one model in Autodesk Forge Viewer?

I am loading two different models via AggregatedView.
Although I have access to view.viewer, executing
var view = new Autodesk.Viewing.AggregatedView();
//...
view.viewer.isolate([0]);
only affects one of the two models.
Is there any way I can call viewer methods such as isolate(), show(), hide(), etc. on both models?
Note that many of the Viewer3D methods such as Viewer3D#isolate accept an additional parameter that you can use to specify the model.
And if the method you're interested in doesn't accept the model as one of its parameters, you can often find the same method directly on the Model class, for example, Model#getProperties.

Autodesk Forge: I created an app to translate CAD files, but cannot see properties for an assembly

So I created a nodejs app to translate CAD files and to display them in the viewer. For .rvt files and individual Inventor components I can see the properties by going to Model Browser and clicking on each component, however, I cannot see properties for an entire Inventor assembly. How can I go about seeing assembly properties?
I am using this sample: https://github.com/Autodesk-Forge/viewer-walkthrough-online.viewer
Only change I made was set compressedUrn to true and added a rootFileName to allow Inventor assemblies to be translated.
There is an option called modelBrowserExcludeRoot that you can set to false when creating the Viewer. In that case, the root object of the model will be visible in the Model browser and when selecting it you'll see the properties of the assembly in the Properties palette
e.g.
Autodesk.Viewing.Initializer(options, () => {
viewer = new Autodesk.Viewing.GuiViewer3D(
document.getElementById('forgeViewer'),
{
modelBrowserExcludeRoot: false
}
);
viewer.start();
var documentId = 'bubble.json';
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
});
Also have written a blog post about it just now:
Root object not visible in Model browser

Autodesk Forge Viewer: Selection-event not working anymore after adding a custom model

I am working on the example from Custom models in Forge Viewer blog entry from Petr Broz.
The problem I am facing at the moment is, that the Autodesk.Viewing.SELECTION_CHANGED_EVENT event is not raised any more after calling the sceneBuilder.addNewModel method.
To test the issue i've modified the Codepen-example:
Add a new function:
function onSelection(event) {
event.dbIdArray.forEach(selection => {
console.log(`selected ${selection}`)
});
}
...and register the function as event listener (as first step in the setup):
[...]
forgist.setup(document.getElementById('viewer'), 2).then(viewer => {
viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelection)
[...]
As long as I don't click the "Add geometry" button, the selection is shown in the console, but afterwards nothing is printed.
edit: the viewer.getSelection() method still returns the correct id of the selected objects
In case of creating a multi model environment, you would have to use
AGGREGATE_SELECTION_CHANGED_EVENT to be notified about selections

Active viewer model for miltimodel mode

Then several models are loaded in viewer, only one of them is "active". I mean that Object tree is shown for this model and select(),isolate() etc methods are related to it. I try to use following method to set "active" model:
function setActiveModel(model) {
var instanceTree = model.getData().instanceTree;
viewer.modelstructure.setModel(instanceTree);
}
But object tree doesn't change.
Questions are:
That is correct way to change viewer "active" model?
How can I get current "active" model in viewer?
Here is a patched version that supports multi-model switching. It is a drop-in replacement, simply include the file after the viewer3D.js script:
MultiModelStructurePanel.js
Multi-models will be supported soon in a future version of the API.