简体   繁体   English

如何保存多 model Autodesk Forge 查看器 state 并恢复相同?

[英]How to save a multi model Autodesk forge viewer state and restore the same?

I have loaded multiple models in the same scene and want to persist a slice viewer state and restore later.我在同一场景中加载了多个模型,并希望保留切片查看器 state 并稍后恢复。 It works with a single model but isn't working with multiple models.它适用于单个 model 但不适用于多个型号。

Unfortunately at the moment you can only restoreState for 1 model. 不幸的是,目前您只能为1个模型恢复状态。 But I have brought up this to our engineers and they have added it as an enhancement request. 但是我已经将此问题提交给我们的工程师,他们已将其添加为增强功能。

A solution to save and restore viewer state from multiple models is to change the way the seedUrn works on the ViewerState.js.从多个模型保存和恢复查看器 state 的解决方案是更改 seedUrn 在 ViewerState.js 上的工作方式。

The issue is that seedUrn, as a simple string, cannot identify models accurately.问题在于 seedUrn 作为一个简单的字符串,无法准确识别模型。 The solution works by changing it to an object, containing the urn of the model and a unique key (set as a loadOption during model loading).该解决方案的工作原理是将其更改为 object,其中包含 model 的 urn 和一个唯一密钥(在 model 加载期间设置为 loadOption)。 When ViewerState needs to find a model, it searches for both the urn and the loaded key, which, if unique, is able to handle even multiple identical models, as long as the key is unique (maybe urn is not needed, but I won't handle that now).当 ViewerState 需要找到一个 model 时,它会同时搜索 urn 和加载的密钥,如果唯一的话,它甚至能够处理多个相同的模型,只要密钥是唯一的(也许不需要 urn,但我赢了'现在处理)。

This is the code that changes the two methods on ViewerState, relating to generating and comparing seedUrn:这是更改 ViewerState 上与生成和比较 seedUrn 相关的两个方法的代码:

    NOP_VIEWER.viewerState.getSeedUrn = function (model) {
        model = model || viewer.model;
        if (model === null) {
            return {
                urn: "",
                uniqueKey: undefined
            };
        } else {
            return {
                urn: model.getSeedUrn(),
                uniqueKey: model.myData.loadOptions.uniqueKey
            };
        }
    };

    NOP_VIEWER.viewerState.getVisibleModel = function (seedUrn) {
        const visibleModels = viewer.getVisibleModels();
        for (let i = 0; i < visibleModels.length; ++i) {
            const modelSeedUrn = this.getSeedUrn(visibleModels[i]);
            if (modelSeedUrn.urn === seedUrn.urn && modelSeedUrn.uniqueKey === seedUrn.uniqueKey) {
                return visibleModels[i];
            }
        }
    };

When loading a model into the viewer, pass the uniqueKey as a loadOption:将 model 加载到查看器时,将 uniqueKey 作为 loadOption 传递:

viewer.loadDocumentNode(obj.doc, obj.geometry, {
    ...
    uniqueKey: 'a unique identifier',
    ...
})

If you need to persist the state in a database, for example, make sure you also save the unique key for the model, since it needs to be loaded with the same key contained in the state JSON.例如,如果您需要将 state 保存在数据库中,请确保您还保存了 model 的唯一密钥,因为它需要使用 state JSON 中包含的相同密钥进行加载。

Note that this solution does not handle cutplanes, which still rely on the viewer.model.请注意,此解决方案不处理切面,切面仍依赖于查看器。model。 Solving that would require overwriting the getState and restoreState functions entirely, which could get obsolete quickly in newer versions of the Viewer.解决这个问题需要完全覆盖 getState 和 restoreState 函数,这在较新版本的 Viewer 中可能很快就会过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM