• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Menus & Tools

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 5 Posters 296 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    PaulG
    last edited by Gábor 29 Apr 2012, 22:11

    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.

    1 Reply Last reply Reply Quote 0
    • D Offline
      Diggsey
      last edited by 29 Apr 2012, 23:22

      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.

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 30 Apr 2012, 07:48

        <span class="syntaxdefault"><br /></span><span class="syntaxcomment">#menu&nbsp;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&nbsp;RNC_Files<br />&nbsp;&nbsp;&nbsp;menu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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 />&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">submenu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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 />&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">submenu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tool_1"</span><span class="syntaxkeyword">){&nbsp;</span><span class="syntaxdefault">RNC_Files</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">.new&nbsp;}<br /></span><span class="syntaxdefault">end</span>
        

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • P Offline
          PaulG
          last edited by 30 Apr 2012, 08:04

          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?

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 30 Apr 2012, 08:09

            @paulg said:

            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.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • P Offline
              PaulG
              last edited by 30 Apr 2012, 15:04

              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?

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 30 Apr 2012, 15:52

                <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&nbsp;file<br /></span><span class="syntaxdefault">module&nbsp;RNC_Files<br />&nbsp;&nbsp;autoload&nbsp;</span><span class="syntaxkeyword">;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxstring">"RNC_Tools/Tool_1.rb"<br /><br />&nbsp;&nbsp;</span><span class="syntaxdefault">menu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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 />&nbsp;&nbsp;</span><span class="syntaxdefault">submenu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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 />&nbsp;&nbsp;</span><span class="syntaxdefault">submenu&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tool_1"</span><span class="syntaxkeyword">){&nbsp;</span><span class="syntaxdefault">Tool_1</span><span class="syntaxkeyword">.new&nbsp;}<br /></span><span class="syntaxdefault">end<br /></span>
                

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • P Offline
                  PaulG
                  last edited by 30 Apr 2012, 16:45

                  Thanks thomthom, much appreciated 😄

                  1 Reply Last reply Reply Quote 0
                  • N Offline
                    niccah
                    last edited by 6 May 2012, 20:29

                    @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 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!!

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      Dan Rathbun
                      last edited by 7 May 2012, 03:17

                      @niccah said:

                      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.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • N Offline
                        niccah
                        last edited by 7 May 2012, 18:55

                        @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 (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!

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        1 / 1
                        • First post
                          4/11
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement