在 Forge 3D 查看器中将 instanceTree 设置为自定义节点

Set instanceTree to a custom node in Forge 3D viewer

假设我正在处理一个 3D 文件,它是一个建筑模型和一个结构模型的组合。 实例树或模型浏览器如下所示

root/
    Arch/
        Level 01/
        Level 02/
        ...
    Str/
        Level 01/
        Level 02/
        ...

我只想显示关卡 01。
所以我:

  1. 已按照查看器教程中的步骤操作
  2. Autodesk.Viewing.GEOMETRY_LOADED_EVENTAutodesk.Viewing.OBJECT_TREE_CREATED_EVENT
  3. 添加事件侦听器
  4. 当 2 发射时,我使用此 article 中的代码只显示关卡 01 而没有重影。

我对这种方法有 2 个问题

  1. 我必须等到整个模型加载完毕才能过滤关卡
  2. 过滤关卡后,如果我点击模型浏览器,我仍然可以看到整个模型结构(但除了关卡 01 之外的所有内容都是隐藏的)。如何将实例树设置为仅包含以下内容?

    root/
        Arch/
           Level 01/
        Str/
           Level 01/
    

编辑
我应该在什么时候覆盖 shouldInclude() 函数?
我试过这个并设置了一个断点,但它似乎永远不会被调用...我也尝试移动它但徒劳无功。

const start = Date.now();
Autodesk.Viewing.UI.ModelStructurePanel.shouldInclude = (node) => {
  Logger.log(node);
  return true;
};
Autodesk.Viewing.Initializer(options, () => {
  Logger.log(`Viewer initialized in ${Date.now() - start}ms`);
  const config = {};
  // prettier-ignore
  Autodesk.Viewing.theExtensionManager.registerExtension('MyAwesomeExtension', MyAwesomeExtension);
  viewerApp = new Autodesk.Viewing.ViewingApplication('MyViewerDiv');
  viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D, config);
  loadDocumentStart = Date.now();
  // prettier-ignore
  viewerApp.loadDocument(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
});

关于 #1:对象树存储在文件的内部数据库中 - 出于性能原因 - 仅在 实际几何体之后加载。

关于 #2:您可以子类化 ModelStructurePanel class and add your own behavior, for example, by overriding the ModelStructurePanel#shouldInclude 方法。

因为我无法理解如何使用 ModelStructurePanel,我覆盖了 Autodesk.Viewing.ViewingApplication.selectItem 以仅修改传递给 loadDocumentNodestartWithDocumentNode 如下:

const options = {
    ids: leafIDs.length > 0 ? leafIDs : null, // changed this line
    acmSessionId: this.myDocument.acmSessionId,
    loadOptions,
    useConsolidation: this.options.useConsolidation,
    consolidationMemoryLimit: this.options.consolidationMemoryLimit || 100 * 1024 * 1024, // 100 MB
  };

其中 leafIDs 是要显示的对象 ID 数组。我能够通过以下方式构建它:

  1. 正在使用 GET :urn/metadata/:guid
  2. 查询 ModelDerivativeAPI
  3. 通过树找到我感兴趣的 ID。

可能有更优雅的方法来做到这一点,但这是目前为止我能做的最好的方法。