javascriptautodesk-forgeautodesk-viewer

How to modify or remove default context menu items and how to create submenus in custom context menu?


I'm using Autodesk Forge Viewer version 7.x and I have two questions regarding the context menu behavior.

  1. Is it possible to modify or remove the default context menu items? By default, when right-clicking in the viewer, it shows built-in items such as:

    • Isolate
    • Hide selected
    • Show all objects

Is there any supported way to rename or remove these default items?

  1. How to create submenus using registerContextMenuCallback? I successfully used viewer.registerContextMenuCallback to add custom context menu items. However, I couldn't find any documentation or examples about adding submenus under a parent item.

Here is a simplified example of what I'm trying to achieve:

Javascript

viewer.registerContextMenuCallback('my-extension', (menu, status) => {
  menu.push({
    title: 'Custom Action',
    target: () => { console.log('clicked'); },
    // How to add submenu here?
  });
});

Is there a supported way to define a submenu structure in Forge Viewer?

Environment:

Any insights or code examples would be greatly appreciated!


Solution

    1. You can always check how certain things are done in the Viewer and just follow that. E.g. Section has a sub menu item:
      Section tool context menu
      So you can check how that item is added:
      Section tool menu item

    2. If you know which extension a given menu item belongs to then I guess you could just disable that extension.
      https://aps.autodesk.com/blog/minimizing-viewer-workloads-loading-models-partially-selected-components-and-features-only
      Otherwise you would have to somehow make sure that all extensions that are adding menu items are loaded before you call registerContextMenuCallback so that you are called last then you have full control of all the other menu items