sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Bulletproof sample code for a plugins menu ???

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 265 Views 3 Watching
    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.
    • sdmitchS Offline
      sdmitch
      last edited by

      if !file_loaded?(File.basename(__FILE__))
       sub=UI.menu("Plugins").add_submenu("My Tools")
       sub.add_item('My First Tool')   {Sketchup.active_model.select_tool MyFirstTool.new } #for class
       sub.add_item('My Second Tool')   {MyTools.MySecondTool} #for module
       file_loaded(File.basename(__FILE__))
      end
      
      

      Nothing is worthless, it can always be used as a bad example.

      http://sdmitch.blogspot.com/

      1 Reply Last reply Reply Quote 0
      • K Offline
        ksor
        last edited by

        It makes a nice menu, yeah, but it don't use my file structure where the individual tools are placed in individual folders.

        Best regards
        KSor, Denmark
        Skype: keldsor

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          I think what you are saying is...
          "How to make a single submenu that contains different tools that each load from their own files..."
          πŸ˜•
          Here's one way using a 'global' reference - but potential to clash with other scripts using the same name - although 'yours' is unlikely to ??
          Make a script called !submenumaker.rb that loads early that contains the line
          $KSORsubmenu=UI.menu("Plugins").add_submenu("KSOR Tools...")unless $KSORsubmenu
          Then in each file that you want loading into the submenu use: $KSORsubmenu.add_item('First Tool'){KSORtools.FirstTool()} etc...

          However, if it's just for your own scripts then placing all of your scripts' code within a same name module in each file, e.g. module KSORtools... end and then setting up the submenu reference inside the module is the best...
          e.g. @submenu=UI.menu("Plugins").add_submenu("KSOR Tools...")unless @submenu
          which gives you a reference to the submenu specific to that module, named @submenu
          So in any of your tools using the same module within their file's code you'll have access to that submenu thus: @submenu.add_item('Second Tool'){KSORtools.SecondTool()} etc...

          TIG

          1 Reply Last reply Reply Quote 0
          • K Offline
            ksor
            last edited by

            TIG

            I think your first solution best fits my needs because it's more flexiable and a new tool can "install" itself in the menu (assumed the "!submenumaker.rb" is loaded !) in best OOP practice BUT ...

            I think your "etc..." in each file holds the lines for triggering/starting up the tool in that particular file - how should these lines look like ?

            Best regards
            KSor, Denmark
            Skype: keldsor

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              No, the 'etc' means add extra menu items as desired...
              The ...{KSORtools.FirstTool()} runs the module's class tool

              As was explained... if it's a Sketchup 'Tool' using the various extra methods that gives you, then you need something like this:
              ...{Sketchup.active_model.select_tool(MySketchupTool.new())}
              or
              ...{Sketchup.active_model.select_tool(KSORtools.MySketchupTool.new())}

              The empty () can be used to pass arguments to the class, which are received by the initialize(args) method as 'args'...

              TIG

              1 Reply Last reply Reply Quote 0
              • K Offline
                ksor
                last edited by

                ???
                I think you write about TWO different files here:

                "Make a script called !submenumaker.rb that loads early that contains the line
                $KSORsubmenu=UI.menu("Plugins").add_submenu("KSOR Tools...")unless $KSORsubmenu"

                this is the first one - making the "Plugins / KSOR Tools" menu structure, and

                "Then in each file that you want loading into the submenu use: $KSORsubmenu.add_item('First Tool'){KSORtools.FirstTool()} etc..."

                this is the next file - you write 'in each file ... use: $KSORsubmenu.add_item('First Tool'){KSORtools.FirstTool()} etc...' !
                and from you last posting: "No, the 'etc' means add extra menu items as desired..."

                Why should each individual tool file make a menu item for MORE than itself - I simply don't get you !

                The "wheel" must have been invented - I'm sure I'm NOT the only one wishing to see a SIMPLE AND EASY sample code around the issue "Making your individial tool files run from a submenu to 'Plugins'" - as close to OOP principles as possible = each individual tool file should install itself in the 'Plugins' (or a sub to it !) menu - NO (or as few as possible !) dependencies between the individual files !

                Why is it so hard to get a "standard" or "best practice" here ?

                You hard-liners should be able to make a SIMPLE and EASY working sample in minutes instead of all this smokescreen and misunderstandings.

                Best regards
                KSor, Denmark
                Skype: keldsor

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  You misunderstand.

                  It makes a reference to a new submenu - either as a global $ or module/class @ then you need only make it once [my example uses a 'unless...' for to ensure it's only added once if you make the reference in more than one script].

                  After the reference is made you use it to add commands/tools/methods to your scripts that put items in the submenu.

                  Write three scripts.
                  One called !KSOR.rb that has the lines
                  module KSORtest @KSORsubmenu=UI.menu("Plugins").add_submenu("KSOR Test...") end
                  The others called
                  KSORhello.rb
                  containing a 'class'
                  ` module KSORtest
                  self::Hello # class
                  def initialize() #class's 'new' method
                  UI.messagebox("Hello.")
                  end #method
                  ### etc etc ***
                  end #class
                  @KSORsubmenu.add("Hello"){self::Hello.new()}

                  *** could be a Sketchup Tool with extra inherited methods, then you'd use

                  @KSORsubmenu.add**_item**("HelloTool"){Sketchup.active_model.select_tool(self::Hello.new())}

                  end #moduleand a 'module' method KSORgoodbye.rbcontaining module KSORtest
                  def self.goodbye()
                  UI.messagebox("Goodbye.")
                  end #method
                  @KSORsubmenu.add**_item**("Goodbye"){self.goodbye()}
                  end #moduleRestart Sketchup and try the new submenu items... You can also run these 'tools' using the Ruby Console... KSORtest.Hello.new()
                  KSORtest.goodbye()Thus separate files make their own commands which can be collected into one combined submenu - in tis case the @KSORsubmenuthat only applies to scripts using the same module. Substituting a $KSORsubmenu` making method in an early loading file makes the submenu accessible to ALL scripts, but has the disadvantage that it might be changed by other's scripts too - although 'KSORsubmenu' is an unusual name !

                  I haven't tested this example code but if there are typos they ought to be obvious.........
                  EDIT: typos corrected - TIG

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • K Offline
                    ksor
                    last edited by

                    It would be better with a link to a WORKING code about this special point alone !

                    People should never accuse you for being a good teacher πŸ˜„ - and what makes this absolutely true is your last comment:

                    ".... if there are typos they ought to be obvious........."

                    Best regards
                    KSor, Denmark
                    Skype: keldsor

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      IF you tried making these three scripts you might find they do work !
                      IF they don't the Ruby Console WILL provide errors to explain why they failed == my typos!

                      These are 'real' scripts - they make a submenu and add two tools that do something.
                      I even threw in the class [with a Sketchup-Tool class option] and a module method code example for you enlightenment...

                      I have not yet said that you ought to download a few scripts and read their contents...
                      Several of mine make a 'shared submenu' that's then used by different scripts, as do thomthom's - and others...
                      I have taken some time to try and explain to you the core principles of doing this without you taking the effort to extract the relevant code from other scripts.
                      If you can't just try the three simple examples, then what more can I do? 😒
                      πŸ˜’

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • K Offline
                        ksor
                        last edited by

                        TIG
                        We are not able to communicate with each other - I'm not exspecting any answers, but I think you should know:

                        You wrote:
                        "IF you tried making these three scripts you might find they do work !
                        IF they don't the Ruby Console WILL provide errors to explain why they failed == my typos!"

                        and here is what's comming up when starting SU:

                        Error Loading File KSORgoodbye.rb
                        undefined method `add' for #Sketchup::Menu:0x33a2430Error Loading File KSORhello.rb
                        C:/Program Files/Google/Google SketchUp 8/Plugins/KSORhello.rb:11: syntax error, unexpected kEND, expecting $end
                        end #module
                        ^

                        That's why people should not accuse you af being a good teacher.

                        You WILL or CAN not imaging what's comming up in your students minds - but we can not be expected to provide more than our abilities permit - that goes for both teacher AND student !

                        Best regards
                        KSor, Denmark
                        Skype: keldsor

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          Now you understand how to debug! 😲

                          Missing _item ...
                          ...@KSORsubmenu.add**_item**...

                          My typo - which occurs twice when the item is added to the submenu [or it wasn't thanks to the typing error πŸ˜‰ ]
                          Edit those two 'add' lines in your script and it should then work.
                          I have edited the original post so other browsers won't get confused...

                          TIG

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

                          Advertisement