home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / examplescripts.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  5.8 KB  |  163 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. # This file includes a number of examples of how to use the Ruby interface
  16. # to access a SketchUp model and perform various kinds of operations.
  17.  
  18. # To test the examples you must first load the files.
  19. # There are two ways that you can do this.
  20.  
  21. # The first way is to manually load it from the Ruby console.  You can show
  22. # the Ruby console by selecting View/Ruby Console. (This is actually added
  23. # to the View menu when the Ruby script sketchup.rb is loaded.)  In the
  24. # Ruby console, type the following line:
  25. # load 'examples/examples.rb'
  26. # If there are no syntax errors in the file, it will display "true" in the
  27. # Ruby console.  Otherwise it will show error messages and information about
  28. # the line on which errors were found.
  29.  
  30. # The second way to load this file is to copy it into the SketchUp plugins
  31. # directory.  Any files with the extension .rb that are in the plugins
  32. # directory are automatically loaded when you start SketchUp.
  33.  
  34. # The following line includes some common useful scripts.  The file
  35. # sketchup.rb is automatically put in the SketchUp plugins directory
  36. # when SketchUp is installed, so it should always get automatically
  37. # loaded anyway, but it is good practice to explicitly require any
  38. # files that you have dependencies on to make sure that they are loaded.
  39. require 'sketchup.rb'
  40.  
  41. #-----------------------------------------------------------------------------
  42. # box.rb is an example of how to create simple geometry using Ruby.
  43. # It also shows how to create a dialog box to prompt for user input
  44. # and how to add an item to a menu.
  45.  
  46. require 'examples/box.rb'
  47.  
  48. #-----------------------------------------------------------------------------
  49. # selection.rb has a number of examples of how to traverse the model and
  50. # select things in Ruby.
  51.  
  52. require 'examples/selection.rb'
  53.  
  54. #-----------------------------------------------------------------------------
  55. # contextmenu.rb shows how you can add new choices to context menus.  It
  56. # adds an item to the context menu for arcs and circles to create a
  57. # point at the center of the arc.
  58.  
  59. require 'examples/contextmenu.rb'
  60.  
  61. #-----------------------------------------------------------------------------
  62. # linetool.rb shows how you can create tools that respond to mouse event
  63. # in Ruby.  It defines a simple tool that behave similar to the pencil
  64. # tool in SketchUp except that it creates finite length construction lines
  65. # instead of regular SketchUp edges
  66.  
  67. require 'examples/linetool.rb'
  68.  
  69. #-----------------------------------------------------------------------------
  70. # animation.rb has an example of how you can create animations in Ruby.
  71. # It creates a simple animation that spins the view around.
  72.  
  73. require 'examples/animation.rb'
  74.  
  75. # attributes.rb shows how to attach arbitrary application specific attribute
  76. # data to SketchUp Entities.
  77.  
  78. require 'examples/attributes.rb'
  79.  
  80. #=============================================================================
  81. # This will set the layer of everything that is selected to
  82. # a layer with the given name.  It will create a new layer if needed
  83. def setLayer(layerName)
  84.  
  85.     model = Sketchup.active_model
  86.     ss = model.selection
  87.     if( ss.empty? ) 
  88.         return nil
  89.     end
  90.  
  91.     # If there is alread a Layer with the given name, the add method on the
  92.     # Layers object will return the existing Layer.  Otherwise it will
  93.     # create a new one and return it.
  94.     layer = model.layers.add(layerName)
  95.     
  96.     # now iterate through everything that is selected and set its layer
  97.     for ent in ss
  98.         ent.layer = layer
  99.     end
  100.     
  101.     # Here is another way that you could do the same thing
  102.     #ss.each {|ent| ent.layer = layer}
  103.     
  104. end
  105.  
  106. #-------------------------------------------------------------------------
  107. # compute the total area of all faces
  108. def totalArea
  109.     area = 0
  110.     
  111.     model = Sketchup.active_model
  112.     
  113.     # this shows a different syntax for iterating through the model
  114.     model.entities.each { |ent| area += ent.area if( ent.is_a? Sketchup::Face ) }
  115.     
  116.     # here is a different way you could do it
  117. #    for ent in model.entities
  118. #        if( ent.is_a? Sketchup::Face )
  119. #            area += ent.area
  120. #        end
  121. #    end
  122.  
  123.     area
  124. end
  125.  
  126. #-------------------------------------------------------------------------
  127. # Get the perimeter of the selected faces
  128. def perimeter
  129.  
  130.     length = 0
  131.     edges = []
  132.     
  133.     # First collect all of the edges that bound all of the selected faces.
  134.     model = Sketchup.active_model
  135.     ss = model.selection
  136.     for ent in ss
  137.         if( ent.is_a? Sketchup::Face )
  138.             edges.concat ent.edges
  139.         end
  140.     end
  141.     
  142.     # remove duplicate edges
  143.     edges.uniq!
  144.     
  145.     # sum the lengths of all of the edges
  146.     edges.each {|e| length += e.length}
  147.     
  148.     length
  149. end
  150.  
  151. #-----------------------------------------------------------------------------
  152. # SketchUp sets up Ruby to look for files in its plugins directory when you
  153. # use the load command.  You can add additional directories to its search path.
  154. # This can be useful when you are developing new scripts because it can
  155. # make it easier to load them during testing.
  156. # This command adds your home directory to the search path.  Note that this
  157. # only works if the environment variable HOME is defined.
  158. # $: is a special system variable in Ruby that defines the search path.
  159. if( ENV["HOME"] )
  160.     homedir = File.expand_path("~")
  161.     $:.push homedir
  162. end
  163.