home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / box.rb.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  5.3 KB  |  122 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 example shows how you can create a simple shape in a script.
  20. # When you run this function, it will display a dialog box which prompts
  21. # for the dimensions of a box, and then creates the box.
  22.  
  23. def create_box
  24.     # First prompt for the dimensions.  This is done using the inputbox
  25.     # method.  In this case, we will actually use a wrapper for UI.inputbox
  26.     # that is defined in sketchup.rb which does some extra error checking
  27.     
  28.     # The first step is to create some arrays which contain the prompts
  29.     # and default values.
  30.     prompts = [$exStrings.GetString("Width"), $exStrings.GetString("Height"), $exStrings.GetString("Depth")]
  31.     values = [6.feet, 5.feet, 4.feet]
  32.     
  33.     # Note that for the default values, we specify the values like "6.feet"
  34.     # This says that the value is  Langth, and tells what the units of
  35.     # the Length are.  When you display a Length value in an input box, it
  36.     # will be formatted using your current units settings and it will parse
  37.     # the values that you enter as a Length.
  38.     
  39.     # Now display the inputbox
  40.     results = inputbox prompts, values, $exStrings.GetString("Box Dimensions")
  41.     
  42.     # The values that the user entered are returned in the results value.
  43.     # If the user hit the "Cancel" button, then the function will return
  44.     # nil.  Otherwise it is an array of values.  SKetchUp tries to match
  45.     # the type of the returned values with the types of the default value
  46.     # supplied, so in this case, since the default values were Lengths we
  47.     # will get back an Array of Lengths
  48.     return if not results # This means that the user canceld the operation
  49.     
  50.     width, height, depth = results
  51.     
  52.     # Now we can actually create the new geometry in the Model.  There are
  53.     # a number of ways that we could actually create the geometry.  We will
  54.     # show a couple of ways.
  55.     
  56.     # The first thing that we will do is bracket all of the entity creation
  57.     # so that this looks like a single operation for undo.  If we didn't do this
  58.     # you would get a whole bunch of separate undo items for each step
  59.     # of the entity creation.
  60.     model = Sketchup.active_model
  61.     model.start_operation $exStrings.GetString("Create Box")
  62.     
  63.     # We will add the new entities to the "active_entities" collection.  If
  64.     # you are not doing a component edit, this will be the main model.
  65.     # if you are doing a component edit, it will be the open component.
  66.     # You could also use model.entities which is the top level collection
  67.     # regardless of whether or not you are doing a component edit.
  68.     entities = model.active_entities
  69.  
  70.     # If you wanted the box to be created as simple top level entities
  71.     # rather than a Group, you could comment out the following two lines.
  72.     group = entities.add_group
  73.     entities = group.entities
  74.     
  75.     # First we will create a rectangle for the base.  There are a few
  76.     # variations on the add_face method.  This uses the version that
  77.     # takes points and automatically creates the edges needed.
  78.     pts = []
  79.     pts[0] = [0, 0, 0]
  80.     pts[1] = [width, 0, 0]
  81.     pts[2] = [width, depth, 0]
  82.     pts[3] = [0, depth, 0]
  83.     base = entities.add_face pts
  84.     
  85.     # You could use a similar technique to crete the other faces of
  86.     # the box.  For this example, we will use the pushpull method instead.
  87.     # When you use pushpull, the direction is determined by the direction
  88.     # of the fromnt of the face.  In order to control the direction and
  89.     # get the pushpull to go in the direction we want, we first check the
  90.     # direction of the face normal.  If it is not in the direction that
  91.     # we want, we will reverse the sign of the distance.
  92.     height = -height if( base.normal.dot(Z_AXIS) < 0 )
  93.     
  94.     # Now we can do the pushpull
  95.     base.pushpull height
  96.     
  97.     # Now we are done and we can end the operation
  98.     model.commit_operation
  99. end
  100.  
  101. # This shows how you can add new items to the main menu from a Ruby script.
  102. # This will add an item called "Box" to the Create menu.
  103.  
  104. # First check to see if we have already loaded this file so that we only 
  105. # add the item to the menu once
  106. if( not file_loaded?("box.rb") )
  107.  
  108.     # This will add a separator to the menu, but only once
  109.     #Note: We don't translate the Menu names - the Ruby API assumes you are 
  110.     #using English names for Menus.
  111.     add_separator_to_menu("Draw")
  112.     
  113.     # To add an item to a menu, you identify the menu, and then
  114.     # provide a title to display and a block to execute.  In this case,
  115.     # the block just calls the create_box function
  116.     UI.menu("Draw").add_item($exStrings.GetString("Box")) { create_box }
  117.  
  118. end
  119.  
  120. #-----------------------------------------------------------------------------
  121. file_loaded("box.rb")
  122.