Exit plugin from within a function
-
Hi
I was trying to make things elegant, but it seems that I am trying too hard.
In my plugin definiiton I try to use only functions calls, and to write all definitions in a library.def initialize() sel=Sketchup.active_model.selection SPP::test_start SPP::check_selection(sel) SPP::test_end end
So first function returns messagebox , and the second one checks if anything is selected.
If not I want to have a error message ( got it ) and then quit the whole tool.
But somehow the third function also executes returning a message` def SPP::test_start() # display initialisation message
UI.messagebox "initialised"
enddef SPP::test_end() # display finishing message
UI.messagebox "finished"
enddef SPP::check_selection(selection)
if selection.length >0
else
UI.messagebox "Nothing selected ! Aborting"
exit # HOW TO EXIT PLUGIN ?
end
end`How to solve it ? Is it possible ?
Or do I have to check it within a main definition of a plugin ?Cheers Matt
-
(1) These are methods not functions. In Ruby there are only a few functions, they are normally global, named the same as a class, and are used to convert from the argument's class, into the class of the function's name.
Ex:
n = Float(23)
Creates a newFloat
instance object,n
, which== 23.0
, the same as if you used:n = 23.to_f
-
(2) You do not really ENTER nor EXIT from a plugin.
In an embedded scripting shell of a GUI application (like SketchUp or DoubleCAD,) the code is written in an EVENT DRIVEN structure.
The code's methods get called by the application engine, in response to events from the user (such as clicking a menu item, clicking a toolbar button, selecting objects in the application's client drawing area, etc.)
The code, which should be wrapped in YOUR toplevel author module, and then within a plugin sub-module, ... gets loaded into memory, and then waits for the app engine to call it's "callback" methods, when the user does something.
If this is your first plugin.. first start by creating an item in the popup context menu.
See: UI::add_context_menu_handler
Example:
require('sketchup.rb') module MattC module SPP class << self ### define plugin methods here end #{# RUN ONCE WHEN FIRST LOADED # unless file_loaded?(File.basename(__FILE__)) # UI.add_context_menu_handler {|popup| sel = Sketchup.active_model.selection unless sel.empty? popup.add_separator popup.add_item("Selection Count") { if sel.single_item? UI.messagebox("1 item is selected.") else UI.messagebox("#{sel.count} items are selected.") end } end } # file_loaded(File.basename(__FILE__)) # end # #}# end # module SPP end # module MattC
(3) Users do not want to be bothered with messageboxes that say "Nothing is selected"... instead use the
Selection
class'empty?
method to not show popup menu items, OR grayout toolbar buttons and dropdown menu items.Meaning the user only has the option to perform your plugin's feature, when the selection is NOT empty.
-
@mattc said:
def initialize() > sel=Sketchup.active_model.selection > SPP;;test_start > SPP;;check_selection(sel) > SPP;;test_end > end
(4) The above is not correct.
The
initialize()
method is only used in a class definition, where you wish to let the user create MANY instances of the object.Users will not need to create many copies of your plugin.
Most plugins are code that needs only one copy, .. so it should be a module (inside your author module.)
But a plugin module can have (and often does,) use custom class definitions and instances inside it's module.
Later on, you will likely be creating a custom
Tool
class inside your plugin module.
Advertisement