@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!
@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!
@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
If I add those last two lines I mentioned for followme, I get this:
@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.
mod = Sketchup.active_model
sel = mod.selection
z_measurement = 2 * perpendicular_measurement
perpendicular_measurement = 3.0
selected_edges = sel.grep(Sketchup::Edge)
if selected_edges.empty?
UI.messagebox('Please select an edge to draw the triangle connected to.')
return
end
selected_edge = selected_edges.first
start_point = selected_edge.start.position
end_point = selected_edge.end.position
midpoint = Geom::Point3d.linear_combination(0.5, start_point, 0.5, end_point)
direction_vector = end_point - start_point
perpendicular_vector = Geom::Vector3d.new(-direction_vector.y, direction_vector.x, 0).normalize
perpendicular_vector.length = perpendicular_measurement
third_vertex = midpoint.offset(perpendicular_vector)
point_above_midpoint = midpoint.offset([0, 0, z_measurement])
triangle_vertices = [midpoint, point_above_midpoint, third_vertex]
triangle_face = mod.entities.add_face(triangle_vertices)
midpoint_third_vertex_edge = mod.entities.add_line(midpoint, third_vertex)
mod = Sketchup.active_model
sel = mod.selection
selected_edge = sel.grep(Sketchup::Edge).first
unless selected_edge
UI.messagebox('Please select an edge to determine its Z height.')
return
end
selected_edge_start_z = selected_edge.start.position.z
selected_edge_end_z = selected_edge.end.position.z
tolerance = 1e-6
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
sel.remove(midpoint_third_vertex_edge)
end
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:
connected_edges_array = selected_edges.to_a
triangle_face.followme(connected_edges_array)
What am I missing?