[code] Example: Add some edit commands to context menu

copy
edit
paste
commands
context_menu

#1

Continuing the discussion from Right click copy/paste:

!Add_context_menu_copypaste.rb (2.1 KB)

Looks like:

# encoding : UTF-8

# A simple example of adding some of the edit menu
#  commands to the right-click mouse context menu.

module User # <--<<< Can be changed to something unique.

  module ContextMenuCopyPasteEtc

    VERSION = '1.0.0'

    WIN = (
      Sketchup.respond_to?(:platform) &&
      Sketchup::platform == :platform_win
    ) || RUBY_PLATFORM !~ /(darwin)/i

    @@loaded ||= false

    unless @@loaded # do this block once:

      # Register a context menu handler to add some edit commands:
      UI.add_context_menu_handler do |popup|

        sel = Sketchup.active_model.selection
        has_selection = !sel.empty?
        if has_selection || Sketchup.active_model.active_path

          if Sketchup.active_model.active_path

            popup.add_item("Close All Edits") {
              sel.clear
              Sketchup.active_model.close_active while Sketchup.active_model.active_path
            }

            popup.add_separator

          end

          popup.add_item("Undo") { Sketchup.send_action("editUndo:") }

          popup.add_item("Redo") { Sketchup.send_action("editRedo:") }

          popup.add_separator

          if has_selection

            popup.add_item("Cut") { Sketchup.send_action("cut:") }

            popup.add_item("Copy") { Sketchup.send_action("copy:") }

          end

          popup.add_item("Paste") { Sketchup.send_action("paste:") }

          popup.add_item("PasteInPlace") {
            Sketchup.send_action( WIN ? 21939 : "pasteInPlace:" )
          }

          if has_selection

            popup.add_item("Delete") { Sketchup.send_action("editDelete:") }

          end

          popup.add_separator

          popup.add_item("Select All") {
            sel.clear
            sel.add(
              Sketchup.active_model.active_entities.grep(Sketchup::Drawingelement)
            )
          }

          if has_selection

            popup.add_item("Select None") { sel.clear }

          end

        end

      end # context menu handler block

      @@loaded = true
      # ... so this block of code will not run again, this session.

    end # run once block of code

  end # specific plugin module
end # outermost namespace module

Also incorporates a simplified “Close All Edits” command.
For a more complex example, see:


Move Command - Hold Copy
Tape Measure Add-in/Extension with copy/paste