开发者问题收集

使用自定义扩展查看器 Autodesk Forge

2020-09-24
575

我正在尝试研究 Autodesk Forge。我尝试测试 此链接 中的脚本,以注册扩展程序在单击按钮时更改背景颜色。但是单击时,alert("Autodesk.ADN.Viewing.Extension.Basic loaded") 可见,但背景颜色不满意。 如果您知道,请告诉我原因以及如何解决。

提前谢谢!

AutodeskNamespace("Autodesk.ADN.Viewing.Extension");
    Autodesk.ADN.Viewing.Extension.Basic = function (viewer, options) {
        Autodesk.Viewing.Extension.call(this, viewer, options);
        
        var _this = this;
        _this.load = function () {
            alert("Autodesk.ADN.Viewing.Extension.Basic loaded");
            viewer.setBackgroundColor(255, 0, 0, 255, 255, 255);
            return true;
        };
        _this.unload = function () {
            viewer.setBackgroundColor(160, 176, 184, 190, 207, 216);
            alert("Autodesk.ADN.Viewing.Extension.Basic unloaded");
            Autodesk.Viewing.theExtensionManager.unregisterExtension(
                "Autodesk.ADN.Viewing.Extension.Basic");
            return true;
            };
        };
    Autodesk.ADN.Viewing.Extension.Basic.prototype = Object.create(Autodesk.Viewing.Extension.prototype);
    Autodesk.ADN.Viewing.Extension.Basic.prototype.constructor = Autodesk.ADN.Viewing.Extension.Basic;
    Autodesk.Viewing.theExtensionManager.registerExtension("Autodesk.ADN.Viewing.Extension.Basic", Autodesk.ADN.Viewing.Extension.Basic);
$(document).ready(function(){
            $("#button").click(function(){
                viewer.loadExtension('Autodesk.ADN.Viewing.Extension.Basic');
            })
    })
1个回答

您链接到的博客文章来自 2016 年。Forge Viewer 仍在快速发展,因此我担心这篇博文中的代码片段太过时了。

如果您有兴趣了解有关 Forge 平台的更多信息,我建议您查看 https://learnforge.autodesk.io 网站。它包含各种教程,每当 Forge 服务或 Forge Viewer API 发生变化时,我们都会尝试使它们保持最新状态。还有一个专门关于查看器扩展的教程: https://learnforge.autodesk.io/#/tutorials/extensions

为了让您有一个概念,下面是今天使用 查看器版本 7.* 实现一个简单的查看器扩展的方法:

class MyAwesomeExtension extends Autodesk.Viewing.Extension {
    constructor(viewer, options) {
        super(viewer, options);
    }

    load() {
        console.log('MyAwesomeExtensions has been loaded');
        viewer.setEnvMapBackground(null); // Hide background environment if there is one
        viewer.setBackgroundColor(0, 64, 128); // Set background color
        return true;
    }

    unload() {
        console.log('MyAwesomeExtensions has been unloaded');
        return true;
    }
}

Autodesk.Viewing.theExtensionManager.registerExtension('MyAwesomeExtension', MyAwesomeExtension);

然后,在初始化查看器时,您将像这样加载扩展:

let viewer = new Autodesk.Viewing.GuiViewer3D(divElement, { extensions: ['MyAwesomeExtension'] });
Petr Broz
2020-09-24