Menus & Tools
-
Hi,
I have been trying to make a new tool and organize it a bit better, but I can't work out how to get it running. I have a menu rb file in the plugins file which I want to call the tool in it's own folder.
Previously I have had the tool file in the plugins folder with the menu adding code and tool in the same file and used active_model.select_tool(RNC_Files::Tool_1.new()) to get the tool running.#menu file module RNC_Files menu = UI.menu("Plugins") submenu = menu.add_submenu("RNC_Tools") submenu = add_item("Tool_1"){Sketchup.load("RNC_Tools/Tool_1.rb")} end
#tool file module RNC_Files class Tool_1 def initialize UI.messagebox("Tool_1 has been called") end end end
Hopefully you guys can point me in the right direction, maybe I'm taking the wrong approach. Any advise will be greatfully recieved.
-
Just load the tool script once at the start of your main script, and then instantiate your tool in the UI handler the same way you were doing before.
-
<span class="syntaxdefault"><br /></span><span class="syntaxcomment">#menu file<br /></span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.require(</span><span class="syntaxstring">"RNC_Tools/Tool_1.rb"</span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault">module RNC_Files<br /> 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">submenu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"RNC_Tools"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">submenu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tool_1"</span><span class="syntaxkeyword">){ </span><span class="syntaxdefault">RNC_Files</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">.new }<br /></span><span class="syntaxdefault">end</span>
-
Thanks for that,the require method includes code from the tool file into the menu file same as require sketchup.rb? Is there a practical limit on the number of files you can require?
-
@paulg said:
Thanks for that,the require method includes code from the tool file into the menu file same as require sketchup.rb?
require
andload
will both make the ruby interpreter load and execute the content of the file you specify. You must use this when you need to use another file.require
if different fromload
in that it ensure the file is loaded only once. I recommend you read up on these methods as they are essential to Ruby. -
Ok thanks, so require is basically like including a header file, so if I have 20 menuitems calling 20 different tools in my menu I will need to require 20 files, this seems like im including code I wont need if my user only uses 1 tool. Is there a more efficiant approach to this?
-
<span class="syntaxdefault"><br /></span><span class="syntaxcomment">#menu file<br /></span><span class="syntaxdefault">module RNC_Files<br /> 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"> submenu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"RNC_Tools"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> submenu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tool_1"</span><span class="syntaxkeyword">){<br /></span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.require(</span><span class="syntaxstring">"RNC_Tools/Tool_1.rb"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> RNC_Files</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">end<br /></span>
I guess that will work - not sure if it's any good practice for lazy loading...
Just remembered - as I was typing:
autoload
http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html<span class="syntaxdefault"><br /></span><span class="syntaxcomment">#menu file<br /></span><span class="syntaxdefault">module RNC_Files<br /> autoload </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"RNC_Tools/Tool_1.rb"<br /><br /> </span><span class="syntaxdefault">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">submenu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"RNC_Tools"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">submenu </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tool_1"</span><span class="syntaxkeyword">){ </span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">.new }<br /></span><span class="syntaxdefault">end<br /></span>
-
Thanks thomthom, much appreciated
-
@thomthom said:
@paulg said:
Thanks for that,the require method includes code from the tool file into the menu file same as require sketchup.rb?
require
andload
will both make the ruby interpreter load and execute the content of the file you specify. You must use this when you need to use another file.require
if different fromload
in that it ensure the file is loaded only once. I recommend you read up on these methods as they are essential to Ruby.I had a similar problem: I could start a plugin in a folder just once. At a second click, nothing started. So, with your help, I could fix it! (I replaced "require" by "load").
Just a small question: Why you are writing "Sketchup.require"? Are there any differences to the "normal" require?
Thanks Thomthom for your helpful explanations!!
-
@niccah said:
Just a small question: Why you are writing "
Sketchup.require
"? Are there any differences to the "normal"require
?FYI: The "normal"
require
(andload
,) comes from moduleKernel
.Sketchup.require
andSketchup.load
can call the internal decryption for rbs files, but theKernel
ones cannot. -
@dan rathbun said:
@niccah said:
Just a small question: Why you are writing "
Sketchup.require
"? Are there any differences to the "normal"require
?FYI: The "normal"
require
(andload
,) comes from moduleKernel
.Sketchup.require
andSketchup.load
can call the internal decryption for rbs files, but theKernel
ones cannot.Okay, this sounds logic! Thanks for the information!
Advertisement