Add items to submenu later on
-
It seems that Sketchup doesn't 'remeber' its menu's...
For instance:
class Test def self.initialize plugins_menu = UI.menu('Plugins') @@my_menu = plugins_menu.add_submenu("mySubmenu") end#def def self.my_menu @@my_menu end#def end#class
From ruby console
my_item = Test.my_menu.add_item('myItem'){UI.messagebox "ok"}
Well, this doesn't work
I notices that every time i entered this in the console:
UI.menu("Plugins")
, the handler to the menu changes...
Is there any possibility to add items to a submenu long after the submenu has been created?
-
I can't make it work either.
-
Btw, nothing that will affect the result of the menu thing, but your sample code would make more sense as a Module:
<span class="syntaxdefault"><br />module Test<br /><br /> plugins_menu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Plugins'</span><span class="syntaxkeyword">)<br /> @@</span><span class="syntaxdefault">my_menu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">plugins_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"mySubmenu"</span><span class="syntaxkeyword">)<br /><br /> </span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">my_menu<br /> </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">my_menu<br /> end</span><span class="syntaxcomment">#def<br /><br /></span><span class="syntaxdefault">end<br /></span>
When you don't create an instance of a class, use a module.
-
@thomthom said:
Btw, nothing that will affect the result of the menu thing, but your sample code would make more sense as a Module:
<span class="syntaxdefault"><br />module Test<br /><br /> plugins_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Plugins'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">my_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> plugins_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"mySubmenu"</span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">my_menu<br /> </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">my_menu<br /> end</span><span class="syntaxcomment">#def<br /><br /></span><span class="syntaxdefault">end<br /></span>
When you don't create an instance of a class, use a module.
Ok. Is there also a reason why I shouldn't use Classes when I don't need instances? I mean does it affect something?
It also seems that:
u1 = UI.menu('Plugins'); u2 = UI.menu('Plugins'); u1==u2 ==> false
-
@sr20vet said:
Ok. Is there also a reason why I shouldn't use Classes when I don't need instances? I mean does it affect something?
It makes the intent of the code clearer. Classes indicate an object you create instances of. A module is more of a namespace wrapper.
@sr20vet said:
It also seems that:
u1 = UI.menu('Plugins'); u2 = UI.menu('Plugins'); u1==u2 > ==> false
Yea, you get a different ruby object each time. And it seem to go out of scope quickly.
-
@thomthom said:
Yea, you get a different ruby object each time. And it seem to go out of scope quickly.
They still exist in ObjectSpace though:
#ON STARTUP; menus = []; ObjectSpace.each_object Sketchup;;Menu do |obj|; menus << obj; end; menus #RESULT; [#<Sketchup;;Menu;0x99faea8>, #<Sketchup;;Menu;0xd419134>, #<Sketchup;;Menu;0xd41915c>, #<Sketchup;;Menu;0xd419274>, #<Sketchup;;Menu;0xd57f014>, #<Sketchup;;Menu;0xd57f6cc>, #<Sketchup;;Menu;0xd58219c>, #<Sketchup;;Menu;0xd583e70>, #<Sketchup;;Menu;0xd584474>, #<Sketchup;;Menu;0xd584c6c>, #<Sketchup;;Menu;0xd585a04>, #<Sketchup;;Menu;0xd585d74>, #<Sketchup;;Menu;0xd587494>, #<Sketchup;;Menu;0xd5879a8>, #<Sketchup;;Menu;0xd587a0c>, #<Sketchup;;Menu;0xd5881c8>, #<Sketchup;;Menu;0xd5881f0>, #<Sketchup;;Menu;0xd613da4>, #<Sketchup;;Menu;0xd62fd60>, #<Sketchup;;Menu;0xd62ff54>, #<Sketchup;;Menu;0xd632650>, #<Sketchup;;Menu;0xd6427f8>, #<Sketchup;;Menu;0xd6936bc>, #<Sketchup;;Menu;0xd695728>, #<Sketchup;;Menu;0xd737a3c>, #<Sketchup;;Menu;0xd7380a4>, #<Sketchup;;Menu;0x15ac140c>, #<Sketchup;;Menu;0x15b4de84>, #<Sketchup;;Menu;0x15b4deac>, #<Sketchup;;Menu;0x15b9ef64>, #<Sketchup;;Menu;0x15c87ebc>, #<Sketchup;;Menu;0x15c87f34>, #<Sketchup;;Menu;0x15cb6744>] #NAVIGATE TO PLUGINS MENU; plugins_menu = UI.menu('plugins') #RESULT #<Sketchup;;Menu;0x15a3004c> #SEARCH OBJECTSPACE AGAIN menus = []; ObjectSpace.each_object Sketchup;;Menu do |obj|; menus << obj; end; menus #RESULT [#<Sketchup;;Menu;0x99faea8>, #<Sketchup;;Menu;0xd419134>, #<Sketchup;;Menu;0xd41915c>, #<Sketchup;;Menu;0xd419274>, #<Sketchup;;Menu;0xd57f014>, #<Sketchup;;Menu;0xd57f6cc>, #<Sketchup;;Menu;0xd58219c>, #<Sketchup;;Menu;0xd583e70>, #<Sketchup;;Menu;0xd584474>, #<Sketchup;;Menu;0xd584c6c>, #<Sketchup;;Menu;0xd585a04>, #<Sketchup;;Menu;0xd585d74>, #<Sketchup;;Menu;0xd587494>, #<Sketchup;;Menu;0xd5879a8>, #<Sketchup;;Menu;0xd587a0c>, #<Sketchup;;Menu;0xd5881c8>, #<Sketchup;;Menu;0xd5881f0>, #<Sketchup;;Menu;0xd613da4>, #<Sketchup;;Menu;0xd62fd60>, #<Sketchup;;Menu;0xd62ff54>, #<Sketchup;;Menu;0xd632650>, #<Sketchup;;Menu;0xd6427f8>, #<Sketchup;;Menu;0xd6936bc>, #<Sketchup;;Menu;0xd695728>, #<Sketchup;;Menu;0xd737a3c>, #<Sketchup;;Menu;0xd7380a4>, [b]#<Sketchup;;Menu;0x15a3004c>[/b], #<Sketchup;;Menu;0x15ac140c>, #<Sketchup;;Menu;0x15b4de84>, #<Sketchup;;Menu;0x15b4deac>, #<Sketchup;;Menu;0x15b9ef64>, #<Sketchup;;Menu;0x15c87ebc>, #<Sketchup;;Menu;0x15c87f34>, #<Sketchup;;Menu;0x15cb6744>]
-
Please (and I know I have keep scolded on this subject ...,) do not change Ruby Core classes and modules.
Test
IS a Ruby Core module (that ALSO has SketchUp API extensions added into it.)The general rule is, that there is NEVER any good reason for YOU to write code FOR YOUR OWN USE, that is not wrapped within YOUR OWN UNIQUE MODULE NAMESPACE.
So following that rule ... use
VET::Test
as your OWN custom test module.ALSO.. do not override the global method
test()
within theTOPLEVEL_BINDING
as it will override it for EVERYONE else's modules and classes.
Instead, if you wish to override thetest()
method, do it locally within one of YOUR OWN module or class namespaces. -
@dan rathbun said:
Please (and I know I have keep scolded on this subject ...,) do not change Ruby Core classes and modules.
Test
IS a Ruby Core module (that ALSO has SketchUp API extensions added into it.)The general rule is, that there is NEVER any good reason for YOU to write code FOR YOUR OWN USE, that is not wrapped within YOUR OWN UNIQUE MODULE NAMESPACE.
So following that rule ... use
VET::Test
as your OWN custom test module.ALSO.. do not override the global method
test()
within theTOPLEVEL_BINDING
as it will override it for EVERYONE else's modules and classes.
Instead, if you wish to override thetest()
method, do it locally within one of YOUR OWN module or class namespaces.Don't worry. I use my own namespaces
Was for illustrating purposes. -
@sr20vet said:
Don't worry. I use my own namespaces. Was for illustrating purposes.
@thomthom said:
And it seem to go out of scope quickly.
Yes it appears that the C++ side garbage collection is cleaning up the references on it's side, leaving our Ruby references orphaned.
At one time I thought I had this working but now I cannot get it to work.
Is this a PC bug or is it also bugged on the Mac ?
-
It seems that the references to submenu objects last a bit longer than references to "topmenu" objects.
Re: [API] Menu.add_item( caption, index )
BUT.. how much longer ? ... and why ?
Is it a block scope issue ?
-
The two main problems we have:
(1) Menu object references on the C++ side are garbage collected quite quickly, and the references on the Ruby-side become invalid.
We can live with having to recall
UI.menu()
to get new valid references to the 9 top level menus, but ...(2) the menu#add_submenu method should return a new valid reference to the submenu IF IT ALREADY EXISTS, or create the submenu if it does not.
Currently it just creates a duplicate submenu.
I am sure that this issue has already been logged.
I so much hope that SketchUp v2013 will fix this (if no more updates to v8 will be released.)
So.. for now... once the startup cycle is complete, we cannot add items to ANY of the previously created submenus.
Advertisement