sketchucation logo sketchucation
    • Login
    1. Home
    2. hollymichel
    3. Posts
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download
    H
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 6
    • Groups 2

    Posts

    Recent Best Controversial
    • RE: Making a selection in a ruby script and applying command

      @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!

      posted in Developers' Forum
      H
      hollymichel
    • RE: Making a selection in a ruby script and applying command

      @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

      posted in Developers' Forum
      H
      hollymichel
    • RE: Making a selection in a ruby script and applying command

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

      posted in Developers' Forum
      H
      hollymichel
    • RE: Making a selection in a ruby script and applying command

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

      posted in Developers' Forum
      H
      hollymichel
    • RE: Making a selection in a ruby script and applying command

      @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

      posted in Developers' Forum
      H
      hollymichel
    • Making a selection in a ruby script and applying command

      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?

      posted in Developers' Forum
      H
      hollymichel
    • 1 / 1