How to get all new edges after add_line?

When creating a line with add_line method, usually I can get the new edge directly:

new_edge = Sketchup.active_model.entities.add_line(p1, p2)

But Sketchup sometimes creates new edges automatically. For example:

p1 =, 32.5221977, 0)
p2 =, 32.5221977, 0)
e1 = Sketchup.active_model.entities.add_line(p1, p2)

p9 =, 32.5221977, 0)
p10 =, 34.8287888, 0)
e5 = Sketchup.active_model.entities.add_line(p9, p10)

As result there are 3 new edges, instead of 2 ones. How can I get all the new edges, including the ones that Sketchup creates automatically?

You can “record” them before and get difference after. E.g.:

def current_edges
  edges = Sketchup.active_model.entities.grep(Sketchup::Edge)

edges_before = current_edges()
# add your edges

edges_after = current_edges() - edges_before

You may consider using #active_entities method instead of the #entities method.


Another way to catch new entities are:

# @param [Sketchup::Entities] entities
def collect_new_entities(entities, &block)
  existing = entities.to_a
  entities.to_a - existing

entities = Sketchup.active_model.active_entities
new_entities = collect_new_entities(entities) do
  # do stuff here

I’ve been using this pattern of encapsulating the logic to a method that takes a block quite a bit. Allows for more code reuse.

1 Like