Script to randomly move sandbox TIN vertices?

Very new to Ruby. Wondering how difficult it would be to create a script to randomly move the vertices of a sandbox TIN along the Z-axis between -1 and 1 units? Can anyone point me in good direction on where to start? Thanks!

Start here …

Then study examples provided by the Trimble Development team and posted in their Extension Warehouse Store (Example Scripts, Shapes, Utilities Tools, etc.)

Also study open source extension by community members.

@thomthom (who now works for Trimble) created a vertex quantizer example some years ago, that may be beneficial for you to study. (It may have been posted over at the SketchUcation forums. Membership is free there.)

1 Like

I found the topic thread (that I referred to above) …

… and my posting of the cleaned up code, which I repost below.

As stated in the original thread, the “plugin module” should be wrapped inside some author module.
Use it as an example of how to adjust geometry vertices in SketchUp.

#  ==========================================================================
#  GridSnap.rb
#  --------------------------------------------------------------------------
#  by Thomas Thomassen
#
#  --------------------------------------------------------------------------
#  THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#  --------------------------------------------------------------------------
#
#  This utility quantizes the vertex locations, of the current selection,
#    of the model, or just the current context, to a 3D grid, whose interval
#    can be set via a popup inputbox.
#
#  --------------------------------------------------------------------------
#  Revisions:
#
#   0.1.0 : 2012-03-26 : by Thomas Thomassen
#   |
#   initial beta release
#
#   0.2.0 : 2012-03-26 : by Dan Rathbun
#   |
#   + Added menu_command() method for inputbox and undo operation.
#
#   0.3.0 : 2012-03-28 : by Dan Rathbun
#   |
#   + Revised the menu_command() method to pass a selection set to the
#       adjust_vertices() method as a 2nd argument.
#
#  ==========================================================================

module GridSnap

  @@last_tolerance = 0.25.m

  class << self # PROXY CLASS

    def round_point( point, tolerance )
      grid_pt = point.to_a.map { |n|
        b = ( n / tolerance ).to_i * tolerance
        r = n % tolerance
        (r > tolerance / 2) ? b + tolerance : b
      }
      Geom::Point3d.new( *grid_pt )
    end
   
    def test_it
      pt = Geom::Point3d.new( 3.976879.m, 3.760209.m, 1.002568.m )
      tolerance = @@last_tolerance
      grid_pt = round_point( pt, tolerance )
     
      puts "Original: #{pt}"
      puts "Tolerance: #{tolerance}"
      puts "Grid Point: #{grid_pt}"
    end
   
    def adjust_vertices( tolerance = @@last_tolerance, ents = nil )
      model = Sketchup.active_model
      vertices = []
      # determine the context:
      ents = model.active_entities() if ents.nil?
      # Collect vertices.
      for e in ents #model.active_entities
        vertices << e.vertices if e.is_a?( Sketchup::Edge )
      end
      vertices.flatten!
      vertices.uniq!
      # Calculate grid adjustments.
      vectors = []
      entities = []
      for vertex in vertices
        pt = vertex.position
        grid_pt = round_point( pt, tolerance )
        vector = pt.vector_to( grid_pt )
        next unless vector.valid?
        entities << vertex
        vectors << vector
      end
      # Apply transformations
      model.active_entities.transform_by_vectors( entities, vectors )
      puts "#{entities.size} adjusted to grid."
    end
    
    def menu_command()
      title   = 'Adjust Vertices'
      prompt  = 'Tolerance'
      default = @@last_tolerance
      
      result = UI.inputbox([prompt],[@@last_tolerance],title) rescue @@last_tolerance
      if result
        model = Sketchup.active_model
        begin
          ###
          model.start_operation("#{title} (#{Sketchup.format_length(result[0])})")
            #
            if model.selection.empty?
              adjust_vertices( result[0] )
            else
              adjust_vertices( result[0], model.selection.to_a() )
            end
            #
          model.commit_operation()
          ###
        rescue
          model.abort_operation()
        else
          @@last_tolerance = result[0]
        end
      end
    end

  end # PROXY CLASS
  
  #{# RUN ONCE
  #
  unless file_loaded?("GridSnap.rb")
   
    # Menu Entry:
    UI.menu("Tools").add_item("GridSnap") { menu_command() }
    
    file_loaded("GridSnap.rb")
  
  end #}

end # module