home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / attributes.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  6.3 KB  |  157 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. # This example shows how you can attach arbitrary attribute data to entities
  19. # and then do queries based on those attributes.
  20.  
  21. # In this example, there is a method to attach a cost estimate attribute to
  22. # selected faces.  You can then do a query on the model and get the total
  23. # cost estimate.
  24.  
  25. # This is a global variable that keeps track of the last value entered
  26. $cost_per_sq_ft = 1.00 if not $cost_per_sq_ft
  27.  
  28. # This method prompts for a cost estimate and attaches it to all selected faces
  29. def attach_cost_estimate
  30.  
  31.     # See if there is anything selected
  32.     ss = Sketchup.active_model.selection
  33.     return if ss.empty?
  34.     
  35.     # First prompt the user for the cost per sq foot
  36.     prompts = [$exStrings.GetString("Cost per sq. foot")]
  37.     values = [$cost_per_sq_ft]
  38.     results = inputbox prompts, values, $exStrings.GetString("Cost Estimate")
  39.     return if not results
  40.     
  41.     # Now attach this as an attribute to all selected Faces
  42.     $cost_per_sq_ft = results[0]
  43.     ss.each do |e|
  44.         if( e.kind_of? Sketchup::Face )
  45.             # An attributes are stored with named values in a named dictionary
  46.             # In this example, the dictionary name is 'skpex'.
  47.             # The attribute value is named 'cpsf'.
  48.             # Note that you can use any name that you want for the dictionary
  49.             # and the attributes.  The attributes names are currently stored 
  50.             # on every entity that the attribute is attached to however, so
  51.             # it is generally a good idea to keep the name short to avoid
  52.             # using a lot of extra memory storing the attributes.
  53.             e.set_attribute 'skpex', 'cpsf', $cost_per_sq_ft
  54.         end
  55.     end
  56.  
  57. end
  58.  
  59. # This method assigns a cost estimate to a Material.  You can then paint
  60. # faces with that material to get them to use that estimate
  61. def assign_estimate_to_material
  62.     # First get a list of all of the materials in the model
  63.     model = Sketchup.active_model
  64.     materials = model.materials
  65.     names = materials.collect {|m| m.name}
  66.     translatednames = materials.collect {|m| m.display_name}
  67.     
  68.     # Display a dialog to pick a material and cost
  69.     prompts = [$exStrings.GetString("Material"), $exStrings.GetString("Cost per sq. foot")]
  70.     values = [translatednames[0], $cost_per_sq_ft]
  71.     enums = [translatednames.join("|")]
  72.     results = inputbox prompts, values, enums, $exStrings.GetString("Cost By Material")
  73.     return if not results
  74.     $cost_per_sq_ft = results[1]
  75.     
  76.     # Get the selected Material
  77.     index = translatednames.index(results[0])
  78.     material = index ? materials[names[index]] : nil
  79.     if( not material )
  80.         UI.messagebox $exStrings.GetString("Could not find Material named") + " #{results[0]}"
  81.         return
  82.     end
  83.  
  84.     # And attach the cost estimate attribute
  85.     material.set_attribute 'skpex', 'cpsf', $cost_per_sq_ft
  86. end
  87.  
  88. # This method iterates through all faces in the model and computes the
  89. # total cost estimate based on the the attributes attached to the faces.
  90. # For purposes of this example, it does not look at faces that are inside
  91. # Groups or Components.
  92. def compute_cost_estimate
  93.     total_cost = 0.0
  94.     
  95.     entities = Sketchup.active_model.entities
  96.     entities.each do |e|
  97.         next if not e.kind_of? Sketchup::Face
  98.         
  99.         # See if it has a cost estimate attribute  get_attribute will
  100.         # return nil if there is no attribute matching the given names
  101.         cost_per_sq_ft = e.get_attribute 'skpex', 'cpsf'
  102.         
  103.         # If there is a cost estimate attached directly to the Face
  104.         # then that is used.  Otherwise, look to see if there is a cost
  105.         # estimate attached to a Material for the Face.  This is a little
  106.         # bit of a problem because a Face can have a front and back Material.
  107.         # For this example, we use the cost estimates from both
  108.         if not cost_per_sq_ft
  109.             material = e.material
  110.             if( material )
  111.                 cost_per_sq_ft = material.get_attribute 'skpex', 'cpsf'
  112.             end
  113.             material = e.back_material
  114.             if( material )
  115.                 cpsf_back = material.get_attribute 'skpex', 'cpsf'
  116.                 if( cpsf_back )
  117.                     if( cost_per_sq_ft )
  118.                         cost_per_sq_ft += cpsf_back
  119.                     else
  120.                         cost_per_sq_ft = cpsf_back
  121.                     end
  122.                 end
  123.             end
  124.         end
  125.         
  126.         # If there is not Material with a cost estimate on it, then
  127.         # skip this Face.
  128.         next if not cost_per_sq_ft
  129.         
  130.         # Compute the estimate for this face based on the area
  131.         # The area is returned in square inches - convert to square feet
  132.         area = e.area / 144.0
  133.         cost = area * cost_per_sq_ft
  134.         
  135.         total_cost += cost
  136.     end
  137.     
  138.     # Now display the results
  139.     dollars = format("%.2f", total_cost)
  140.     msg = $exStrings.GetString("Total Cost Estimate") + " = \$#{dollars}"
  141.     UI.messagebox(msg, MB_OK, $exStrings.GetString("Cost Estimate"))
  142. end
  143.  
  144. # Add some menu items to access this
  145. if( not file_loaded?("attributes.rb") )
  146.     #Note: We don't translate the Menu names - the Ruby API assumes you are 
  147.     #using English names for Menus.
  148.     plugins_menu = UI.menu("Plugins")
  149.     cost_menu = plugins_menu.add_submenu($exStrings.GetString("Cost"))
  150.     cost_menu.add_item($exStrings.GetString("Assign Estimate to Material")) { assign_estimate_to_material }
  151.     cost_menu.add_item($exStrings.GetString("Assign Estimate to Faces")) { attach_cost_estimate }
  152.     cost_menu.add_item($exStrings.GetString("Compute Estimate")) { compute_cost_estimate }
  153. end
  154.  
  155. #-----------------------------------------------------------------------------
  156. file_loaded("attributes.rb")
  157.