Apurva Bhavsar
Apurva Bhavsar

Reputation: 37

Can not fetch instanceTree autodesk

I am getting this error: Uncaught TypeError:> Cannot read property 'getRootId' of undefined even i am using Autodesk.Viewing.GEOMETRY_LOADED_EVENT..still no effect.

Upvotes: 0

Views: 415

Answers (2)

Felipe
Felipe

Reputation: 4375

You just need to wait for Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT to be fired when you want to access the instanceTree:

viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {

  var instanceTree = model.getData().instanceTree //cool
})

Upvotes: 2

Augusto Goncalves
Augusto Goncalves

Reputation: 8574

You should not be using instanceTree data structure, but yet the functions/operations, which are the supported way. If your need is to enumerate leaf nodes, try something similar to as described here:

function getAllLeafComponents(viewer, callback) {
    var cbCount = 0; // count pending callbacks
    var components = []; // store the results
    var tree; // the instance tree

    function getLeafComponentsRec(parent) {
        cbCount++;
        if (tree.getChildCount(parent) != 0) {
            tree.enumNodeChildren(parent, function (children) {
                getLeafComponentsRec(children);
            }, false);
        } else {
            components.push(parent);
        }
        if (--cbCount == 0) callback(components);
    }
    viewer.getObjectTree(function (objectTree) {
        tree = objectTree;
        var allLeafComponents = getLeafComponentsRec(tree.getRootId());
    });
}

Upvotes: 0

Related Questions