简体   繁体   中英

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. It works with a single model but isn't working with multiple models.

Unfortunately at the moment you can only restoreState for 1 model. 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.

The issue is that seedUrn, as a simple string, cannot identify models accurately. 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). 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).

This is the code that changes the two methods on ViewerState, relating to generating and comparing 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:

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.

Note that this solution does not handle cutplanes, which still rely on the viewer.model. Solving that would require overwriting the getState and restoreState functions entirely, which could get obsolete quickly in newer versions of the Viewer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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