sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    [code] Timer class

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 1.3k Views 2 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.
    • M Offline
      MartinRinehart
      last edited by

      I wrote a Timer class. I'm setting it free here, hoping that newbie developers will find it useful or educational.

      The code here is more test code than Timer code. The Timer class is more doc than code. The test code here moves a selected component 100 times by [1,1,1]. (Set frames_per_second to your own taste.)

      If you don't know how to move components, look at the move() routine in the Timer_test class.

      
      # timer.rb - a Timer class
      
      require 'sketchup'
      
      class Timer
      =begin
      Written and placed in public domain by Martin Rinehart, January, 2010.
      
      Thinking back to my Java days, I thought that UI.start/stop_timer() methods should really be replaced by a Timer class. You can do three things with a timer;
       
      timer = Timer.new( pause_length, runnable )
      timer.start()
      timer.stop()
      
      A Timer is initialized with a pause_length (seconds, including fractions of a second, often 1.0/frames_per_second) and a Runnable object. An object is Runnable if it has a run() method that returns a boolean (true==keep running, false==stop running). 
      
      After the Timer's start() method is called, the Runnable's run() is called every pause_length seconds until the Timer's stop() method is called or the Runnable's run() method returns false.
      =end
      
          # attr_reader ;pause_length, ;runnable, ;running
      
          def initialize( pause_length, runnable )
              @pause_length = pause_length
              @runnable = runnable
              @running = false
          end
      
          def start()
              @id = UI.start_timer( @pause_length, true ) { 
                  @running =  @runnable.run()
                  stop() unless @running
              }
          end
      
          def stop()
              UI.stop_timer( @id )
              @running = false
          end
      
      end # of class Timer
      
      class Timer_test # implements Runnable
      
          attr_reader ;valid
      
          def initialize()
              model = Sketchup.active_model
              sels = model.selection()
              @valid = false
              if sels.length > 0
                  @sel = sels[0]
                  @valid = @sel.is_a?( Sketchup;;ComponentInstance )
              end
              @moves = 0
          end
          
          def move( r, g, b )
              trans = @sel.transformation().to_a()
              trans[12] += r; trans[13] += g; trans[14] += b
              trans2 = Geom;;Transformation.new( trans )
              @sel.move!( trans2 )
              Sketchup.active_model().active_view().invalidate()
          end
          
          def run()
              move( 1, 1, 1 )
              @moves += 1
              return @moves < 100
          end
          
      end # of Timer_test
      
      fps = 10 # frames per second
      
      tt = Timer_test.new() 
      if tt.valid()
          t = Timer.new( 1.0/fps, tt ) 
          t.start()
      else
          UI.messagebox( 'Please select a component.' )
      end
      
      # end of timer.rb
      
      

      To use this code, save it into /some/dir, not Plugins. Select a component in any model and make sure you can still see it [100,100,100] away from its current location. In the Ruby Console load '/some/dir/timer.rb'.

      Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

      1 Reply Last reply Reply Quote 0
      • C Offline
        cjthompson
        last edited by

        Just a very small suggestion: instead of having

        t = Timer.new( 1.0/fps, tt )
        

        you might want to put:

        t = Timer.new( 1/fps.to_f, tt )
        

        That way, people experimenting with the code can easily change the numbers, without having to make sure that it's a decimal.

        EDIT: Although, now that I think of it, they would only change the fps, not the 1. 😳

        1 Reply Last reply Reply Quote 0
        • M Offline
          MartinRinehart
          last edited by

          @cjthompson said:

          you might want to put:

          t = Timer.new( 1/fps.to_f, tt )
          

          Matz made some brilliant decisions, and some other decisions. The decision not to coerce to float was, maybe, one of the other decisions. How many times are you really helped by 9/2 returning 4?

          Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

          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