Check if group of entities exist, if true delete them
-
I'm struggling a bit with this simple action.
I've got a tool that generates and places 3d text based on user input. However each time they use the tool I need to check if the 3d text already exists. If it does, it needs to be deleted and replaced.
I've dug up Entities.erase_entities from the API, but I have not been able to implement it successfully where I avoid undefined local variable (trying to detect the 3d text on first run) and reference to deleted Group (attempting to delete the 3d text a second time) errors.
Here is the relevant snippet:
commentgroup = entities.add_group commentgroupents = commentgroup.entities addusercomment = commentgroupents.add_3d_text(usercomment, TextAlignLeft, "Arial", true, false, fontheight, 0.0, 0.5, true, 5.0)
Thank you.
-
Maybe this will help
if you know the x1 y1 z1 of a group textSketchup.active_model.active_entities.to_a.each{|e| e.erase! if e.is_a?(Sketchup::Text) and e.point==Geom::Point3d.new(x1,y1,z1) }
Given a [x,y,z] delete a text note.
http://sketchucation.com/forums/viewtopic.php?f=180&t=45726#p408528 -
@dukejazz said:
Maybe this will help
if you know the x1 y1 z1 of a group textSketchup.active_model.active_entities.to_a.each{|e| e.erase! if e.is_a?(Sketchup::Text) and e.point==Geom::Point3d.new(x1,y1,z1) }
Given a [x,y,z] delete a text note.
http://sketchucation.com/forums/viewtopic.php?f=180&t=45726#p408528He talked about 3d text - not the Text labels -so that won't work.
Problem is - once you use entities.add_3d_text it just generates a bunch of faces and edges. There is no way of knowing that it belong to something that came from .add_3d_text.
@descoteaux: can you explain a bit more about what your plugin is doing?
Is the 3d text the only thing in the group? Or is there more?If it's the only thing in the group then it's easy - you just need to check if there is any geometry in there.
I made a plugin that let you edit 3d text (if it's made with the plugin): http://extensions.sketchup.com/en/content/3d-text-editor
The text is contained within a group or component - and I add attributes with the settings I used to create the text so I can later detect that the group/component is 3d text and what the settings where to create it. -
Give your 3dText 'commentgroup' a unique 'attribute' to identify it.
commentgroup.set_attribute('my_commentgroup', 'id', true)
Then when you go to 'renew' it, delete it if it exists usingentities.grep(Sketchup;;Group).each{|g| if g.get_attribute('my_commentgroup', 'id', nil) g.erase! break ### assuming there's only ever going to be one found ??? end }
-
Thanks for your help everyone! TIG's advice did the trick.
Also thomthom, thank you for pointing out your editable 3d text plugin. I've wished it existed for a long time. Awesome work!
Advertisement