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.
- Code: Select all
# 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'.