Delete All Scenes with API Question...

Delete All Scenes with API Question...

Postby renderiza » Tue Oct 03, 2017 3:23 pm

Hi,

Why the following code only deletes some scenes and not all?
Code: Select all
model = Sketchup.active_model
pages = model.pages

pages.each {|page| pages.erase(page)}

A workaround for this issue is...
Code: Select all
model = Sketchup.active_model
pages = model.pages

delete_list = []
pages.each {|page| delete_list << page}
delete_list.each {|page| pages.erase(page)}

But still, I ask why did the first example fail to deliver?

Shortly after this I post stumbled upon this TOPIC and it explained the issue.
0
Last edited by renderiza on Tue Oct 03, 2017 4:16 pm, edited 1 time in total.
User avatar
renderiza 
Premium Member
Premium Member
 

Re: Delete All Scenes with API Question...

Postby TIG » Tue Oct 03, 2017 3:37 pm

When you iterate a collection - like pages, or selection or entities, and change that collection's contents - e.g. by deleting something - then you get issues because the collection changes as a result.
BUT if you 'freeze' the collection, but using .to_a OR perhaps you collect items, which are then deleted afterwards, after you exit using the collection... then it avoids the issue.
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 10 guests

Visit our sponsors: