SketchUcation Plugin Store

 

 

Move points to a '3D grid'

Re: Move points to a '3D grid'

Postby thomthom » Mon Mar 26, 2012 7:53 pm

Dan Rathbun wrote:It seemed to only change a cubes vertices in the Z axis ??

Should apply to X,Y and Z... Seemed to work correctly in my quick test...
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Move points to a '3D grid'

Postby daiku » Tue Mar 27, 2012 2:47 pm

Dan Rathbun wrote:
thomthom wrote:Cubificator?

Polyhedronizer ?

Orthogonizer ?

Orthographizer ?

OrthograFixer ?

:lol:


It's like Autotune for cad! They can make you sound good, even if you can't sing.
If you are editing digital music, and you need to make the drum beats perfect, you can move the beats to the nearest sixteenth note, or whatever. It's call "quantizing".
daiku
 
Posts: 213
Joined: Mon Nov 12, 2007 2:54 pm
Location: Minneapolis
Name: Clark Bremer

Re: Move points to a '3D grid'

Postby Dan Rathbun » Tue Mar 27, 2012 4:00 pm

daiku wrote:It's called "quantizing".

TRUE !!

Restriction to discrete values (or multiples thereof,) rather than any values in the set of possibles (real numbers, in this case.)

So.. the long term:
Vertex Positional Quantization

The short form:
Vertex to Grid
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4069
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Move points to a '3D grid'

Postby rossthompson » Wed Mar 28, 2012 2:30 pm

Hi guys, I have had a play with Dan's Beta version and for me it works really well. The verticies are adjusted in X,Y and Z directions and the undo function works fine!

The only slight problem: it adjusts all the geometry in the model, rather than just the active selection. The original module just adjusted the active selection.

Any ideas?

Again thanks for the support - this script should be a realy time saver!

Ross
rossthompson
 
Posts: 27
Joined: Tue Jan 27, 2009 12:26 pm
Location: Oxford
Name: Ross Thompson
Operating system: Windows
SketchUp version: 7
License type: Free
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Move points to a '3D grid'

Postby Dan Rathbun » Wed Mar 28, 2012 3:27 pm

Well .. I did not think I changed the context of what Thomas' code did... just tried to wrap the code properly and added the menu_command() method to handle an inputbox.

I'll have a look at it.. again.

(And it's not really MY edition just because I added the inputbox.. it's still Thomas' thing.)
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4069
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Move points to a '3D grid'

Postby Dan Rathbun » Wed Mar 28, 2012 3:56 pm

OK... added support for a selection. (Pssst! ... there was no selection support from the beginning Ross.)

This utility should probably be wrapped in Thomas' toplevel author module, and reside below his author directory in some manner.

I'll leave those choices up to him, and when he releases version 1.0.0, we'll delete these beta versions in this thread and put in a link to the release thread.

Code: Select all
#  ==========================================================================
#  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
 
 
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4069
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Vertex Tools for SketchUp. Take control over each vertex with this vertex editor for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: Exposys and 7 guests