Adding edges to manual selection following a snake pattern.

Adding edges to manual selection following a snake pattern.

Postby wisesketch » Mon Mar 20, 2017 5:00 pm

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. :smiley:
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.

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
edges2 = vertex_array[0].edges # Gathers the edges connected to the first vertex (index 0) to the array of edges

# 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

# 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

# Now we just add the edge previously identified as the shortest in the edges2 array to the selection
sel.add edges2[_MinLengthEdge]


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.


Re: Adding edges to manual selection following a snake patte

Postby Dan Rathbun » Sat Mar 25, 2017 2:40 pm

phpBB engined forums uses BBcode.
Please wrap program code in code tags like:

[ code ]
# code here
[ /code ]
There is a "code" button on the post edit toolbar (2nd row, 3rd button)

Read help on BBcode: faq.php?mode=bbcode
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago

    Ad Machine 


    Return to Developers' Forum


    Who is online

    Users browsing this forum: No registered users and 13 guests

    Visit our sponsors: