如何获取 Autodesk Forge 中的所有 DB-ID

How to get all the DB-IDs in Autodesk Forge

我需要从 Autodesk forge 模型中获取所有 DB-Id。我引用了 https://forge.autodesk.com/cloud_and_mobile/2015/12/select-all-elements-in-the-viewer-with-view-and-data-api-with-javascript.html

中的代码

我在自己的扩展里也试过了,代码如下

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);
  }
};

但是我在开发者工具中遇到了一个错误 cannot read property 'getData' of null 你认为我哪里出错了。提前致谢。

问题一定是模型没有完全加载,所以您应该等待该事件 (Autodesk.Viewing.GEOMETRY_LOADED_EVENT)。最好也等待对象树创建事件 (Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT) - 请参阅此处的讨论:

顺便说一下,现在有一种更简单的方法来获取所有的 dbId:Get all database id's in the model