user8265273
user8265273

Reputation:

How to get all the DB-IDs in Autodesk Forge

I need to get all the DB-Ids from the Autodesk forge model. I have referred the code from https://forge.autodesk.com/cloud_and_mobile/2015/12/select-all-elements-in-the-viewer-with-view-and-data-api-with-javascript.html

I also have tried it in my own extension and the code is as follows.

AutodeskNamespace("Autodesk.ADN.Viewing.Extension");
Autodesk.ADN.Viewing.Extension.Color = function(viewer, options) {

  Autodesk.Viewing.Extension.call(this, viewer, options);
  var _self = this;

  var _viewer = viewer;
  var instanceTree = viewer.model.getData().instanceTree;

  var rootId = this.rootId = instanceTree.getRootId();
  _self.load = function() {
    getgetAlldbIds(rootId);
  };

  function getAlldbIds(rootId) {
    var alldbId = [];
    if (!rootId) {
      return alldbId;
    }
    var queue = [];
    queue.push(rootId);
    while (queue.length > 0) {
      var node = queue.shift();
      alldbId.push(node);
      instanceTree.enumNodeChildren(node, function(childrenIds) {
        queue.push(childrenIds);
      });
    }
    console.log(alldbId);
  }
};

But I'm getting an error in Developer Tools as cannot read property 'getData' of null where do you think I'm going wrong. thanks in advance.

Upvotes: 1

Views: 2427

Answers (1)

Adam Nagy
Adam Nagy

Reputation: 2125

The problem must be that the model is not fully loaded, so you should wait for that event (Autodesk.Viewing.GEOMETRY_LOADED_EVENT). It might be better to wait for the object tree created event as well (Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT) - see discussion here: How to Retrieve Forge Viewer objectTree?

By the way, there is an easier way now to get all the dbIds: Get all database id's in the model

Upvotes: 2

Related Questions