Problems with script
-
Can someone test this piece of code on the attached model?
For me it does not erase the entities (all text)
Any ideas?Sketchup.active_model.entities.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
-
Hi,
It works, but to make it work you have to run the piece of code several times, until everything is erased.
entities method returns a collection of entities. When you have to erase more than one entity, better use 'erase_entities', like this:def xxx() arrayents=[] Sketchup.active_model.entities.each{|e| arrayents.push(e) if (e.layer.name=="_4D_Finish_Text" or e.layer.name=="_4D_Start_Text") } Sketchup.active_model.entities.erase_entities(arrayents) end end
Everything's gone in one go Hope this helps,
-
Great! That works excellent.
I wonder why it does not delete everything with the method i suggested.I think i will need to review some of my code. Is erase the only method that gives this 'problem'?
Thx again Didier
-
You should NOT change a collection of entities you are iterating through...
NO-NO=Sketchup.active_model.entities.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
YES-YES=
Sketchup.active_model.entities.to_a.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
Note how the entities is made into frozen array [.to_a] before processing...
To remove such items from inside all definitions use
Sketchup.active_model.definitions.each{|d|d.entities.to_a.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' })
EDIT: typo corrected '.to_a' added to final example
Advertisement