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 = Geom::Point3d.new(30.6172829, 32.5221977, 0)
p2 = Geom::Point3d.new(7.05351925, 32.5221977, 0)
e1 = Sketchup.active_model.entities.add_line(p1, p2)
p9 = Geom::Point3d.new(27.9214783, 32.5221977, 0)
p10 = Geom::Point3d.new(30.3216038, 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?
dezmo
2
You can “record” them before and get difference after. E.g.:
def current_edges
edges = Sketchup.active_model.entities.grep(Sketchup::Edge)
edges.to_a
end
edges_before = current_edges()
# add your edges
edges_after = current_edges() - edges_before
BTW
You may consider using #active_entities method instead of the #entities method.
2 Likes
tt_su
3
Another way to catch new entities are:
# @param [Sketchup::Entities] entities
def collect_new_entities(entities, &block)
existing = entities.to_a
block.call
entities.to_a - existing
end
entities = Sketchup.active_model.active_entities
new_entities = collect_new_entities(entities) do
# do stuff here
end
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