sketchucation logo sketchucation
    • Login
    šŸ¤‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Adding edges to manual selection following a snake pattern.

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 2 Posters 764 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.
    • W Offline
      wisesketch
      last edited by

      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


      Reference image. Snake-like skeleton structure.


      Code

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

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

        [pre:3asokg6p][ code ]

        code here

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


        Read help on BBcode: http://sketchucation.com/forums/faq.php?mode=bbcode

        I'm not here much anymore.

        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