如何在Forge Viewer中卸载默认扩展?

我尝试从Autodesk卸载查看器中的扩展,我从Viewing.Initializer调用了回调内部的扩展,但这部分代码中的扩展似乎尚未加载。

var viewer;
var options = {
    env: 'AutodeskProduction',
    api: 'derivativeV2',  // for models uploaded to EMEA change this option to 'derivativeV2_EU'

    // Function to define the method to get the token and renew it
    getAccessToken: function (onTokenReady) {
        let token = '';
        let timeInSeconds = 3600; // TODO: Use value provided by Forge Authentication (OAuth) API

        // Code to get my token and time remaining
        onTokenReady(token, timeInSeconds);

    }
};

/**
 * Initializer function, when load the viewer
 */
Autodesk.Viewing.Initializer(options, function () {
    // Extensions
    var config3d = {
        extensions: ['forgeExtension', 'EventsTutorial', 'Autodesk.DocumentBrowser'],
    };

    // The dom element, where load the viewer
    var htmlDiv = document.getElementById('forgeViewer');
    viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv, config3d);


   //
   // Here I want to unload 'Autodesk.Explode'
   let explodeExtension = viewer.getExtension('Autodesk.Explode'); // explodeExtension is null
   explodeExtension.unload();
   //
   //

    var startedCode = viewer.start();
    if (startedCode > 0) {
        console.error('Failed to create a Viewer: WebGL not supported.');
        return;
    }

    console.log('Initialization complete, loading a model next...');
});

是否有任何建议卸载默认扩展? 我正在使用V7查看器,我在V6中尝试了同样的结果。


解决方案

您可以使用查看器事件来确保扩展存在并且可以卸载。您的卸载代码是正确的,您只需要等待完整的几何图形加载。使用此查看器事件,您可以在加载几何时卸载默认扩展模块

viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, (x) =>  {
let explodeExtension = viewer.getExtension('Autodesk.Explode'); /
explodeExtension.unload();} );

这将在加载扩展后立即卸载它。

或者,您也可以通过取消注册来阻止加载扩展。Autodesk.Viewing.theExtensionManager.unregisterExtension('Autodesk.Explode');这将导致错误,因为查看器仍在尝试加载扩展,但查看器仍将按预期工作。

相关文章