Search code examples
autodesk-forgeautodesk-viewer

Why does the ToolController's getPriority return 0 for my tool?


According to a prior SO answer, you can implement getPriority for a forge viewer Tool. And according to another SO answer extending the ToolInterface does not work. Hence, me not extending the ToolInterface implementing my Tool like so:

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

    ...
}

My tool's priority is returned as 0 in the ToolController, although it shouldn't:

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

I don't know why this function returns 0 as tool.getPriority instanceof Function returns true if I call MyCustomExtension.getPriority myself.


Solution

  • TL;DR: Activate the tool in button click event from a toolbar button instead of the extension's load method.

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

    I activated the tool instantly after registering it in the Extension's load method. Petr Broz's github repo from his blog post loads the tool from a button in the toolbar. So I moved the activation of the tool to a button click in the toolbar which worked for me.