The API doesn’t look up existing menus when you call menu.add_menu - it blindly does what it says on the tin - it adds.
That being said, if you stored your menu references in an instance variables you might achieve this.
require 'sketchup.rb'
module NN_MyOwnUniqueNamespace
def self.get_menu
# This will create the menu on demand.
@submenu ||= menu.add_submenu("AAA_xxx")
@submenu
end
module Awesome_A
unless file_loaded?( __FILE__ )
menu = UI.menu( 'Plugins' )
submenu = NN_MyOwnUniqueNamespace.get_menu
aaasubmenu = submenu.add_submenu("AAA")
aaasubmenu.add_item( 'a1' ) { self.hello_world }
aaasubmenu.add_item( 'a2' ) { self.hello_world }
end
file_loaded( __FILE__ )
end # module Awesome_A
end # module NN_MyOwnUniqueNamespace
Then in your other extension:
require 'sketchup.rb'
module NN_MyOwnUniqueNamespace
def self.get_menu
# This will create the menu on demand.
@submenu ||= menu.add_submenu("AAA_xxx")
@submenu
end
module Awesome_B
unless file_loaded?( __FILE__ )
menu = UI.menu( 'Plugins' )
submenu = NN_MyOwnUniqueNamespace.get_menu
bbbsubmenu = submenu.add_submenu("BBB")
bbbsubmenu.add_item( 'b1' ) { self.hello_world }
bbbsubmenu.add_item( 'b2' ) { self.hello_world }
end
file_loaded( __FILE__ )
end # module Awesome_B
end # module NN_MyOwnUniqueNamespace
But note that this only works if they both load at startup. If you have one of them installed, and the user install the other afterwards the menus won’t be created because the menu item has become stale.
def self.get_menu
# This will create the menu on demand.
@submenu ||= menu.add_submenu("AAA_xxx")
@submenu
end
The last @submenu is there to ensure the method return the menu object. Ruby has implicit returns, where the value of the last statement automatically returns.