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
Related
I have an application that adds many custom meshes to the Forge Viewer using the ModelBuilder extension. This application has worked for quite a while but ran into issues with version 7.71 of the viewer.
When graphics are updated in my application, I remove all existing meshes and then recreate them based on new data. With version 7.71 of the viewer, when I recreate the meshes I get many instances the following error in the dev tools console:
Uncaught TypeError: Cannot read properties of undefined (reading 'material')
at FragmentList.getMaterial (FragmentList.js:760:1)
at FragmentList.getMaterialId (FragmentList.js:754:1)
at RenderBatch.js:222:1
at Int32Array.sort (<anonymous>)
at RenderBatch.sortByMaterial (RenderBatch.js:220:1)
at RenderModel.applyVisibility (RenderModel.js:388:1)
at RenderModel.nextBatch (RenderModel.js:360:1)
at RenderScene.reset (RenderScene.js:387:1)
at Array.cmdBeginPhase (Viewer3DImpl.js:1125:1)
at RenderCommandSystem.executeCommandList (Viewer3DImpl.js:847:1)
After these errors, the new meshes are not created.
The release notes don't mention anything about any expected changes here. Looking for guidance from the Autodesk team.
Here is a repo that reproduces the issue:
https://github.com/bencameron/custom-mesh-refresh
I'm told it will take some time to implement the fix (ticket id LMV-6757). In the meantime you could stick with v7.70 or try these workarounds a colleague suggested:
1.) If all of the custom geometry is removed, like it is in the example app, it's probably simpler to remove the model entirely and start with a new one. Here is an (untested) snippet to do that:
// Instead of deleting all geometries individually, simply delete the model and create a new one
viewer.unloadModel(modelBuilder.model);
// drop the reference to the model builder instance, i.e. assign a new instance
modelBuilder = await sceneBuilder.addNewModel({});
// start adding the geometry again...
...
2.) If option #1 doesn't work, or if the user doesn't want to delete all geometries, they could also avoid the crash by running this snippet right after adding new geometries (i.e. every time they add a batch of geometries):
const scenes = modelBuilder.model.getIterator().getGeomScenes();
for (let i = 0; i < scenes.length; ++i) {
scenes[i].numAdded = 0;
}
I would recommend the first option over the second one if applicable.
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.
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=>{
//...
})
I would like to use a single viewer to load/unload models instead of tearing down the viewer and creating a new instance of viewer.
Reasoning: I've loaded multiple models and one of the models is too large and problematic that it slows down the rendering, I'm thinking if it's possible to just unload the problematic model instead of reloading all the models EXCEPT the problematic model.
If you just want to unload a specific model, the following code snippet might help.
const models = viewer.impl.modelQueue().getModels();
const model = models[2]; //!<< The model you want to unload
viewer.impl.unloadModel( model );
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.