13 Sep 2017

Selection override

When clicking on objects in the Viewer, always the leaf node will be selected. 

In many models the components will have a child called "Body", or "Solid", etc, and often those are not the objects with the properties you are interested in. That makes it difficult for the user to get quickly at the data they need.

Fortunately, you can easily write a function that would override the selection to select the parent of the given object. You just have to listen to the Autodesk.Viewing.SELECTION_CHANGED_EVENT event and modify the selection:

    // ...
    oViewer =new Autodesk.Viewing.Private.GuiViewer3D ($("#viewer") [0], {});
    oViewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectionChanged)

function onSelectionChanged(event) {
    // Let's only control selection in case of
    // single user selection
    if (event.dbIdArray.length === 1) {
        oViewer.getProperties(event.dbIdArray[0], function(data) {
            if (data.name.startsWith("Solid")) {
                var instanceTree = oViewer.model.getData().instanceTree;
                var parentId = instanceTree.getNodeParentId(event.dbIdArray[0])

Now when clicking on the object, the parent gets selected which has all the properties we need:

Selecting the parent instead

Related Article

Posted By

Adam Nagy

Follow @AdamTheNagy Adam Nagy joined Autodesk back in 2005 and has been providing programming support, consulting, training and evangelism to external developers. He started his career in Budapest, then worked in Prague for 3 years and now lives in South England, UK. At the moment focusing on Inventor and Fusion 360, plus cloud and mobile related technologies. Adam has a degree in Software Engineering and has...