Menus & Tools

Menus & Tools

Postby 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.
0

PaulG 
 

Re: Menus & Tools

Postby 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.
0

Diggsey 
 

Re: Menus & Tools

Postby thomthom » Mon Apr 30, 2012 8:48 am

Code: Select all
#menu file
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
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Menus & Tools

Postby 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?
0

PaulG 
 

Re: Menus & Tools

Postby 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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Menus & Tools

Postby 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?
0

PaulG 
 

Re: Menus & Tools

Postby thomthom » Mon Apr 30, 2012 4:52 pm

Code: Select all
#menu file
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:
autoload
http://www.rubyinside.com/ruby-techniqu ... -1652.html

Code: Select all
#menu file
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
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Menus & Tools

Postby PaulG » Mon Apr 30, 2012 5:45 pm

Thanks thomthom, much appreciated :)
0

PaulG 
 

Re: Menus & Tools

Postby 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!!
0

niccah 
 

Re: Menus & Tools

Postby 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.
0
User avatar
Dan Rathbun 
Top SketchUcator
 

Re: Menus & Tools

Postby 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!
0

niccah 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Artisan Organic Toolset - a set of powerful organic modeling tools.

Premium Members get 20% discount!

Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: marinabermejo, Sketchygo and 6 guests

Visit our sponsors: