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

Forge Viewer中如何恢复存储的sectionBox值?setSectionBox无效怎么解决

问题根因

你代码里的问题主要出在3处:

  1. 变量被意外覆盖:你在loadExtension回调中已经拿到了Section扩展的实例sectionTool,后续又执行var sectionTool = markupsExt.tool.getSectionBoxValues()重写了该变量,最终调用setSectionBox的对象已经不是剖切扩展实例,自然不会生效。
  2. 变换矩阵应用方式错误:原生THREE.Box3没有transform属性,你直接挂载该属性不会被setSectionBox方法识别,需要主动将变换矩阵应用到Box3实例上。
  3. 缺少必要的状态校验:未确认剖切工具完全激活就执行设置操作,可能因为状态未初始化导致设置失效。

修正后的代码

viewer.restoreState(currentState, null, false);

viewer.hide(Object.values(val.hiddenNodes));
if(Object.values(val.isolateNodes).length > 0){                     
    viewer.isolate(Object.values(val.isolateNodes));
}
if(val.cutPlanes.length !== 0){
    viewer.loadExtension("Autodesk.Section").then(function(sectionExt){
        // 先激活对应剖切模式
        sectionExt.activate(val.sectionStyle);
        
        // 构造剖切框边界
        const sectionbboxmin = new THREE.Vector3(val.sectionBox[0], val.sectionBox[1], val.sectionBox[2]);      
        const sectionbboxmax = new THREE.Vector3(val.sectionBox[3], val.sectionBox[4], val.sectionBox[5]);
        const box = new THREE.Box3(sectionbboxmin, sectionbboxmax);
        
        // 应用变换矩阵
        if (val.sectionBoxTransform) {
            // 若保存的矩阵是数组格式,先转为THREE.Matrix4实例再应用
            const transformMatrix = new THREE.Matrix4().fromArray(val.sectionBoxTransform);
            box.applyMatrix4(transformMatrix);
        }
        
        // 设置剖切框
        sectionExt.setSectionBox(box);
        // 低版本Viewer可加下面这句强制刷新视图
        // viewer.impl.invalidate(true, true, true);
    });
}

校验注意事项

  • 确认你保存的val.sectionBox顺序为minX、minY、minZ、maxX、maxY、maxZ,顺序错误会导致剖切框生成异常
  • 如果你保存的sectionBoxTransform本身就是THREE.Matrix4实例,可直接调用box.applyMatrix4(val.sectionBoxTransform)无需转换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 00:06:08