Autodesk forge 为什么ToolController';我的工具的getPriority返回0?

Autodesk forge 为什么ToolController';我的工具的getPriority返回0?,autodesk-forge,autodesk-viewer,Autodesk Forge,Autodesk Viewer,根据先前的介绍,您可以实现forge viewer工具。另一种说法是,扩展工具接口不起作用。因此,我没有像这样扩展ToolInterface来实现我的工具: class MyCustomExtension extends Autodesk.Viewing.Extension { constructor(viewer, options) { super(viewer, options); this.theiaUtil = new TheiaUtil(t

根据先前的介绍,您可以实现forge viewer工具。另一种说法是,扩展
工具接口
不起作用。因此,我没有像这样扩展ToolInterface来实现我的工具:

class MyCustomExtension extends Autodesk.Viewing.Extension {
    constructor(viewer, options) {
        super(viewer, options);   
        this.theiaUtil = new TheiaUtil(this);     
    }

    getPriority() {
        console.log("Theia#getPriority called! ", (this.getPriority && this.getPriority() || 0)); 
        return 100000;
    }

    ...
}

我的工具的优先级在ToolController中返回为0,但它不应:

function getPriority(tool) 
{
    return tool.getPriority instanceof Function && tool.getPriority() || 0;
}

我不知道为什么这个函数返回0作为
tool。如果我自己调用
MyCustomExtension.getPriority
函数,getPriority instanceof会返回true。

注意
ToolInterface
是这样实现的:

函数工具接口()
{
this.names=[“unnamed”];
this.getNames=function(){返回this.names;};
this.getName=function(){返回this.names[0];};
this.getPriority=function(){return 0;};
this.register=function(){};
this.deregister=函数(){};
this.activate=函数(名称,viewerApi){};
this.deactivate=函数(名称){};
this.update=函数(highResTimestamp){return false;};
this.handleSingleClick=函数(事件,按钮){return false;};
this.handleDoubleClick=函数(事件,按钮){return false;};
this.handleSingleTap=函数(事件){return false;};
this.handleDoubleTap=函数(事件){return false;};
// ...
}
因此,简单地扩展
工具接口
类是行不通的,因为在构造函数中添加到实例中的所有这些属性和函数都将优先于实际的类方法。这也可能是您看到优先级值返回为零的原因-当您调用
myTool.getPriority()
时,您实际上并不是在调用
getPriority
方法,而是在
ToolInterface
的构造函数中为
This.getPriority
指定的默认函数

为了解决这个问题,我建议显式删除类构造函数中的相应字段(我在上的博客文章中解释了这一点):

class DrawTool扩展了Autodesk.Viewing.ToolInterface{
构造函数(){
超级();
this.names=['box-drawing-tool','spheredrawing-tool'];
//Hack:删除工具实例*上定义的函数。
//我们希望工具控制器调用我们的类方法。
删除此注册表;
删除此项。注销;
删除此项。激活;
删除此项。停用;
删除此.getPriority;
删除此项。handleMouseMove;
删除此.handleButtonDown;
删除此.handleButtonUp;
删除此.handleSingleClick;
}
寄存器(){
console.log(“DrawTool已注册”);
}
注销{
console.log(“DrawTool未注册”);
}
激活(名称、查看器){
console.log('绘图工具已激活');
}
停用(名称){
console.log(“DrawTool已停用”);
}
getPriority(){
return 42;//或者可以随意使用大于0的任何数字(这是所有默认查看器工具的优先级)
}
// ...
}

TL;DR:从工具栏按钮而不是扩展的加载方法激活按钮单击事件中的工具

在扩展的加载方法中注册后,我立即激活了该工具。Petr Broz的github repo在他的博客文章中从工具栏中的按钮加载该工具。因此,我将该工具的激活移动到工具栏中的一个按钮点击,这对我来说很有用

class MyExtension extends Autodesk.Viewing.Extension {
    ...
    onToolbarCreated(toolbar) {
        const MyToolName = 'My.Tool.Name'
        let button = new Autodesk.Viewing.UI.Button('my-tool-button');
            button.onClick = (e) => {
            const controller = this.viewer.toolController;
            if (controller.isToolActivated(MyToolName)) {
                controller.deactivateTool(MyToolName);
                button.setState(Autodesk.Viewing.UI.Button.State.INACTIVE);
            } else {
                controller.activateTool(MyToolName);
                button.setState(Autodesk.Viewing.UI.Button.State.ACTIVE);
            }
        };
    }
    ...
}