Deleting Component Instance
-
Hi guys. I'm inserting into my model two Component
pants_path1 = Sketchup.find_support_file("myCOMPO1.skp" ,"Plugins") pants_def1 = Skecthup.active_model.definitions.load(pants_path1) pG1 = Geom;;Ponit3d.new(0,0,5) transform1 = Geom;;Transformation.new(pG1) compo1 = @entities.add_instance(pants_def1, transform1) compo1.name="Name1" compo1.locked = true
and similarly for the 2nd Component
pants_path2 = Sketchup.find_support_file("myCOMPO2.skp" ,"Plugins") pants_def2 = Skecthup.active_model.definitions.load(pants_path2) pG2 = Geom;;Ponit3d.new(0,0,10) transform2 = Geom;;Transformation.new(pG2) compo2 = @entities.add_instance(pants_def2, transform2) compo2.name="Name2" compo2.locked = true
If, in a separate function, I try to delete those Components through an iteration I get a Error: #<TypeError: reference to deleted Entity>.
def eraseCompo() Sketchup.active_model.active_entities.each{|e| if e.typename=="ComponentInstance" if e.name=="Name1" or e.name=="Name2" Sketchup.active_model.erase_entities(e) end end }
What I'm missing?
-
Try this...
Sketchup.active_model.active_entities.grep(Sketchup;;ComponentInstance).select{|e| e.name=="Name1" || e.name=="Name2" }.each{|e| e.erase! if e.valid? }
-
@bomastudio said:
Hi guys. I'm inserting into my model two Component
> pants_path1 = Sketchup.find_support_file("myCOMPO1.skp" ,"Plugins") > pants_def1 = Skecthup.active_model.definitions.load(pants_path1) > pG1 = Geom;;Ponit3d.new(0,0,5) > transform1 = Geom;;Transformation.new(pG1) > compo1 = @entities.add_instance(pants_def1, transform1) > compo1.name="Name1" > compo1.locked = true >
and similarly for the 2nd Component
> pants_path2 = Sketchup.find_support_file("myCOMPO2.skp" ,"Plugins") > pants_def2 = Skecthup.active_model.definitions.load(pants_path2) > pG2 = Geom;;Ponit3d.new(0,0,10) > transform2 = Geom;;Transformation.new(pG2) > compo2 = @entities.add_instance(pants_def2, transform2) > compo2.name="Name2" > compo2.locked = true >
If, in a separate function, I try to delete those Components through an iteration I get a Error: #<TypeError: reference to deleted Entity>.
def eraseCompo() > Sketchup.active_model.active_entities.each{|e| > if e.typename=="ComponentInstance" > if e.name=="Name1" or e.name=="Name2" > Sketchup.active_model.erase_entities(e) > end > end > } >
What I'm missing?
The mis-spelling of Sketchup in the .load statements, and Point3d in the Geom::Transformation statements?
Sketchup.active_model.erase_entities(e) should be e.erase!
-
Your missing the number one rule of iterating collections (in any programming language.)
Do not delete collection members while iterating the collection. The loop will lose it's place, and strange results occur. In that case, always iterate an array copy of the collection, if your loop must remove or add collection members. This is most often seen while modifying Entities collections.
In TIG's example, he used the
select
method, which produces a separate new array subset of the original collection.The simplest way to get an array copy, is using the
to_a
method.
The second good thing TIG showed was to always check your API
Entity
sub-class objects for validity. -
Guys you are and invaluable support for SU developers.... a beer to all of you!!!
Advertisement