Trying to access right click menu with no selection

I’m trying to add a new menu item to the context menu that appears when there is no selection in the viewport. I say ‘context menu’ with hesitation because it is apparently not referred to as a context menu in sketchup-ruby-land when there is no selection. I’m using add_context_menu_handler, but that only appears to work when there is a selection. Not when there is no selection.

Here is my code - I’m not sure how to paste code effectively so please excuse this formatting:

  module RtClick
    if !defined?(@ui_loaded)
      UI.add_context_menu_handler do |menu|
        item = menu.add_item("It's Empty") {
          UI.messagebox('Really Empty!')
        }
      end
    end
  end

context menu

The result of this code, is that the menu only appears when there is an active selection. Not when the selection is empty.

Another thing I find odd is that even if I try to hide the menu item in the context menu using

if model.selection.empty?

the menu item exists as long as there is a selection.

Well, we do call it a context menu.

But yes, there is no explicit context to commands as nothing is selected to give them context.

I have already reported this. I previously logged this as Issue 809 in API Issue Tracker.

There actually is what we can call a “implicit model context” when there is no selection and that this implicit context is the top-level model entities collection.

Evidence that the Invert Selection command actually becomes a Select All command and selects everything at the top-level of the model.

I can’t remember offhand what it was I thought would be nifty on such a no selection context menu. (I’ll sleep on it.)

It actually does not have a conditional to ensure it shows when the selection is empty. I.e.:

  module RtClick
    if !defined?(@ui_loaded)
      UI.add_context_menu_handler do |menu|
        if Sketchup.active_model.selection.empty?
          menu.add_item("It's Empty") {
            UI.messagebox('Really Empty!')
          }
        end
      end
    end
  end

But it still does not appear when the selection is empty.

Weirdly it does with a right-click on any axis.

1 Like

The first thing to ask is, could there be a better place for your command ?

What kind of command is it? Does it belong on one of the main application menus?

If you create a create a custom tool, you can achieve something like that using the #getMenu method, e.g.:

If you try this with or without a preselection, the context menu will appear accordingly

class TestToolMenuA
  def getMenu(menu, flags, x, y, view)
    if view.model.selection.empty?
      menu.add_item("It's Empty") {
        UI.messagebox("Really Empty!")
      }
    else
      menu.add_item("Not Empty") {
        UI.messagebox("Not Empty!")
      }
    end
   end
end
Sketchup.active_model.select_tool(TestToolMenuA.new)

or an other idea (like in a getMenu example) were the menu depends on if there is an entity under the cursor :

class TestToolMenuB
  def getMenu(menu, flags, x, y, view)
    ph = view.pick_helper(x, y)
    entity = ph.best_picked
    unless entity
      menu.add_item("It's Empty") {
        UI.messagebox("Really Empty!")
      }
     end
   end
end
Sketchup.active_model.select_tool(TestToolMenuB.new)

__
See also : the #onRButtonDown method.

2 Likes

Thanks for the help everyone. It looks like I stripped out too much code when I was making a simplified version of my little script.

This is my first time using ruby and while I’ve done a bit of coding and scripting plugins for other apps, it’s been a long time. But it’s been fun starting to make custom tools for sketchup, which I’m just getting proficient enough in to realize where it needs speeding up and streamlining.

Maybe one day, Trimble will let us execute Layout scripts from within Layout. I know it’s a novel idea, but a boy can hope. That program is a mess and is missing a LOT of functionality; even almost unusable in some cases (IE trying to place line point any where near the center point of an image or group. You might as well give up in this case.)

With regard to a 2019 release that accidentally had it’s Ruby Console still activated …

… so, a “live” LayOut API is on the roadmap.

And, alas, has remained there for four years!