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

DWG模型坐标转Autodesk Forge Viewer坐标的实现问题

Fixing Coordinate Conversion for Point Display in Autodesk Forge Viewer

Hey there! Let’s work through this coordinate conversion issue you’re hitting when trying to display points in the Forge Viewer. It’s super common for older code snippets (like that Stack Overflow post) to break a bit with newer Viewer versions or different model types (like DWGs), so let’s break down what’s going on and how to fix it.

Why viewer.model.getData().globalOffset Isn’t Working

The globalOffset property used to live directly under model.getData(), but in recent Viewer updates (and for certain model formats like DWG), this property has moved or been replaced by more robust methods. Instead of digging into raw model data directly, the Viewer now provides an official method to get this offset, which is way more reliable.

Correct Ways to Get the Global Offset

Here are two solid approaches to retrieve the offset you need for coordinate conversion:

  1. Use the Official Viewer Method
    The simplest and most future-proof way is to call viewer.model.getGlobalOffset(). This method returns a THREE.Vector3 representing the offset applied to the model to center it in the Viewer:

    const globalOffset = viewer.model.getGlobalOffset();
    
  2. Access Placement Data (Alternative)
    If you need to dig into the model’s placement details, you can use viewer.model.getData().placementWithOffset. The translation property here holds the same offset value:

    const modelData = viewer.model.getData();
    const globalOffset = modelData.placementWithOffset.translation;
    

Full Example: Convert CAD Coordinates to Viewer World Coordinates

Once you have the global offset, you can convert your raw CAD points to the Viewer’s coordinate system. Also, keep in mind that some DWG models might use a Z-Up coordinate system instead of the Viewer’s Y-Up, so we’ll add a check for that too:

// 1. Get your raw CAD point (from your DWG data)
const cadPoint = new THREE.Vector3(100, 200, 300);

// 2. Handle Z-Up to Y-Up conversion if needed
const coordSys = viewer.model.getData().coordinateSystem;
if (coordSys === 'ZUp') {
  // Swap Y and Z axes, invert Z to match Viewer's orientation
  const tempY = cadPoint.y;
  cadPoint.y = cadPoint.z;
  cadPoint.z = -tempY;
}

// 3. Apply global offset conversion
const globalOffset = viewer.model.getGlobalOffset();
const viewerWorldPoint = cadPoint.sub(globalOffset);

// 4. Create and add a point marker to the Viewer
const sphereGeometry = new THREE.SphereGeometry(8, 16, 16);
const redMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const pointMarker = new THREE.Mesh(sphereGeometry, redMaterial);
pointMarker.position.copy(viewerWorldPoint);

// Add to Viewer's scene and refresh
viewer.impl.scene.add(pointMarker);
viewer.impl.invalidate(true);

Key Notes

  • Always prefer viewer.model.getGlobalOffset() over raw data properties—this method is maintained by Autodesk and will work across model types and Viewer versions.
  • For DWGs, double-check the coordinate system with viewer.model.getData().coordinateSystem to avoid axis misalignment issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:57:40