# Copyright 2004, @Last Software, Inc. # This software is provided as an example of using the Ruby interface # to SketchUp. # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted, provided that the above # copyright notice appear in all copies. # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #----------------------------------------------------------------------------- require 'sketchup.rb' #----------------------------------------------------------------------------- # This is an example of a simple animation that spins the model around class ViewSpinner def initialize # save the center of rotation model = Sketchup.active_model view = model.active_view camera = view.camera @target = model.bounds.center @up = Geom::Vector3d.new(0, 0, 1) @distance = camera.eye.distance @target @zmin = @target.z @zmax = @zmin + @distance @dz = @distance / 300 @z = @zmin @angle = 0 @frame = 0; @startTime = Time.now Sketchup::set_status_text("FPS", 1) end # The only required method for an animation is nextFrame. It is called # whenever you need to show the next frame of the animation. def nextFrame(view) @frame = @frame + 1 totalTime = Time.now - @startTime fps = 1 if( totalTime > 0.001 ) fps = @frame / totalTime end fps = fps.to_i Sketchup::set_status_text(fps, 2) # Compute the eye point for this frame a = @angle * Math::PI / 180.0 x = @target.x + (@distance * Math::sin(a)) y = @target.y + (@distance * Math::cos(a)) eye = Geom::Point3d.new(x, y, @z) view.camera.set(eye, @target, @up) @angle = (@angle+1)%360 view.show_frame # make the camera move up and down @z += @dz if( @z > @zmax ) @z = @zmax @dz = -@dz elsif( @z < @zmin ) @z = @zmin @dz = -@dz end # If nextFrame returns false, the animation will stop # Uncommenting the next line will cuase th animation to # stop after one revolution. # @frame < 360 true end # The stop method will be called when SketchUp wants an animation to stop # This method is optional (you do not need to do anything in your implementation # of the method, unless you want to. Usually it is used to clean up things, such # as the status bar. # The animation doesn't actually stop until the stop method has finished. def stop UI.messagebox "Stopped" end end # class nextFrameTest # This is just a function that starts spinning the active view def nextFrameTest model = Sketchup.active_model view = model.active_view view.animation = ViewSpinner.new end def pause UI.messagebox "Paused" end def resume UI.messagebox "Resumed" end #----------------------------------------------------------------------------- # Add an Animations sub-menu to the Camera menu if( not file_loaded?("aminationtests.rb") ) # This will add a separator to the menu, but only once add_separator_to_menu("Plugins") # To add an item to a menu, you identify the menu, and then # provide a title to display and a block to execute. In this case, # the block just calls the create_box function plugins_menu = UI.menu("Plugins") AnimationTest_menu = plugins_menu.add_submenu("Animation Tests") AnimationTest_menu.add_item("Animation.NextFrame") { nextFrameTest } AnimationTest_menu.add_item("Animation.pause") { } AnimationTest_menu.add_item("Animation.resume") { } # We cannot call the stop method of an Animation class implementation # explicitly, only SketchUp calls it given certain events occuring. One # such event is to set the current active animation to point to nothing (nil) AnimationTest_menu.add_item("Animation.Stop") { Sketchup.active_model.active_view.animation = nil } end file_loaded("animationtests.rb")