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":


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')


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:



Related Article

Posted By

Adam Nagy

Follow @AdamTheNagy Adam Nagy joined Autodesk back in 2005 and has been providing programming support, consulting, training and evangelism to external developers. He started his career in Budapest, then worked in Prague for 3 years and now lives in South England, UK. At the moment focusing on Inventor and Fusion 360, plus cloud and mobile related technologies. Adam has a degree in Software Engineering and has...