• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Making a selection in a ruby script and applying command

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 2 Posters 45 Views
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.
  • H Offline
    hollymichel Newcomers
    last edited by 1 Apr 2024, 13:41

    Hi,
    I'm new to making ruby scripts and am attempting to automate a sequence of actions I use constantly. The basic idea is, I select an edge and my script will draw a triangle of my specified size and select all the edges that are connected to my initial selection and perform a followme on the triangle. Everything works up until the followme. My triangle is created and my selection set is accurate. If I stop the script there, I can manually click Followme and it works on the path. But I can't seem to get the script to recognize those additional selected lines. It will only do a followme on the very first line I picked before running the script. It doesn't collect the new edges.
    I used this language to attempt the followme:

    Convert the selection to an array of edges

    connected_edges_array = selected_edges.to_a

    Perform follow me on triangle face using selected edges as path

    triangle_face.followme(connected_edges_array)

    What am I missing?

    T 1 Reply Last reply 1 Apr 2024, 14:13 Reply Quote 0
    • T Offline
      TIG Moderator @hollymichel
      last edited by 1 Apr 2024, 14:13

      @hollymichel
      You're not helping us help you.
      You have only posted a few lines of code, so we can't see where you're going wrong.
      Also a screenshot of the starting setup would be helpful...

      Also please use the 'Code' formatting for any Ruby code your are posting...

      As you seem to be able to do it manually the path and face seem OK - although we haven't seen them !

      ### something sets a selection of edges up ??
      connected_edges_array = selected_edges.to_a
      ### the face is somehow selected ??
      triangle_face.followme(connected_edges_array)
      

      How do you ensure that all of those selected edges which are to be used are connected ?
      How do you select the face ?

      More code please...

      TIG

      H 1 Reply Last reply 1 Apr 2024, 14:44 Reply Quote 0
      • H Offline
        hollymichel Newcomers @TIG
        last edited by 1 Apr 2024, 14:44

        @TIG
        Here is all the code that works and results in a closed loop of the edges I want. Not sure how to get the results to make a followme. I am testing this on a simple rectangle to start.

        Get the active model and selection

        mod = Sketchup.active_model
        sel = mod.selection

        Define z and perpendicular measurements

        z_measurement = 2 * perpendicular_measurement
        perpendicular_measurement = 3.0

        Get selected edges

        selected_edges = sel.grep(Sketchup::Edge)

        Exit if there are no selected edges

        if selected_edges.empty?
        UI.messagebox('Please select an edge to draw the triangle connected to.')
        return
        end

        Select the first selected edge

        selected_edge = selected_edges.first

        Get the start and end vertices of the selected edge

        start_point = selected_edge.start.position
        end_point = selected_edge.end.position

        Calculate the midpoint of the selected edge

        midpoint = Geom::Point3d.linear_combination(0.5, start_point, 0.5, end_point)

        Calculate the direction vector perpendicular to the edge

        direction_vector = end_point - start_point
        perpendicular_vector = Geom::Vector3d.new(-direction_vector.y, direction_vector.x, 0).normalize
        perpendicular_vector.length = perpendicular_measurement

        Calculate the third vertex of the triangle

        third_vertex = midpoint.offset(perpendicular_vector)

        Create a point at the desired height above the midpoint

        point_above_midpoint = midpoint.offset([0, 0, z_measurement])

        Define the vertices of the triangle

        triangle_vertices = [midpoint, point_above_midpoint, third_vertex]

        Create the triangle face

        triangle_face = mod.entities.add_face(triangle_vertices)

        Create an edge between midpoint and third_vertex

        midpoint_third_vertex_edge = mod.entities.add_line(midpoint, third_vertex)

        Get the active model and selection

        mod = Sketchup.active_model
        sel = mod.selection

        Get the selected edge

        selected_edge = sel.grep(Sketchup::Edge).first

        Exit if no edge is selected

        unless selected_edge
        UI.messagebox('Please select an edge to determine its Z height.')
        return
        end

        Get the Z coordinate of the selected edge

        selected_edge_start_z = selected_edge.start.position.z
        selected_edge_end_z = selected_edge.end.position.z

        Define a tolerance for comparing Z coordinates

        tolerance = 1e-6

        Select all edges with starting and ending Z coordinates matching the selected edge

        mod.entities.each do |entity|
        if entity.is_a?(Sketchup::Edge)
        edge = entity
        next if edge == selected_edge

        # Get the starting and ending Z coordinates of the edge
        edge_start_z = edge.start.position.z
        edge_end_z = edge.end.position.z
        
        # Check if starting and ending Z coordinates match the selected edge
        if (selected_edge_start_z - edge_start_z).abs < tolerance &&
           (selected_edge_end_z - edge_end_z).abs < tolerance
          sel.add(edge)
        end
        

        end

        Remove the edge between midpoint and third_vertex to the selection

        sel.remove(midpoint_third_vertex_edge)
        end

        T 1 Reply Last reply 1 Apr 2024, 15:27 Reply Quote 0
        • H Offline
          hollymichel Newcomers
          last edited by 1 Apr 2024, 14:47

          9708afc2-4b9d-4708-9667-a979506c19f9-image.png

          1 Reply Last reply Reply Quote 0
          • H Offline
            hollymichel Newcomers
            last edited by 1 Apr 2024, 14:55

            b74330b8-fe91-4426-896e-6523af2b9883-image.png
            If I add those last two lines I mentioned for followme, I get this:

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator @hollymichel
              last edited by 1 Apr 2024, 15:27

              @hollymichel
              Please format your code properly using the 'Code' option above

              </>
              

              It's almost impossible to read.

              I think you are over complicating it.

              mod = Sketchup.active_model
              sel = mod.selection
              selected_edges = sel.grep(Sketchup::Edge)
              ### add the triangle code - where the face is located on the edges is academic, you can draw it on and selected edge.
              ### Add the face from the desired points. You can get a reference to it after it's added.
              triangle_face.followme(selected_edges.to_a)
              

              The selected_edges are not guaranteed to be all connected and in order [if they are connected there are somewhat complicated methods to order them].
              A simpler way to get an array of those edges is to get the 'base_face' rather than the what you are selecting the edges around, and use that -

              mod = Sketchup.active_model
              sel = mod.selection
              selected_base_face= sel.grep(Sketchup::Face)[0]
              ### make triangle...
              triangle_face.followme(selected_base_face.outer_loop.edges)
              ### automatically an order array
              ###
              

              TIG

              H 1 Reply Last reply 1 Apr 2024, 15:40 Reply Quote 0
              • H Offline
                hollymichel Newcomers @TIG
                last edited by 1 Apr 2024, 15:40

                @TIG ```
                Sorry, this should read better.
                Will the base face method work if there is no face, just lines that could be a face?
                <# Get the active model and selection
                mod = Sketchup.active_model
                sel = mod.selection

                <# Define z and perpendicular measurements
                z_measurement = 2 * perpendicular_measurement
                perpendicular_measurement = 3.0

                <# Get selected edges
                selected_edges = sel.grep(Sketchup::Edge)

                <# Exit if there are no selected edges
                if selected_edges.empty?
                UI.messagebox('Please select an edge to draw the triangle connected to.')
                return
                end

                <# Select the first selected edge
                selected_edge = selected_edges.first

                <# Get the start and end vertices of the selected edge
                start_point = selected_edge.start.position
                end_point = selected_edge.end.position

                <# Calculate the midpoint of the selected edge
                midpoint = Geom::Point3d.linear_combination(0.5, start_point, 0.5, end_point)

                <# Calculate the direction vector perpendicular to the edge
                direction_vector = end_point - start_point
                perpendicular_vector = Geom::Vector3d.new(-direction_vector.y, direction_vector.x, 0).normalize
                perpendicular_vector.length = perpendicular_measurement

                <# Calculate the third vertex of the triangle
                third_vertex = midpoint.offset(perpendicular_vector)

                <# Create a point at the desired height above the midpoint
                point_above_midpoint = midpoint.offset([0, 0, z_measurement])

                <# Define the vertices of the triangle
                triangle_vertices = [midpoint, point_above_midpoint, third_vertex]

                <# Create the triangle face
                triangle_face = mod.entities.add_face(triangle_vertices)

                <# Create an edge between midpoint and third_vertex
                midpoint_third_vertex_edge = mod.entities.add_line(midpoint, third_vertex)

                <# Get the active model and selection
                mod = Sketchup.active_model
                sel = mod.selection

                <# Get the selected edge
                selected_edge = sel.grep(Sketchup::Edge).first

                <# Exit if no edge is selected
                unless selected_edge
                UI.messagebox('Please select an edge to determine its Z height.')
                return
                end

                <# Get the Z coordinate of the selected edge
                selected_edge_start_z = selected_edge.start.position.z
                selected_edge_end_z = selected_edge.end.position.z

                <# Define a tolerance for comparing Z coordinates
                tolerance = 1e-6

                <# Select all edges with starting and ending Z coordinates matching the selected edge
                mod.entities.each do |entity|
                if entity.is_a?(Sketchup::Edge)
                edge = entity
                next if edge == selected_edge

                # Get the starting and ending Z coordinates of the edge
                edge_start_z = edge.start.position.z
                edge_end_z = edge.end.position.z
                
                # Check if starting and ending Z coordinates match the selected edge
                if (selected_edge_start_z - edge_start_z).abs < tolerance &&
                   (selected_edge_end_z - edge_end_z).abs < tolerance
                  sel.add(edge)
                end
                

                end

                <# Remove the edge between midpoint and third_vertex to the selection
                sel.remove(midpoint_third_vertex_edge)

                <# Convert the selection to an array of edges
                connected_edges_array = selected_edges.to_a

                <# Perform follow me operation on triangle face using selected edges as path
                triangle_face.followme(connected_edges_array)
                end

                T 1 Reply Last reply 1 Apr 2024, 21:18 Reply Quote 0
                • H Offline
                  hollymichel Newcomers
                  last edited by 1 Apr 2024, 16:06

                  @TIG said in Making a selection in a ruby script and applying command:

                  selected_base_face= sel.grep(Sketchup::Face)[0]

                  Awesome, the base face works great when a face with edges is selected!

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    TIG Moderator @hollymichel
                    last edited by 1 Apr 2024, 21:18

                    @hollymichel
                    No, it doesn't read a lot better.
                    You must add the 'code' tag and put your Ruby code inside it.
                    Then the start and end tags [i.e. three back-quotes each: note I put spaces in front here to prevent the forum thinking it was more code...

                        ```
                        ### ruby code text goes here
                        ```
                    

                    ] they must both be on their own lines.
                    Which looks like this in the post.

                        ### ruby code text goes here
                    

                    Simplify your process, and make it more complex only when you've got some good results.
                    Trying to do it all in one go is just too difficult.

                    How do you eat an elephant - take small bites !

                    TIG

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

                    Advertisement