微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么ToolController的getPriority为我的工具返回0?

如何解决为什么ToolController的getPriority为我的工具返回0?

根据先前的SO answer,您可以为伪造查看器工具实现getPriority。并且根据另一SO answer,扩展$a1=array($source_array["name"],"<old_name>"); $a2=array($source_array["name"],"<new_name>"); print_r(array_replace($a1,$a2)); 不起作用。因此,我不会像这样扩展扩展实现我的工具的ToolInterface:

ToolInterface

我的工具的优先级在ToolController中返回为0,尽管不应这样:

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;
    }

    ...
}

我不知道为什么此函数会返回0,因为如果我自己调用function getPriority(tool) { return tool.getPriority instanceof Function && tool.getPriority() || 0; } 会导致tool.getPriority instanceof Function返回true。

解决方法

请注意,ToolInterface的实现方式如下:

function ToolInterface()
{
    this.names = [ "unnamed" ];
    this.getNames = function() { return this.names; };
    this.getName = function() { return this.names[0]; };
    this.getPriority = function() { return 0; };
    this.register = function() {};
    this.deregister = function() {};
    this.activate = function(name,viewerApi) {};
    this.deactivate = function(name) {};
    this.update = function(highResTimestamp) { return false; };
    this.handleSingleClick = function( event,button ) { return false; };
    this.handleDoubleClick = function( event,button ) { return false; };
    this.handleSingleTap = function( event ) { return false; };
    this.handleDoubleTap = function( event ) { return false; };
    // ...
}

因此,仅扩展ToolInterface类是行不通的,因为添加到构造函数实例中的所有这些属性和函数将优先于实际的类方法。这也可能是您看到优先级值返回为零的原因-调用myTool.getPriority()时,实际上并没有调用getPriority方法,而是分配给了{ this.getPriority的构造函数中的{1}}。

要变通解决此问题,我建议您明确删除类的构造函数中的相应字段(我在implementing custom Forge Viewer tools的博客文章中对此进行了解释):

ToolInterface
,

TL; DR:而不是扩展程序的加载方法,通过工具栏按钮激活按钮单击事件中的工具。

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);
            }
        };
    }
    ...
}

在扩展程序的load方法中注册该工具后,我立即激活了该工具。 Petr Broz在他的博客文章中的github存储库通过工具栏中的按钮加载。因此,我将工具的激活移到了对我有用的工具栏中单击按钮。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。