You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Model-Derivative API与Viewer 7的模型特定对象查看及子dbId获取问询

获取父dbId的所有子节点dbId(Viewer 7)

Viewer 7提供了两种实用方式来获取父节点的子节点:

1. 基于Object Tree API直接查询

通过viewer.model.getObjectTree()获取模型的对象树实例,可直接获取直接子节点,若需递归获取所有后代节点,可自行实现递归逻辑:

// 获取模型对象树
const objectTree = await viewer.model.getObjectTree();

// 获取父节点的所有后代子节点dbId(包含直接子节点)
async function getAllChildDbIds(parentDbId) {
  let childDbIds = [];
  // 获取直接子节点
  const directChildren = await objectTree.getNodeChildren(parentDbId);
  childDbIds.push(...directChildren.map(node => node.dbId));
  // 递归处理子节点的后代
  for (const childNode of directChildren) {
    const grandChildren = await getAllChildDbIds(childNode.dbId);
    childDbIds.push(...grandChildren);
  }
  return childDbIds;
}

// 调用示例:假设父节点dbId为123
const parentDbId = 123;
const allChildDbIds = await getAllChildDbIds(parentDbId);
console.log('所有子节点dbId:', allChildDbIds);

如果仅需直接子节点,简化代码即可:

const objectTree = await viewer.model.getObjectTree();
const directChildNodes = await objectTree.getNodeChildren(parentDbId);
const directChildDbIds = directChildNodes.map(node => node.dbId);

2. 结合Bulk Properties API筛选

若需结合属性过滤子节点,可使用viewer.model.getBulkProperties辅助筛选:

const parentDbId = 123;
const objectTree = await viewer.model.getObjectTree();
const allDbIds = await objectTree.getRootId().then(rootId => objectTree.search(node => true));
// 筛选父节点为指定dbId的所有节点
const childDbIds = await viewer.model.getBulkProperties(allDbIds, ['parent']).then(props => {
  return props.filter(prop => prop.properties.find(p => p.displayValue === parentDbId.toString())).map(p => p.dbId);
});

查看树结构中的最后一组子节点(叶子节点)

你提到的“最后一组子节点”通常指无后代的叶子节点,可通过以下步骤实现:

1. 定位叶子节点

遍历对象树,收集所有没有子节点的节点:

const objectTree = await viewer.model.getObjectTree();
const rootId = await objectTree.getRootId();

// 递归遍历收集所有叶子节点dbId
async function getAllLeafDbIds(nodeId) {
  let leafDbIds = [];
  const children = await objectTree.getNodeChildren(nodeId);
  if (children.length === 0) {
    leafDbIds.push(nodeId);
  } else {
    for (const child of children) {
      const childLeaves = await getAllLeafDbIds(child.dbId);
      leafDbIds.push(...childLeaves);
    }
  }
  return leafDbIds;
}

// 获取所有叶子节点dbId
const leafDbIds = await getAllLeafDbIds(rootId);

2. 查看/高亮目标节点

获取叶子节点后,可通过Viewer API执行高亮、隔离等操作:

  • 高亮并适配视角:
viewer.clearSelection();
viewer.select(leafDbIds);
viewer.utilities.fitToView(leafDbIds);
  • 隔离显示叶子节点:
viewer.isolate(leafDbIds);
  • 仅显示叶子节点:
const allDbIds = await objectTree.getRootId().then(rootId => objectTree.search(node => true));
viewer.hide(allDbIds.filter(id => !leafDbIds.includes(id)));
viewer.show(leafDbIds);
viewer.utilities.fitToView(leafDbIds);

注意事项

  • 调用异步API时需使用async/await或Promise链式调用,避免回调嵌套问题
  • 大型模型遍历节点可能存在性能损耗,建议在模型加载完成后(监听viewer.model.onLoadDone事件)再执行操作
  • 若“最后一组子节点”是指定父节点下的最末层级节点,只需将getAllLeafDbIds的起始参数改为该父节点dbId即可

内容的提问来源于stack exchange,提问作者chelmy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:05:21