Breaking out of a loop
-
Is there anyway to duplicate the VB's DoEvents in a Ruby script? I have been trying to figure out a way to interupt a loop. I have tried most if not all of the 'on' tool options but none have succeeded in breaking out of a loop.
-
see the Ruby keyword
break
:
http://www.ruby-doc.org/docs/keywords/1.9/Object.html#method-i-break -
Thanks Dan. I am familiar with break and have it inside my loop. The problem is I want the trigger for the break has to come from outside the loop which is why I have attempted to use all the 'on' tools to set the trigger but they seem to be ignored until the loop terminates on its own.
-
Well .. there is not much help we can give you without some sort of example. Or at least explaining what you want to do in psuedo-code.
"on tools" I think you are meaning the various Observer callbacks ??
-
Dan, I didn't make myself very clear. The 'on' tools are onKeyDown, onKeyUP, etc. When the code is running, I wanted to stop the spinning when I pressed a key and resume it when I released the key. But the onKeyDown doesn't execute until the spin_it method times out. I have also tried all the ButtonDown and Ups with the same results.
class Spinners def activate mod = Sketchup.active_model ent = mod.entities sel = mod.selection @corg=[] sel.each{|e| @corg<<e if e.class==Sketchup;;Group || e.class==Sketchup;;ComponentInstance} sel.clear;@stop_spin=false;@start_time=Time.now;self.spin_it end def spin_it begin @corg.each{|e| tr=Geom;;Transformation.rotation(e.bounds.center,Z_AXIS,1.degrees) e.transform! tr } Sketchup.active_model.active_view.refresh break if @stop_spin sleep(0.1) end until Time.now - @start_time > 10 end def onKeyDown(key,repeat,flags,view) @stop_spin=true puts "stopped" end def onKeyUp(key,repeat,flags,view) puts "started" @stop_spin=false @start_time=Time.now self.spin_it end def onCancel(flags,view) Sketchup.send_action "selectSelectionTool;" end end UI.menu("Plugins").add_item("Spin_It") {Sketchup.active_model.select_tool Spinners.new}
-
Oh OK.. it might work better if you put everything inside the
spin_it()
method within aUI.start_timer
block.
see: http://code.google.com/apis/sketchup/docs/ourdoc/ui.html#start_timer
also take note ofUI.stop_timer()
-
Thanks again Dan. I think the start/stop_timer may be the solution to the problem. However they are apparently not without pitfalls. My first try ended up starting the timer with repeat set to true and not stopped before the plugin exited so my object continued to spin.
-
@sdmitch said:
... and not stopped before the plugin exited so my object continued to spin.
Well you need to add
deactivate()
andsuspend()
callbacks that stop the timer block. Your timer id will need to be an@var
(instance variable.)P.S.: The timer blocks are the only "thread-like" mechanism in the API.
-
Don't forget about the Animation class - it could also have been used instead of a time.
-
Shouldn't you swap the results of the key-up and key-down events ?
You hold the key down it spins, release it and it stops ?
Take theself.spin_it
out of theactivate
method, it's run when there's a key-DOWN event anyway.
When there's a key-UP event it sets@stop_spin=true
, so the 'until test' should expand to beuntil Time.now - @start_time > 10 **or** @stop_spin
and you should remove the matching break from inside the loop too as the until breaks it anyway...
Advertisement