how to zoom asset in forge viewer - autodesk-forge

we have implemented the forge viewer in our web application. When clicking the asset in forge viewer, we need to make our entire page should be loaded.
any click event is available when object is selected in viewer?
please provide the function name or code, we need to hit any function when asset selecting.

you can use below even, you can write this event after you create instance of viewer
viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectionChanged);
function onSelectionChanged()
{
perform your operation;
}
for zooming the asset you can use
viewer.fitToView([arrayofdbid],viewer.model);
if you want to highlight selection, you can use,
viewer.select([array of dbid]);

Related

click event is not working in forge viewer

We have implemented the Autodesk Forge Viewer in our web application. We have displayed the Navisworks file in Forge Viewer.
On clicking the particular element/object in forge viewer, we need to get the Object ID of selected element.
We will use this Object ID for multiple purpose (like zooming, etc) in our application.
For this we have used the below selection changed event in our page but the below event is not working.
EventsTutorial.prototype.onSelectionEvent = function(event){
var currSelection = this.viewer.getSelection();
var domElem = document.getElementById('MySelectionValue');
domElem.innerText = currSelection.length;
};
Kindly help us to resolve this issue.
It seems you didn't register your function as a listener to the viewer. So I guess your function onSelectionEvent is never called.
You probably want to register a listener to either the SELECTION_CHANGED_EVENT(docs) or the AGGREGATE_SELECTION_CHANGED_EVENT(docs).
this.viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, (args) => {
// do something
});

Mapping Forge Viewer button to external buttons

I am hosting Forge Viewer as a component in my own GUI. I am wondering if I can map Forge Viewer buttons to external buttons in my own GUI.
An as example, instead of clicking on the model browser button in Forge Viewer, I want to be able to click on a button in my own GUI in order to see the tree view of the loaded model in forge viewer.
Such feature is already implemented in viewer.autodesk.com for model browser button, and I am wondering how I can do so.
Sure thing. These default controls are usually nested inside extensions that house the ui for that functionality. However you can get any extension by name after initaliazing the viewer using getExtension You now have a reference to the extension which you can control
Please try this is browser console:
let ext = null
let gotExtension = function(extension){
ext = extension
}
let openEvent = function(extension){
extension.activate();
}
let closeEvent = function(extension){
extension.deactivate();
}
NOP_VIEWER.getExtension('Autodesk.ModelStructure', gotExtension);
openEvent(ext);

Highlighting Text When a Part is Selected in the Forge Viewer

Currently, I have successfully been able to isolate and highlight parts or assemblies in the autodesk-forge viewer, with external buttons. When a button is pressed externally, it will automatically zoom into the selected part inside the viewer. It is explained more here. My next step now, is to have those external parts on the website, highlight when a part is clicked inside the viewer. How can I have a function be executed (a highlight text function) when the user selects a part on the viewer. I have tried the following code but it doesn't work:
if (viewer.isolate(dbId) == true){
highlightFunction()
}
Any help on how a function can be executed when a user selects a part on the viewer would be greatly appreciated.
Thanks!
If I understand you correctly, then all you need to do is listen to the selection event and call your function from there:
viewer.addEventListener(
Autodesk.Viewing.SELECTION_CHANGED_EVENT,
(event) => { // do what you need }
)
Here is a sample: Selection override
If you're working with multiple models in the Viewer then listen to the AGGREGATE_SELECTION_CHANGED_EVENT event instead. See Multi-model refresher

Forge viewer: Auto-start animation

We would like to trigger/auto-start an animation/simulation of a specific object (Revit ElementID) when the model loads in the Forge viewer. The goal would be to move a cube (Revit ElementID) back and forth on the floor in a repeating loop. Would this be possible using any relevant extension or code?
Yes it is possible but there is no built-in extension you can use out of the box. The animation logic has to be a custom implementation.
From a given ElementId/UniqueId you need to find the corresponding dbId, see that reply for a starting point on how to do that.
From a given dbId, you can obtain fragmentIds and use those to apply transforms to the components in the viewer. The animation logic has to be by your own application. You can refer to that article How to create animations in the viewer? or one of the several demos I wrote performing animations:
Kinematics - source
Physics - source
You can use the Autodesk.Viewing.GEOMETRY_LOADED_EVENT and Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT to trigger the animation logic, so you can make sure the model is fully loaded. See also that article: Asynchronous viewer events notification.
Hope that helps.

disable double click to zoom in autodesk viewer online

When selecting many elements in the Autodesk's online viewer, I double click accidentally and the model zooms to the element. I have to zoom and pan to the view I'm comfortable with again and it is very annoying. Is there a way to disable this functionality in the online viewer?
I found similar questions for the desktop apps (https://forums.autodesk.com/t5/autocad-forum/turn-off-wheel-double-click-zoom-extents/td-p/53144) but not the online viewer.
There is a fairly straightforward workaround for that: create a custom tool and absorb the double-click event. See that article for a start Creating "Tools" for the View & Data API.
To absorb an event implement the corresponding handler and return true:
this.handleDoubleClick = function(event, button) {
// ... do your stuff if needed ...
// event handled
return true;
};