Bulletproof sample code for a plugins menu ???
-
It makes a nice menu, yeah, but it don't use my file structure where the individual tools are placed in individual folders.
-
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
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 ?
-
No, the 'etc' means add extra menu items as desired...
The...{KSORtools.FirstTool()}
runs the module's class toolAs 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 theinitialize(args)
method as 'args'... -
???
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.
-
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 #module
and 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 -
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........."
-
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
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 !
-
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...
Advertisement