sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Move Vertices to Whole Number Coords?

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 218 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      Jim
      last edited by

      I'm just playing around trying to move all vertices to the nearest rounded whole-number positions. For example, a vertex at (1.2, 5.5, 9.1) would be moved to (1, 6, 9).

      Here's the code I have, but it doesn't seem to work very well. The vertices move but seem to bounce back and forth rather than landing on the target position.

      I am using a model with a single edge for my test. Can anyone see a problem, or explain why SU is having trouble with this?

      Note, if I run the code 30-40 times, the vertices eventually end up positioned on integer coordinates, so I am not sure why this doesn't work on the first run.

      
      model = Sketchup.active_model
      sel   = model.selection
      ents  = model.entities
      verts = Set.new
      vecs  = []
      edges = ents.grep(Sketchup;;Edge)
      
      edges.each do |edge|
        edge.vertices.each do |vertex|
          if not verts.include?(vertex)
            verts.insert(vertex)
            target_position = vertex.position.to_a.map{|e| e.round }
            vec = vertex.position.vector_to(target_position)
            vecs.push(vec)
            # cpoint for visual aid
            cp = vertex.position.offset(vec)
            ents.add_cpoint(cp)
          end
        end
      end
      
      ents.transform_by_vectors(s.to_a, vecs)
      
      
      

      Hi

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        My 1st advice.. do not use the SketchUp API's Set class (because it's a thin wrapper around a C++ collection,) and you cannot be sure of the order of it's elements, especially if you will be sync'ing to another Ruby-side collection such as an Array.

        If you want to sync... use a Hash, whose keys are the vertex objects, and whose values are the offset vectors.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by

          @dan rathbun said:

          My 1st advice.. do not use the SketchUp API's Set class (because it's a thin wrapper around a C++ collection,) and you cannot be sure of the order of it's elements, especially if you will be sync'ing to another Ruby-side collection such as an Array.

          Yeah, that seemed to be the problem - Sets are not ordered. Thanks Dan.

          Hi

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement