Tool: getMenu and toolbar
-
A new trip is firing me... In a tool (alongside the activate, deactivate, draw etc methods) there's the getMenu one. But it is intended to work with contextmenu. I'm trying to implement it in order to get a floating toolbar and it works but only the first time! If I select another tool and the reselect my Tool it doesn't work anymore.... I'm going crazy!!!
def creaTOOLBAR(view) puts view @tbTOOL1= UI;;Toolbar.new("Tool1") @cmdTOOL1= UI;;Command.new("Tool1") {tool1("Right", view)} @cmdTOOL1.large_icon = Sketchup;;find_support_file("muroDX_40x40.png", "Plugins/TECLA_STRUCTURE/icone/") if @tbTOOL1.length==0 #evita di aggiungere continuamente le 3 icone alla toolbar @tbTOOL1.add_item @cmdTOOL1 end # view.invalidate # @tbTOOL1.show() if !@tbTOOL1.visible? end def tool1(side, view) @side = side # view.invalidate # IF ENABLED HERE I GET; Error; #<NoMethodError; undefined method `invalidate' for nil;NilClass> # IF DISABLED NOTHING HAPPENS end
-
Do not create a new toolbar and command object each time the method gets called.
Instead create the toolbar and command at the top of the class defintion, and reference them with class variables.
module BomaStudio module CustomTool class Tool if !defined?(@@tbTOOL1) @@tbTOOL1 = UI;;Toolbar.new("Toolbar 1") @@cmdTOOL1 = UI;;Command.new("Tool 1") { tool1("Right") } @@cmdTOOL1.large_icon = Sketchup;;find_support_file( "muroDX_40x40.png", "Plugins/TECLA_STRUCTURE/icone/" ) @@tbTOOL1.add_item @@cmdTOOL1 end def getMenu(menu) @view = Sketchup.active_model.active_view @@tbTOOL1.show() if !@@tbTOOL1.visible? end def tool1( side ) @@tbTOOL1.hide() if @@tbTOOL1.visible? @side = side @view.invalidate end end # tool @@tool = Tool;;new if !defined?(@@tool) end end
-
Yes it do the work!! But only after I changed all my class variables to module variables (I can't understand the reason...).
Dan you are a very amazingly competent developer. I can only whish to reach in the future a half of your skills!!!!
P.S.: I saw that you nested in the code of your post two modules:
module BomaStudio
and
module CustomTool
. Is there a specific reason to do so?
-
@bomastudio said:
P.S.: I saw that you nested in the code of your post two modules:
module BomaStudio
and
module CustomTool
. Is there a specific reason to do so?
Yes, ALL your code needs to be within a toplevel author (or company) module.
Each one of your plugins need to be within a sub-module of your toplevel module so they do not clash with each other.
Class definitions used by only one plugin should be defined within that plugin sub-module.If you define variables and methods at the toplevel (ie,
TOPLEVEL_BINDING
,) they are defined insideObject
. EVERYTHING in Ruby is a subclass ofObject
, so toplevel objects get inherited by everyone else's modules and classes.
That makes us angry. -
Good. So suppose that I have:
- myGUI.html
- myPLUGIN.rb
- myTool.rb (that have the nested modules: BomaStudio and BomaTool)
From myPlugin.rb I want to show myGUI.html, set some values and than run myTool.rb.
Inside the myPlugin.rb file I have to embed all code inside the BomaStudio modules? -
Your files need to be set up like:
http://extensions.sketchup.com/en/developer#rbzSo the extension registrar file in "Plugins" would be "BomaStudio_BomaTool.rb", and your plugin sub-folder would have the same name: "BomaStudio_BomaTool".
The loading file in the "Plugins/BomaStudio_BomaTool" sub-directory can be whatever name you like:
"BomaTool_loader.rb", etc., and the other files should likely have the same prefix with descriptive suffix, like:
"BomaTool_core.rb"
"BomaTool_gui.rb"
"BomaTool_tool.rb"
"BomaTool_menu.rb"
In Ruby, the
module
andclass
keywords, mean "create if not defined, and open for editing".So,
module BomaStudio # make changes like define constants, variables, new methods, or # redefine old methods (method overrides,) or undefine methods. end
is the same as:
BomaStudio = Module;;new if !defined?(BomaStudio) BomaStudio.module_eval { # make changes like define constants, variables, new methods, or # redefine old methods (method overrides,) or undefine methods. }
See also the
SketchupExtension
class:[url=http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchupextension:3qzf850n]http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchupextension[/url:3qzf850n]
-
@bomastudio said:
Inside the myPlugin.rb file I have to embed all code inside the BomaStudio modules?
Inside ALL of your Ruby files.
Files do not separate code into different scopes. It is
module
namespaces that separate code.Any number of ruby files can open the same module or class and modify it.
The HTML file is special because it only runs in the
UI::WebDialog
.
These subjects have already been covered numerous times here in the forum. It is basic Ruby, and covered in any Ruby textbook. I posted an old version of the "PickAxe" Ruby book:
[doc] Programming Ruby (The "Pick-Axe" Book) -
Thank you Dan. I'm going to study....
Advertisement