Search code examples
autodesk-forgeautodesk-viewer

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!


Solution

  • 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