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