SketchUcation Premium Membership

 

 

looping all entities

looping all entities

Postby Pout » Thu Mar 01, 2012 9:27 am

I'm a bit puzzled.

Sketchup.active_model.entities.each{|e|e.erase! if e.valid?}
works but doesn't delete all entities

Sketchup.active_model.entities.to_a.each{|e|e.erase! if e.valid?}
works and deletes all entities

Now, does this mean that to loop all entities in a model i always have to put them in an array first?
I never have run into any problems before using the first method.

Additionaly
Code: Select all
f=0
Sketchup.active_model.entities.each{|ent|
   f+=1
   Sketchup.active_model.selection.clear
   Sketchup.active_model.selection.add ent
   ent.erase!
}
puts f

returns 4095 (on a 11200 entities model)
while
Code: Select all
f=0
Sketchup.active_model.entities.each{|ent|
   f+=1
   Sketchup.active_model.selection.clear
   Sketchup.active_model.selection.add ent
   #ent.erase!
}
puts f

returns 11200 (on a 11200 entities model)

I must be missing something essential here... :shock: :?: :?:
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: looping all entities

Postby thomthom » Thu Mar 01, 2012 10:05 am

A few points:

1. Bulk operations are much faster than single operations.

Code: Select all
model.entities.erase_entitiesarray_of_entities 


Is faster than
Code: Select all
for entity in array_of_entities
  e
.erase!
end


But when you want to clear the entire Enitites collection you can just use
Code: Select all
model.entities.clear



2. Never iterate the Entities collection directly when you modify it.

Code: Select all
for entity in model.entities
  e
.erase!
end

This will make some entities be skipped as you are modifying the Entities object you iterate.

Instead make an array copy of the entities:
Code: Select all
for entity in model.entities.to_a
  e
.erase!
end

This will not skip any entities.


3. Model.entities does not contain every entity in the model - only the root of the model hierarchy. Entities in Groups and Components are stored in the definitions in model.defintions.
But if you want to empty the whole model, model.entities.clear! is still the easiest.
http://www.thomthom.net/thoughts/2012/0 ... -sketchup/
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: looping all entities

Postby Pout » Thu Mar 01, 2012 10:21 am

ThomThom

Thanks for the fast reply!
I did the test on a single box (not a group nor instance) and even then it went wrong
So I can assume that Sketchup.active_model.entities.each{|e|
...
}
can always be incorrect even when just checking for something (eg. check if the current entity has a certain value in it's attribute dictionary)?

On point 1, what would be faster

Code: Select all
Sketchup.active_model.entities.to_a.each{|e|
check=e.get_attribute'x','y'
if check==1
e.erase!
end
}

or
Code: Select all
test=[]
Sketchup.active_model.entities.to_a.each{|e|
check=e.get_attribute'x','y'
if check==1
test<<e
end
}
model.entities.erase_entities(test)
Last edited by Pout on Thu Mar 01, 2012 10:25 am, edited 1 time in total.
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: looping all entities

Postby thomthom » Thu Mar 01, 2012 10:25 am

Pout wrote:went wrong

What went wrong? Examples?

Pout wrote:So I can assume that Sketchup.active_model.entities.each{|e|
...
}
can always be incorrect even when just checking for something?

No, just when you modify it. If you just read states and properties there should be no problems.

Pout wrote:On point 1, what would be faster

The second - using .erase_entities.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: looping all entities

Postby Pout » Thu Mar 01, 2012 10:33 am

thomthom wrote:What went wrong? Examples?

It goes wrong caused by the fact that the entities object is edited while executing the loop. So as you stated.




thomthom wrote:No, just when you modify it. If you just read states and properties there should be no problems.

Modify (like in push pull) or only when the entity is deleted?

thomthom wrote:The second - using .erase_entities.

In the second case I could use Sketchup.active_model.entities.each (without the to_a) Correct? And if so would that be faster then with to_a?

Thx again!
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: looping all entities

Postby thomthom » Thu Mar 01, 2012 10:54 am

Pout wrote:It goes wrong caused by the fact that the entities object is edited while executing the loop. So as you stated.

Yes, this is where you need to use to_a.

Pout wrote:Modify (like in push pull) or only when the entity is deleted?

Anything that adds or removed entities to the collection.

Pout wrote:Correct? And if so would that be faster then with to_a?

That won't be of much difference. What makes a difference is using bulk methods that affect multiple entities in one operation over methods that affect only one entity at a time.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: looping all entities

Postby Pout » Thu Mar 01, 2012 10:55 am

Excellent information.
Thank you ThomThom, much appreciated!
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Artisan Organic Toolset - a set of powerful organic modeling tools.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012



 

Return to Developers' Forum

Who is online

Users browsing this forum: gumppy, Jfred305, qbkowsky, salpbes and 2 guests