Right Click Menu Items


#1

I’ve noticed with some plugins that when you create something within them you can then right click on the object and plugin specific items are shown in that menu.

I would like to do something like that for the truss and foundation plugin where a user can right click and then be shown the option to edit the roof or foundation assembly.

Can someone point me in the right direction for adding this type of functionality.


Truss Plugin/Extension
#2

the API can help, just pad it out…

# Right click on anything  meeting your condition
# to see your outcome item.
UI.add_context_menu_handler do |context_menu|
  # add your conditional
  if Sketchup.active_model.selection[0].name.include?('TRUSS_ASSEMBLY')
    # add a title
    context_menu.add_item("Medeek Truss") {
      # add an outcome
      UI.messagebox("TRUSS_ASSEMBLY")
    }
  end
end

#3

Top item in the API under UI, somehow I missed this… go figure.

So would I add this block of code into my plugin loader so it is activated up each time the plugin is loaded into SketchUp?


#4

the basic concept, add it when you add menu items…

I would also consider making it a reusable method…

def context_menu(title, cmd)
	UI.add_context_menu_handler do |context_menu|
	# add your conditional
	if Sketchup.active_model.selection[0].name.include?(title)
		# add a title
		context_menu.add_item(title) {
			# add an outcome
			cmd
		}
	end
end

context_menu('Medeek_Truss', cmd1)
context_menu('Medeek_Slab', cmd2)

john


#5

NO only the FIRST time it is loaded !

… so the calls to create the context menu loader go inside a conditional block:

# IF you require("sketchup.rb") at top of your files:
unless file_loaded?(File.basename(__FILE__))
  # create menus and context handlers
end

Or, I believe that an extra array of huge absolute filepaths is a waste of memory,
… so I instead create my own @@loaded module var at the top of my modules:

module Author::SomePlugin
  @@loaded ||= false

  # other declarations and definitions

  # require any subordinate ruby files to load

  # run once block
  unless @@loaded
    # create menus and context handlers
    @@loaded = true
  end

end

#6

Or you could just let the menu be duplicated each time you reload the file. I used to make sure that code only ran once because that was what I learned from various code examples. However sometimes you want the menu re re-generated because you’ve changed it and then I prefer not to have to restart SketchUp. Duplicated menus when I test the code doesn’t bother me and the code should only run once in production anyway.


#7

What happens if the user switches off the extension and then switches it back on via Preferences (or Extension Manager) ?

Defensive coding is never a bad thing.


closed #8

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.