home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / utilitiestools.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  7.4 KB  |  218 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 adds some useful functions to the Utilities menu in SketchUp
  16. require 'sketchup.rb'
  17.  
  18. # This funtion will create a face from the edges in the selection set.
  19. # There must be at least three Edges selected.
  20. # It will show an error message if a face couldn't be created from the edges.
  21. def create_face_from_selection
  22.     ss = Sketchup.active_model.selection
  23.     
  24.     # Get an Array of all of the selected Edges
  25.     edges = ss.find_all { |e| e.kind_of?(Sketchup::Edge) }
  26.     
  27.     # We need at least 3 Edges
  28.     if( edges.length < 3 )
  29.         UI.messagebox($uStrings.GetString("You must select at least three Edges"))
  30.         return nil
  31.     end
  32.     
  33.     # Try to create a Face from the Edges in the active component
  34.     face = nil
  35.     begin
  36.         face = Sketchup.active_model.active_entities.add_face edges
  37.     rescue
  38.     end
  39.     
  40.     # If a Face wasn't created, then there should be an error message
  41.     # telling what the problem was
  42.     if( not face )
  43.         if( $! )
  44.             msg = $!.message
  45.         else
  46.             msg = $uStrings.GetString("Create Face from Edges failed")
  47.         end
  48.         UI.messagebox msg
  49.     end
  50.     
  51.     # Return the Face that was created
  52.     face
  53. end
  54.  
  55. #-------------------------------------------------------------------------
  56. # This example show how you can write a simple tool in Ruby.  To implement
  57. # a tool, you have to implement the methods that you want to handle.
  58. # In this example the following methods are implementd:
  59. # onMouseMove is called when a mouse move event is received.
  60. # draw is handled to draw the tool
  61.  
  62. # This tool show coordinates of points and screen positions.
  63. # It also shows lengths of selected Edges or areas of selected Faces.
  64.  
  65. # To see this tool in action, type the following in the Ruby console window:
  66. # Sketchup.active_model.select_tool TrackMouseTool.new
  67.  
  68. class TrackMouseTool
  69.  
  70. # The activate method is called when a tool is first activated.  It is not
  71. # required, but it is a good place to initialize stuff.
  72. def activate
  73.     @ip = Sketchup::InputPoint.new
  74.     @iptemp = Sketchup::InputPoint.new
  75.     @displayed = false
  76. end
  77.  
  78. # onMouseMove is called whenever SketchUp gets a mouse move event.  It is called
  79. # a lot, so you should try to make it as efficient as possible.
  80. def onMouseMove(flags, x, y, view)
  81.     # show the screen position in the VCB
  82.     Sketchup::set_status_text("#{x}, #{y}", SB_VCB_VALUE)
  83.     
  84.     # get a position in the model and show it in a tooltip
  85.     @iptemp.pick view, x, y
  86.     if( @iptemp.valid? )
  87.         changed = @iptemp != @ip
  88.         @ip.copy! @iptemp
  89.         pos = @ip.position;
  90.         
  91.         # get the text for the position
  92.         msg = @ip.tooltip
  93.         if( msg.length > 0 )
  94.             msg << " "
  95.         end
  96.         #msg << pos.to_s
  97.         msg << "( #{Sketchup.format_length(pos.x)},#{Sketchup.format_length(pos.y)},#{Sketchup.format_length(pos.z)} )"
  98.  
  99.         
  100.         # See if it is on any special geometry
  101.         if( @ip.vertex == nil )
  102.             if( @ip.edge )
  103.                 if( @ip.depth > 0 )
  104.                     length = @ip.edge.length(@ip.transformation)
  105.                 else
  106.                     length = @ip.edge.length
  107.                 end
  108.                 msg <<  "\n       " + $uStrings.GetString("length") + " = #{Sketchup.format_length(length)}"
  109.             elsif( @ip.face )
  110.                 if( @ip.depth > 0 )
  111.                     area = @ip.face.area(@ip.transformation)
  112.                 else
  113.                     area = @ip.face.area
  114.                 end
  115.                 msg << "\n       " + $uStrings.GetString("area") + " = #{Sketchup.format_area(area)}"
  116.             end
  117.         end
  118.  
  119.         msg2 = msg.gsub(/\n/,' ')
  120.         Sketchup::set_status_text msg2
  121.         
  122.         # set the tooltip to show this message
  123.         view.tooltip = msg
  124.         
  125.         # see if we need to update the display for this point
  126.         if( changed and (@ip.display? or @displayed) )
  127.             # This tells the view that we want it to update itself
  128.             view.invalidate
  129.         end
  130.     end
  131.     
  132. end
  133.  
  134. # onLButtonDown is called when the user presses the left mouse button
  135. def onLButtonDown(flags, x, y, view)
  136.     Sketchup::set_status_text $uStrings.GetString("Left button down at") + " (#{x}, #{y})"
  137. end
  138.  
  139. # onLButtonUp is called when the user releases the left mouse button
  140. def onLButtonUp(flags, x, y, view)
  141.     Sketchup::set_status_text $uStrings.GetString("Left button up at") + " (#{x}, #{y})"
  142. end
  143.  
  144. # draw is optional.  It is called on the active tool whenever SketchUp
  145. # needs to update the screen.
  146. # in this case, we display an input point if needed
  147. def draw(view)
  148.     if( @ip.valid? && @ip.display? )
  149.         @ip.draw view
  150.         @displayed = true
  151.     else
  152.         @displayed = false
  153.     end
  154. end
  155.  
  156. def getInstructorContentDirectory
  157.     "Query"
  158. end
  159.  
  160. end
  161.  
  162. #-----------------------------------------------------------------------------
  163. # Make selected components unique.
  164. # This looks at all selected components.
  165. def make_selected_components_unique
  166.  
  167.     # First get all selected components
  168.     ss = Sketchup.active_model.selection
  169.     components = ss.find_all {|e| e.kind_of?(Sketchup::ComponentInstance) }
  170.     
  171.     # Create a Hash.  The keys are the guids of all of the component definitions
  172.     # for selected components.  The values are arrays of all of the component instances
  173.     # that have that definition
  174.     definitions = {}
  175.     for component in components
  176.         key = component.definition.guid
  177.         a = definitions[key]
  178.         if( a )
  179.             a.push component
  180.         else
  181.             definitions[key] = [component]
  182.         end
  183.     end
  184.     
  185.     # Now make each collection of component instances have a new unique definition
  186.     definitions.each_value do |a|
  187.         # a is an array of component instances that all share the same definition
  188.         # We will make the first one have a new unique definition, and all of the
  189.         # other ones use that new definition
  190.         definition = nil
  191.         for component in a
  192.             if( definition )
  193.                 # We already have a new definition.  Make this component use it
  194.                 component.definition = definition
  195.             else
  196.                 # Make the component have a unique definition and save it
  197.                 component.make_unique
  198.                 definition = component.definition
  199.             end
  200.         end
  201.     end
  202.  
  203.     true
  204. end
  205.  
  206. #----------------------------------------------------------------------------
  207. # Add things to the Utilities menu
  208. if( not $utilities_menu_loaded )
  209.     add_separator_to_menu("Tools")
  210.     utilities_menu = UI.menu("Tools").add_submenu($uStrings.GetString("Utilities"))
  211.  
  212.     utilities_menu.add_item($uStrings.GetString("Create Face")) { create_face_from_selection }
  213.     utilities_menu.add_item($uStrings.GetString("Query Tool")) { Sketchup.active_model.select_tool TrackMouseTool.new }
  214.     #utilities_menu.add_item($uStrings.GetString("Fix Non-planar Faces")) { Sketchup.send_action "fixNonPlanarFaces:" }
  215.  
  216.     $utilities_menu_loaded = true
  217. end
  218.