Folks,
This is my first post. I am a novice just learning the ropes using SketchUp manually, as well as trying my first ruby scripts. Please, bear with me. I know my code is neither elegant nor efficient. I am sure there are far better ways to do what I am trying to do, but right now Iād just be happy if the code works.
Iād really appreciate if I can get some help on this seemly simple code that has gotten already too many hours of fruitless work. Thanks in advance to those from whom I get some help.
MODEL: I have a structure that resembles a snakeās skeleton. Basically ribs attached to a spine. It consists of a series of short edges connected forming a long snake-like elongated s-shaped curve. I call this curve āthe spineā, where each short edge is like a āvertebraeā. To each one of the short vertebrae edges, there are two longer edges (I call them āribsā) connected roughly perpendicularly to the short edge at the end of it, where it also connects to the next short edge. So, I have this curvy spine constituted of short edge vertebrae, to which longer edge ribs are connected in roughly perpendicular fashion. Please, see uploaded picture for reference.
GOAL: I want my script to add a given number of consecutive short edge vertebrae to the selection starting from whichever short edge vertebrae I select manually.
PROBLEM: With the script as it is, depending on where I start, sometimes it adds a couple of short edges and sometimes it adds a few more, but never the full amount that I specify in the script. It seems like after adding a few of the short edges to the initial manual selection it just stops adding them.
CODE:
model = Sketchup.active_model # Access the model
ents = model.entities # Access the entities in the model
sel = model.selection # Access selection object
vertex_array = []
edges2 = []
for _Count1 in 1..7
vertex_array.clear # Clears the vertex_array to repopulate it with the newly added edge's vertices
vertex_array = sel[_Count1 - 1].vertices
edges2.clear # Clears the edges2 array to repopulate it with the newly added edge's vertices
# Since the connector/short (spine) edges connecting the long (rib) edges are always descending, ...
# ... find the vertex with the lowest value for the z-coordinate to identify the next set of edges, ...
# ... where to find the next short (vertebrae) edge.
if vertex_array[0].position.z.to_mm > vertex_array[1].position.z.to_mm
edges2 = vertex_array[1].edges # Gathers the edges connected to the second vertex (index 1) to the array of edges
else
edges2 = vertex_array[0].edges # Gathers the edges connected to the first vertex (index 0) to the array of edges
end
Now we get rid of the short edge that was last added to the selection, since it was included ...
... in the edges2 array, as it is also connected to the vertex used to gather the edges.
after removingt this edge there would only be 3 edges in the edges2 array.
_LastSelId = sel[_Count1 - 1].object_id.to_s
for myCount in 0..(edges2.length - 1)
if edges2[myCount].object_id.to_s == _LastSelId
edges2.delete_at(myCount)
end
end
After getting rid of the short edge that was last added to the selection, of the 3 edges left, ...
the shortest one (a vertebrae) should be the one to be the next edge to add to the selection.
_MinLengthEdge = [edges2[0].length, edges2[1].length, edges2[2].length].min
for myCount2 in 0..(edges2.length - 1)
if edges2[myCount2].length == _MinLengthEdge
_MinLengthEdge = myCount2
end
end
Now we just add the edge previously identified as the shortest in the edges2 array to the selection
sel.add edges2[_MinLengthEdge]
end
The above code is also available in a text file I uploaded in this post.
Please, help me figure this out. There are probably amongst us some SketchUp code gods (and hopefully goddesses too) that would figure out whatās wrong with my code in a second. Also, feel free to point enhancement (both in elegance and efficiency) to my code.
Thanks in advance.
Raoul