使用Autodesk.Viewing.Navigation.fitBounds时出现fov未定义错误求助
问题描述
尝试结合getBoundingBox与fitBounds方法实现模型定位时,出现以下未捕获错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'fov') at Navigation.getVerticalFov (UnitFormatter.js:320:1) at Navigation.fitBounds (SnapResult.js:87:1) at Array.<anonymous> (index.ts:1100:25) at propertyWorkerCallback (LeafletLoader.js:42:1) at Worker.callbackFn (PropDbLoader.js:151:1)
调用代码:
const boundingBox = extension.getBoundingBox(dbId) window.navigation.fitBounds(true, boundingBox, true, true);
getBoundingBox实现:
getBoundingBox = (dbId: number): THREE.Box3 => { const fragIds = this.getFragmentIds(dbId); const fragList = this.viewer.model.getFragmentList(); const fragBox = new THREE.Box3(); const nodeBox = new THREE.Box3(); fragIds.forEach((fragId) => { fragList.getWorldBounds(fragId, fragBox); nodeBox.union(fragBox); }); return nodeBox; };
问题排查与原因分析
1. Navigation实例引用错误
你代码中使用的window.navigation是浏览器原生的导航接口(用于历史记录操作),并非Autodesk Viewer的Navigation实例。Viewer的导航控制对象是viewer.navigation(viewer为你的Viewer3D实例),调用错误的实例会导致内部找不到相机(camera)属性,进而触发fov读取失败的错误。
2. 调用时机过早
如果在Viewer未完全初始化、模型几何未加载完成时调用fitBounds,Navigation实例的相机对象可能还未创建,同样会出现fov未定义的错误。
3. 无效的BoundingBox
若目标dbId不存在、未找到对应片段ID(fragIds为空数组),getBoundingBox会返回一个空的THREE.Box3对象,传递给fitBounds后可能导致内部逻辑异常,间接引发相机相关错误。
解决方案
- 修正Navigation实例引用
将window.navigation替换为Viewer实例的navigation属性:
const boundingBox = extension.getBoundingBox(dbId); viewer.navigation.fitBounds(true, boundingBox, true, true);
- 确保调用时机正确
在模型加载完成后执行定位逻辑,例如监听OBJECT_TREE_CREATED_EVENT事件:
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, () => { const boundingBox = extension.getBoundingBox(dbId); if (!boundingBox.isEmpty()) { viewer.navigation.fitBounds(true, boundingBox, true, true); } });
- 校验BoundingBox有效性
调用fitBounds前检查边界框是否有效,避免传入空值:
const boundingBox = extension.getBoundingBox(dbId); if (!boundingBox.isEmpty()) { viewer.navigation.fitBounds(true, boundingBox, true, true); } else { console.error(`无法获取dbId ${dbId}的有效边界框`); }
内容的提问来源于stack exchange,提问作者IBG
相关产品推荐
相关产品推荐

