home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / animation.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  3.3 KB  |  109 lines

  1. # Copyright 2005-2008, Google, Inc.
  2.  
  3. # This software is provided as an example of using the Ruby interface
  4. # to SketchUp.
  5.  
  6. # Permission to use, copy, modify, and distribute this software for 
  7. # any purpose and without fee is hereby granted, provided that the above
  8. # copyright notice appear in all copies.
  9.  
  10. # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  11. # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  12. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. #-----------------------------------------------------------------------------
  14.  
  15. require 'sketchup.rb'
  16.  
  17. #-----------------------------------------------------------------------------
  18.  
  19. # This is an example of a simple animation that spins the model around
  20.  
  21. class ViewSpinner
  22.  
  23. def initialize
  24.     # save the center of rotation
  25.     model = Sketchup.active_model
  26.     view = model.active_view
  27.     camera = view.camera
  28.     @target = model.bounds.center
  29.     @up = Geom::Vector3d.new(0, 0, 1)
  30.     @distance = camera.eye.distance @target
  31.     @zmin = @target.z
  32.     @zmax = @zmin + @distance
  33.     @dz = @distance / 300
  34.     @z = @zmin
  35.     @angle = 0
  36.     @frame = 0;
  37.     @startTime = Time.now
  38.     Sketchup::set_status_text($exStrings.GetString("FPS"), 1)
  39. end
  40.  
  41. # The only required method for an animation is nextFrame.  It is called 
  42. # whenever you need to show the next frame of the animation.
  43. def nextFrame(view)
  44.     @frame = @frame + 1
  45.     totalTime = Time.now - @startTime
  46.     fps = 1
  47.     if( totalTime > 0.001 )
  48.         fps = @frame / totalTime
  49.     end
  50.     fps = fps.to_i
  51.     Sketchup::set_status_text(fps, 2)
  52.     
  53.     # Compute the eye point for this frame
  54.     a = @angle * Math::PI / 180.0
  55.     x = @target.x + (@distance * Math::sin(a))
  56.     y = @target.y + (@distance * Math::cos(a))
  57.     eye = Geom::Point3d.new(x, y, @z)
  58.     view.camera.set(eye, @target, @up)
  59.     @angle = (@angle+1)%360
  60.     view.show_frame
  61.     
  62.         # make the camera move up and down
  63.     @z += @dz
  64.     if( @z > @zmax )
  65.         @z = @zmax
  66.         @dz = -@dz
  67.     elsif( @z < @zmin )
  68.         @z = @zmin
  69.         @dz = -@dz
  70.     end
  71.     
  72.     # If nextFrame returns false, the animation will stop
  73.     # Uncommenting the next line will cuase th animation to
  74.     # stop after one revolution.
  75.     # return @frame < 360
  76.     return true
  77. end
  78.  
  79. # The stop method will be called when SketchUp wants an animation to stop
  80. # this method is optional.
  81. def stop
  82.     # clear the stuff we displayed on the status line
  83.     Sketchup::set_status_text("", 1)
  84.     Sketchup::set_status_text("", 2)
  85. end
  86.  
  87. end # class ViewSpinner
  88.  
  89. # This is just a function that starts spinning the active view
  90. def spinview
  91.     Sketchup.active_model.active_view.animation = ViewSpinner.new
  92. end
  93.  
  94. #-----------------------------------------------------------------------------
  95.  
  96. # Add an Animations sub-menu to the Camera menu
  97. if( not file_loaded?("animation.rb") )
  98.     #Note: We don't translate the Menu names - the Ruby API assumes you are 
  99.     #using English names for Menus.
  100.     add_separator_to_menu("Camera")
  101.     animation_menu = UI.menu("Camera").add_submenu($exStrings.GetString("Animations"))
  102.     animation_menu.add_item($exStrings.GetString("Spin View")) {spinview}
  103.     animation_menu.add_item($exStrings.GetString("Stop Spinning")) {
  104.         Sketchup.active_model.active_view.animation = nil
  105.     }
  106. end
  107.  
  108. file_loaded("animation.rb")
  109.