Ruby for SketchUp: Extracting information from a selected entity/selection

I want to extract information from a selected edge. In particular the starting point, the end point, and the length of the edge. The Ruby API documentation goes over how to define edges, points, vectors, etc. manually but I can not seem to figure out how to get this info from an existing edge. All the methods under selection and edge just return the number of entities selected rather than any info on the entity itself.

Any advice would be of help.

1 Like

One approach out of a million:

Class: Sketchup::Selection

# Get a handle to the selection set.
model = Sketchup.active_model
selection = model.selection

The #each method is used to iterate through all of the selected entities.

selection.each { |entity| puts entity }

Check if the entity is_a?
Sketchup::Edge
and query the desired properties, like:

  • The #end method is used to retrieve the Vertex object at the end of the edge.
  • The #length method is used to retrieve the length of an edge in current units.
  • The #position method is used to retrieve the Point3d position of a vertex.
    ect.
selection.each { |entity| 
  puts entity 
  if entity.is_a?(Sketchup::Edge)
    puts "#{entity.length}  (#{entity.length.inspect})"
    vertex = entity.end
    puts vertex.position
  end
}
1 Like

You can try something like this:

edge = Sketchup.active_model.selection.first # Assuming an edge is selected
puts edge.start.position