Why this error and how to correct it?

The reason this happens is because you are breaking a fundamental rule of collections.
The rule is: Do not modify the collection while you are iterating it.

Instead, save up an array of items (faces) to be deleted, and then use the bulk entities delete method:

            edges_to_erase = []
            grp.entities.grep(Sketchup::Edge) do |e|
              edges_to_erase << e if e.faces.size != 2
            end
            grp.entities.erase_entities(edges_to_erase)
4 Likes