10 Mar 2019

When to remove toolbar buttons

Sometimes you may want to restrict which buttons you make available for the user. In that case you can just find the specific button and use removeControl() function to remove it. 

However, depending on which control/button we talk about the time to remove it might be different as they might be added by different extensions.
E.g. as shown in Philippe's answer, when you want to remove the 'toolbar-firstPersonTool' you can listen to the loading of the 'Autodesk.FirstPerson' extension, and do it after that: https://stackoverflow.com/questions/49334001/how-to-remove-first-person-tool-button-from-the-forge-navigation-tool-bar

The source code of the Viewer is available, so one way of finding out what event fires after a given control has been added might be found that way. 

If I want to remove e.g. the "toolbar-modelStructureTool" button and I search for it in the source code, then I find that it's added in the initModelTools function:

initModelTools function

When I search for that function I can see that the next event fired after that is the "TOOLBAR_CREATED_EVENT":

TOOLBAR_CREATED_EVENT

So that event should fire after the "toolbar-modelStructureTool" button has been created, so I should be able to remove it.

// used by viewer.addEventListener(Autodesk.Viewing.TOOLBAR_CREATED_EVENT, onToolbarCreated)
const onToolbarCreated = (e) => {
    const settingsTools = viewer.toolbar.getControl('settingsTools')
    settingsTools.removeControl('toolbar-modelStructureTool') 
    settingsTools.removeControl('toolbar-propertiesTool')            
    settingsTools.removeControl('toolbar-settingsTool')          
    settingsTools.removeControl('toolbar-fullscreenTool')           

    viewer.removeEventListener(
        Autodesk.Viewing.TOOLBAR_CREATED_EVENT,
        onToolbarCreated)  
} 

As you can see in the topmost picture, by the time this event is fired the buttons I'm interested in have been created so I can remove them. Here is the result:

Result

Tags:

Related Article