by PaulG » Sun Apr 29, 2012 11:11 pm
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. - Code: Select all
#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
- Code: Select all
#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.
-
PaulG
-
- Posts: 9
- Joined: Thu May 05, 2011 9:54 am
- Name: Paul
by Diggsey » Mon Apr 30, 2012 12:22 am
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.
-
Diggsey
-
- Posts: 37
- Joined: Tue Aug 11, 2009 7:46 pm
- Name: Diggory James Joshua Blake
by thomthom » Mon Apr 30, 2012 8:48 am
- Code: Select all
Sketchup.require("RNC_Tools/Tool_1.rb")
module RNC_Files menu = UI.menu("Plugins") submenu = menu.add_submenu("RNC_Tools") submenu = add_item("Tool_1"){ RNC_Files::Tool_1.new } end
-

thomthom
- Global Moderator
-
- Posts: 17603
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by PaulG » Mon Apr 30, 2012 9:04 am
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
-
- Posts: 9
- Joined: Thu May 05, 2011 9:54 am
- Name: Paul
by thomthom » Mon Apr 30, 2012 9:09 am
PaulG wrote:Thanks for that,the require method includes code from the tool file into the menu file same as require sketchup.rb? require and load 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 from load in that it ensure the file is loaded only once. I recommend you read up on these methods as they are essential to Ruby.
-

thomthom
- Global Moderator
-
- Posts: 17603
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by PaulG » Mon Apr 30, 2012 4:04 pm
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?
-
PaulG
-
- Posts: 9
- Joined: Thu May 05, 2011 9:54 am
- Name: Paul
by thomthom » Mon Apr 30, 2012 4:52 pm
- Code: Select all
module RNC_Files menu = UI.menu("Plugins") submenu = menu.add_submenu("RNC_Tools") submenu = add_item("Tool_1"){ Sketchup.require("RNC_Tools/Tool_1.rb") RNC_Files::Tool_1.new } end
I guess that will work - not sure if it's any good practice for lazy loading... Just remembered - as I was typing: autoloadhttp://www.rubyinside.com/ruby-techniqu ... -1652.html- Code: Select all
module RNC_Files autoload :Tool_1, "RNC_Tools/Tool_1.rb"
menu = UI.menu("Plugins") submenu = menu.add_submenu("RNC_Tools") submenu = add_item("Tool_1"){ Tool_1.new } end
-

thomthom
- Global Moderator
-
- Posts: 17603
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by PaulG » Mon Apr 30, 2012 5:45 pm
Thanks thomthom, much appreciated 
-
PaulG
-
- Posts: 9
- Joined: Thu May 05, 2011 9:54 am
- Name: Paul
by niccah » Sun May 06, 2012 9:29 pm
thomthom wrote:PaulG wrote:Thanks for that,the require method includes code from the tool file into the menu file same as require sketchup.rb? require and load 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 from load 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
-
- Posts: 88
- Joined: Tue Jan 03, 2012 8:26 pm
- Name: niccah
by Dan Rathbun » Mon May 07, 2012 4:17 am
niccah wrote:Just a small question: Why you are writing "Sketchup.require"? Are there any differences to the "normal" require?
FYI: The "normal" require (and load,) comes from module Kernel. Sketchup.require and Sketchup.load can call the internal decryption for rbs files, but the Kernel ones cannot.
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4074
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by niccah » Mon May 07, 2012 7:55 pm
Dan Rathbun wrote:niccah wrote:Just a small question: Why you are writing "Sketchup.require"? Are there any differences to the "normal" require?
FYI: The "normal" require (and load,) comes from module Kernel. Sketchup.require and Sketchup.load can call the internal decryption for rbs files, but the Kernel ones cannot.
Okay, this sounds logic! Thanks for the information!
-
niccah
-
- Posts: 88
- Joined: Tue Jan 03, 2012 8:26 pm
- Name: niccah
Return to Developers' Forum
|