Search code examples
autodesk-forgeautodesk-viewer

How to properly change the viewer setting for selectionMode in Autodesk Viewer


I need to change the selection mode for IFC models in Autodesk Viewer from "First Object" (value 1) to either "Leaf Object" or "Last Object" (0 or 2).

I have encountered multiple problems:

While other configuration parameters like viewCube can be modified programmatically using the profiles API, selectionMode seems to only change correctly when using the HTML interface.

Using the following code, the viewCube setting changes correctly, but the selectionMode does not:

let defaultCustomProfile = Autodesk.Viewing.ProfileSettings.Default;
let customProfileSettings = Autodesk.Viewing.ProfileSettings.clone(defaultCustomProfile);
customProfileSettings.settings.selectionMode = 2;
customProfileSettings.settings.viewCube = false;
const customProfile = new Autodesk.Viewing.Profile(customProfileSettings);
viewer.profileManager.registerProfile('Default', customProfile);
viewer.setProfile(customProfile);

Manually clicking the selectionMode option in the config panel, so that the viewer loads with that option from the saved config when reloading the viewer, does not always work. While the config panel shows the correct option is selected, the viewer sometimes functions as if "First Object" mode is still in effect. I have not been able to find a correlation as to why it works sometimes and not others.

Currently, the only option I have found that seems to work consistently is this workaround:

let intervalId = setInterval(function() {
  let dropdown = document.getElementById("Selection Mode_dropdown");
  if (dropdown) {
    dropdown.selectedIndex = 1;
    dropdown.dispatchEvent(new Event('change'));
    dropdown.selectedIndex = 0;
    dropdown.dispatchEvent(new Event('change'));
    clearInterval(intervalId);
  }
}, 500);

While this solution works perfectly, it is far from ideal as it is just a band-aid fix. I would like to know if there is a proper way to change selectionMode that will work

Also, is there somewhere I can read about the differences between "Leaf Object" and "Last Object"? From what I can see, they both appear to be the same.


Solution

  • The selection mode not being set by the profile seems like a potential bug. We will investigate this on our end, and if needed, report it to the engineering team.

    Note however that you can set the selection mode programatically using the viewer.setSelectionMode method. There should be no need to manipulate the settings UI programatically.

    As for the difference between "Leaf Object" and "Last Object", quoting the documentation:

    • Autodesk.Viewing.SelectionMode.LEAF_OBJECT: Always select the leaf objects in the hierarchy.
    • Autodesk.Viewing.SelectionMode.FIRST_OBJECT: For a given node, selects the first non-composite (layer, collection, model) on the path from the root to the given node, and all children.
    • Autodesk.Viewing.SelectionMode.LAST_OBJECT: For a given node, selects the nearest ancestor composite node and all children.

    These selection modes depend heavily on the types of nodes in the instance trees generated by the Model Derivative service. It is true that for many input file formats the Model Derivative service doesn't really generate any composite nodes, and so there's no difference between the LEAF_OBJECT and LAST_OBJECT modes.