DWG模型坐标转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:
Use the Official Viewer Method
The simplest and most future-proof way is to callviewer.model.getGlobalOffset(). This method returns aTHREE.Vector3representing the offset applied to the model to center it in the Viewer:const globalOffset = viewer.model.getGlobalOffset();Access Placement Data (Alternative)
If you need to dig into the model’s placement details, you can useviewer.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().coordinateSystemto avoid axis misalignment issues.
内容的提问来源于stack exchange,提问作者nodd13

