1

In our developed system, we integrated Autodesk Viewer to display BIM models from ACC. With the default API settings, holding Ctrl and clicking on a BIM model component selects multiple unrelated components simultaneously. How can I use the Autodesk Viewer API to achieve the behavior where ‌"holding Ctrl + clicking a component only adds the clicked component to the selection"‌?

Please provide guidance on adjusting the selection logic via the API to ensure precise single-component selection during Ctrl+click interactions.

1
  • Please provide enough code so others can better understand or reproduce the problem. Commented Apr 10 at 11:49

1 Answer 1

0

You can built a Viewer extension to override the click behavior and achieve this goal, just like the sample below:

const MultiSelectionToolToolName = 'multi-selection-tool';

class MultiSelectionTool extends Autodesk.Viewing.ToolInterface {
  constructor(viewer) {
    super();
    this.viewer = viewer;
    this.names = [MultiSelectionToolToolName];
    this.selection = [];
    this.active = false;
    // Hack: delete functions defined on the *instance* of a ToolInterface (we want the tool controller to call our class methods instead)
    delete this.register;
    delete this.deregister;
    delete this.activate;
    delete this.deactivate;
    delete this.getPriority;
    delete this.handleSingleClick;
  }

  register() {
      console.log('MultiSelectionTool registered.');
  }

  deregister() {
      console.log('MultiSelectionTool unregistered.');
  }

  activate() {
      if (!this.active) {
          console.log('MultiSelectionTool activated.');
          this.active = true;
      }
  }

  deactivate() {
      if (this.active) {
          this.active = false;
      }
  }

  getPriority() {
      return 99; // Feel free to use any number higher than 0 (which is the priority of all the default viewer tools)
  }

  handleSingleClick(event, button) {
      const currentSelection = this.viewer.getSelection();
      //check if ctrl key is pressed
      if (event.ctrlKey) {
          this.selection.push(...currentSelection)
      }
      else{
        const castResult = this.viewer.clientToWorld(event.canvasX, event.canvasY);
        this.selection = !!castResult?.dbId? [castResult.dbId]: []; // Reset the selection if ctrl key is not pressed
      }
      this.viewer.select(this.selection); 
      //Return true so it doesn't call the default behavior of the viewer (which is to select the object under the mouse cursor)
      return true;
  }
}

class MultiSelectionExtension extends Autodesk.Viewing.Extension {
  constructor(viewer, options) {
    super(viewer, options);
    this.tool = new MultiSelectionTool(viewer);
  }

  async load() {
    const controller = this.viewer.toolController;
    this.viewer.toolController.registerTool(this.tool);
    controller.activateTool(MultiSelectionToolToolName);
    return true;
  }

  unload() {
    const controller = this.viewer.toolController;
    controller.deactivateTool(MultiSelectionToolToolName);
    return true;
  }
}

Autodesk.Viewing.theExtensionManager.registerExtension('MultiSelectionExtension', MultiSelectionExtension);

You can find a live demo at https://joaomartins-callmejohn.github.io/aps-viewer-multiselection/

Source at https://github.com/JoaoMartins-callmeJohn/aps-viewer-multiselection/tree/main

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.