Move Vertices to Whole Number Coords?
-
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)
-
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. -
@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.
Advertisement