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

如何从Autodesk Viewer对象中获取顶点坐标等几何数据?

How to Retrieve Vertex Data from a dbId in Forge Viewer

Absolutely, you can get vertex data from a dbId—you just need to combine the two methods you’ve already experimented with! Here’s a step-by-step breakdown to make it work:

Step 1: Map dbId to its associated fragIds

First, use the fragment list to get all fragIds linked to your target dbId. Keep in mind a single dbId might be split across multiple fragments, so you’ll get an array of fragIds:

const fragmentList = viewer.model.getFragmentList();
const fragIds = fragmentList.dbId2fragId[targetDbId]; // Returns an array of frag IDs

Step 2: Extract vertex data from each fragment

For each fragId in the array, fetch the fragment proxy (like you did before) and then access the underlying THREE.js geometry to pull out vertex positions:

async function getVerticesFromDbId(viewer, dbId) {
  const fragmentList = viewer.model.getFragmentList();
  const fragIds = fragmentList.dbId2fragId[dbId];
  
  if (!fragIds || fragIds.length === 0) return [];

  const allVertices = [];

  for (const fragId of fragIds) {
    // Make sure the fragment is loaded (critical for large models)
    if (!fragmentList.isFragmentLoaded(fragId)) {
      await new Promise(resolve => {
        viewer.impl.loadFragment(viewer.model, fragId, resolve);
      });
    }

    // Get the fragment proxy and update its transform
    const fragProxy = viewer.impl.getFragmentProxy(viewer.model, fragId);
    fragProxy.updateAnimTransform(); // Ensures vertices are in world space

    // Access the THREE.js mesh and its geometry
    const mesh = fragProxy.getMesh();
    if (!mesh || !mesh.geometry) continue;

    // Extract raw vertex data (flat array: x1, y1, z1, x2, y2, z2, ...)
    const positionArray = mesh.geometry.attributes.position.array;
    allVertices.push(...Array.from(positionArray));
  }

  // Optional: Convert flat array into [x, y, z] tuples for easier use
  const vertexTuples = [];
  for (let i = 0; i < allVertices.length; i += 3) {
    vertexTuples.push([allVertices[i], allVertices[i+1], allVertices[i+2]]);
  }

  return vertexTuples;
}

Key Notes:

  • World vs Local Space: Calling fragProxy.updateAnimTransform() gives you vertices in world space (aligned with the viewer’s coordinate system). Skip this if you need vertices in the fragment’s local space.
  • Loading Fragments: For large models, some fragments might not be loaded initially. The async wait ensures we don’t try to access unloaded data.
  • Edge Cases: Complex or instanced geometries might need extra handling, but this works for most standard model components.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:12:00