Model Operation blockform methods.
-
Weird...
If you specify an empty string, as the name of the first operation, in a set which the followers are transparent,... the name in the menu is: "Undo Macro"
-
I pushed several changes today.
https://bitbucket.org/dan_rathbun/sae-operation-classWorks much more like it should:
call
/commit
method can take an array of values to pass into the block.- class method
::raise_errors()
and::raise_errors=()
to control whether exceptions are raised. (This will likely be used for debugging mostly.) - class method
::redo
wrapper forSketchup.send_action('editRedo:')
- class method
::undo
wrapper forSketchup.send_action('editUndo:')
- an
OpStat
object is passed into the block, holding information about the operation itself that might be useful. (This was done instead of passing a direct reference to the operation itself, which would open the door to dangerous things, like callingcommit
from within the block and causing a vicious loop that would probably cause a stack overflow.) onError() {|opStat,model,exception| ... code block ... }
Allows custom handling of errors (ie, translate error messages into Spanish, etc.)
onSuccess() {|opStat,model,results| ... code block ... }
Allows custom handling if everything goes well. (ie, prompt the user to save something, etc.)
-
Here's an example of using the one-shot block form class wrapper method:
1) Say you write a simple "Add Face" plugin with no operations (because either you did not know about them being a newbie, or you just wished to get it working first and polish it later.)
So you have code that looks like:
require('sketchup.rb') module Author ### <-- this would actually be your toplevel namespace module FaceAdder class << self private def add_face # mdl = Sketchup.active_model depth = 10 width = 10 pts = [] pts[0] = [0, 0, 0] pts[1] = [width, 0, 0] pts[2] = [width, depth, 0] pts[3] = [0, depth, 0] # Add the face to the entities in the model grp = mdl.entities.add_group() face = grp.entities.add_face( pts ) return grp # end end # proxy class unless file_loaded?(File.basename(__FILE__)) UI.menu('Plugins').add_item('FaceAdder') { add_face() } file_loaded(File.basename(__FILE__)) end end # module FaceAdder end # module
2) Now you want the the entire command to be one Undo operation.
You can modify the menu item proc, like this:
UI.menu('Plugins').add_item('FaceAdder') { begin require('sae/operation.rb') rescue LoadError add_face() # User does not have file. else SAE.operation('Add Face') { add_face() } end }
In this example, the "Undo" name that will appear in the menu (or the popup tooltip for the undo button on the toolbar,) is specified as "Add Face". All other options will use the defaults, .. ie, the 2nd
model
argument will default to theactive_model
, the 3rd argument will be set to disable the UI during the operation's block execution, and the operation will not be hidden.Putting it all together, the example file, would look like:
require('sketchup.rb') module Author ### <-- this would actually be your toplevel namespace module FaceAdder class << self private def add_face # mdl = Sketchup.active_model depth = 10 width = 10 pts = [] pts[0] = [0, 0, 0] pts[1] = [width, 0, 0] pts[2] = [width, depth, 0] pts[3] = [0, depth, 0] # Add the face to the entities in the model grp = mdl.entities.add_group() face = grp.entities.add_face( pts ) return grp # end end # proxy class unless file_loaded?(File.basename(__FILE__)) UI.menu('Plugins').add_item('FaceAdder') { begin require('sae/operation.rb') rescue LoadError add_face() # User does not have file. else SAE.operation('Add Face') { add_face() } end } file_loaded(File.basename(__FILE__)) end end # module FaceAdder end # module
If this kind of method were to be added into the API, it would likely be an instance method of
Sketchup::Model
, which would not need the 2nd model argument.Of course there could also be a model "flexible" duplicate class method (like I've implemented it,) that does take a model arg, which would either be a module method
Sketchup.operation()
, or a class method likeSketchup::Model.operation()
Thoughts ??
-
Putting this on the back burner until I get some feedback.
Advertisement