home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / selection.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  3.8 KB  |  116 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. # Two functions that were requested on the forum
  16.  
  17. # Invert the current selection
  18. def invert_selection
  19.     model = Sketchup.active_model
  20.     ss = model.selection
  21.     model.entities.each {|e| ss.toggle(e)}
  22. end
  23.  
  24. # Hide everything that is not selected
  25. def hide_rest
  26.     model = Sketchup.active_model
  27.     ss = model.selection
  28.     model.start_operation $exStrings.GetString("Hide Rest")
  29.     model.entities.each {|e| e.visible = false if not ss.contains?(e) }
  30.     model.commit_operation
  31. end
  32.  
  33. #-------------------------------------------------------------------------
  34. # This function allows you to select things in the model based on a predicate
  35. # that you pass it as a block.  It can allow for some very flexible kinds 
  36. # of selections.
  37.  
  38. # This exression will select everything that is on the Layer named "Joe"
  39. # do_select {|e| e.layer.name == "Joe"}
  40.  
  41. # This one will select everything that is on either layer "Joe" or layer "Bob"
  42. # do_select {|e| e.layer.name == "Joe" || e.layer.name == "Bob"}
  43.  
  44. # You can also use regular expressions.  The following command would
  45. # select everything on layers whose name started with "W".  for example
  46. # everything on layers "Walls" and "Windows"
  47. # do_select {|e| (e.layer.name =~ /W.*/) == 0}
  48.  
  49. # This will select all Edges in the model
  50. # do_select {|e| e.kind_of?(Sketchup::Edge)}
  51.  
  52. # This will select all edges that are on layer "Joe"
  53. # do_select {|e| e.kind_of(Sketchup::Edge) && e.layer.name == "Joe"}
  54.  
  55. def do_select
  56.  
  57.     model = Sketchup.active_model
  58.  
  59.     # First clear the selection
  60.     ss = model.selection
  61.     ss.clear
  62.     
  63.     # iterate through everything in the model
  64.     for ent in model.entities
  65.         if( yield(ent) )
  66.             ss.add(ent)
  67.         end
  68.     end
  69.     
  70.     # return the number of things selected
  71.     ss.length
  72. end
  73.  
  74. # These examples add a couple of menu items to select things in the model 
  75. # based on Layer or Material.  The also demonstrate putting a popup list
  76. # in an niput box
  77.  
  78. def select_by_layer
  79.     # First get a list of all of the layers in the model
  80.     model = Sketchup.active_model
  81.     layers = model.layers
  82.     names = layers.collect {|l| l.name}
  83.     
  84.     # Display a dialog to pick the layer to select
  85.     prompts = [$exStrings.GetString("Layer")]
  86.     values = [names[0]]
  87.     enums = [names.join("|")]
  88.     results = inputbox prompts, values, enums, $exStrings.GetString("Select By Layer")
  89.     return if not results
  90.     
  91.     # Now select everything on the selected layer
  92.     layername = results[0]
  93.     do_select {|e| e.layer.name == layername}
  94. end
  95.  
  96. def select_by_material
  97.     # First get a list of all of the materials in the model
  98.     model = Sketchup.active_model
  99.     materials = model.materials
  100.     names = materials.collect {|m| m.name}
  101.     displaynames = materials.collect {|m| m.display_name}
  102.     
  103.     # Display a dialog to pick the material to select
  104.     prompts = [$exStrings.GetString("Material")]
  105.     values = [displaynames[0]]
  106.     enums = [displaynames.join("|")]
  107.     results = inputbox prompts, values, enums, $exStrings.GetString("Select By Material")
  108.     return if not results
  109.     
  110.     # Now select everything with the selected Material
  111.     index = displaynames.index(results[0])
  112.     materialname = index ? names[index] : $exStrings.GetString("Default")
  113.     do_select {|e| e.material && (e.material.name == materialname)}
  114. end
  115.  
  116.