sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Breaking out of a loop

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 484 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sdmitchS Offline
      sdmitch
      last edited by

      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.

      Nothing is worthless, it can always be used as a bad example.

      http://sdmitch.blogspot.com/

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        see the Ruby keyword break:
        http://www.ruby-doc.org/docs/keywords/1.9/Object.html#method-i-break

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • sdmitchS Offline
          sdmitch
          last edited by

          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.

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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 ??

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • sdmitchS Offline
              sdmitch
              last edited by

              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}
              
              

              Nothing is worthless, it can always be used as a bad example.

              http://sdmitch.blogspot.com/

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                Oh OK.. it might work better if you put everything inside the spin_it() method within a UI.start_timer block.
                see: http://code.google.com/apis/sketchup/docs/ourdoc/ui.html#start_timer
                also take note of UI.stop_timer()

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • sdmitchS Offline
                  sdmitch
                  last edited by

                  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.

                  Nothing is worthless, it can always be used as a bad example.

                  http://sdmitch.blogspot.com/

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @sdmitch said:

                    ... and not stopped before the plugin exited so my object continued to spin.

                    Well you need to add deactivate() and suspend() 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.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jim
                      last edited by

                      Don't forget about the Animation class - it could also have been used instead of a time.

                      Hi

                      1 Reply Last reply Reply Quote 0
                      • TIGT Online
                        TIG Moderator
                        last edited by

                        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 the self.spin_it out of the activate 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 be until 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...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement