View Model's Objects - autodesk-forge

We have a solution that upload and view models using model-derivative APIs. We were able to upload and view models successfully, but we want to view specific objects on the model (last group of children in the tree).
We need to know how to view these objects and if there is a way, we can get all the children dbIds from the parent dbId. We are using viewer version 7.

The way of getting leaf nodes hasn't been changed in v7.
ref: https://stackoverflow.com/a/45874364/7745569
let model = viewer.getAllModels()[0];
let leafNodeDbIds = await getLeafNodes( model, [123] )

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

forge viewer get element dbId for multiple models same time NOP_VIEWER.getSelection(); []

I am trying to retrieve dbId from multibale model from a viewer, I am using getSelection method
also the getSelection it work with NOP_VIEWER i am wondering if there something like NOP_VIEWER.getAllModels()[needed model].getSelection()
the result is only from one model, I am wondering if there is a way to get the dbid from all models at the same time
thanks
When working with multiple models, you'll want to use the "aggregate" versions of methods, for example, getAggregateSelection. For more details on this, please refer to https://forge.autodesk.com/blog/multi-model-refresher.

Autodesk Forge Viewer - Click Events on models created through SceneBuilder

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=>{
//...
})

What is the best way to persist a reference to an item in forge viewer?

Currently, we are importing into forge viewer some revit models.
In the viewer, we want to be able to store into external database some info attached to an element of the model we see in the viewer (for example a door).
We have 3 ways to identify an item:
dbid (e.g 2214)
guid/externalId (e.g. a6aa132d-ccd7-408f-b2f9-ed67350c8c3a-0003b64a)
Revit ID in bracket beside the name (e.g. Roof [243274])
I would need to be able to reference an item on the model in external database, even if the revit model gets updated and reconverted in the middle.
It sounds dbid can change if we convert a new version, so not the good candidate. (https://forums.autodesk.com/t5/view-and-data-api-read-only/are-the-dbids-in-an-objecttree-of-a-revit-model-fixed/m-p/5517214#M757)
Guid seems ok, but we cannot get direct guid to dbid mapping in the viewer, we need 1000s costly web service call (https://forums.autodesk.com/t5/view-and-data-api-read-only/how-to-get-object-s-dbid-from-its-guid/m-p/5226891#M192)
Revit ID sounds just informative
1-Is Guid, the thing we should use to reference items in revit file (even between updates) ?
2-If this is Guid, how can we directly have a mapping from guid to dbid (since all in viewer is handled by dbid) ?
For now I see the solution above
Maybe playing with model-derivative :urn/metadata/:guid/properties to fetch all guid
Maybe there is already this guid->dbid mapping somewhere in the viewer and I miss it (behind getBulkProperties, I saw a getPropertyDb, that could possibly have it)
Thank you
First, to summarize:
Revit ID is a sequential numbering used on Revit desktop, it may be reused and it's not uniqu
Revit GUID is unique (as any GUID) and maintained between versions and exposed on both Viewer (JavaScript library) and Model Derivative GET Properties endpoint as external id
dbId just index used on the model to access geometry, no guarantee will be the same between versions/translations of the model.
Now, the only true identifier you can use to track the same element between versions is the external id (from Revit GUID).
If you want a server-side mapping, use Model Derivative GET Properties on all model views. On the client-side, I would suggest first enumerateLeafNodes and then call getBulkProperties on these nodes to get the external id.

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.