Working with selection - ungrouped or within a group
-
(Posted this at the SketchUp forum but since its dissapearing I'd thought I'd post it here also)
If I want a script to work with a selection of either ungrouped objects or within a group.
Could something like this work?if selection.typename == "Group" { entities=group.entities }
else { entities=model.active_entities } -
No.
You cannot have a selection of entities in and out a group/component at the same time.Shetchup.active_model.active_entities returns the whole model if you aren't editing a group/component, and returns the collection of entities of the group/component if you are editing a group/component.
Shetchup.active_model.selection is completely different from that.
Hope this helps, -
I'll try to explain what I'm after.
Is it possible to make a script work with either the edges/faces in a selected group or the ungrouped edges/faces if there is no group?
I don't mean at the same time, but to recoginize if the selection is a group or not and be able to work either way.If not, I guess I'll have to make two scripts, one for groups and one without.
-
Yes it is doable.
For instance:
def editing_group?
if Sketchup.active_model.active_entities.parent.class == Sketchup::ComponentDefinition and Sketchup.active_model.active_entities.parent.group?
return true
else return false
endWill return a boolean to tell the script if the user is currently editing a group. BUT using methods of the selection class is independant of that.
Even if the user is editing a group, Sketchup.active_model.entities can be accessed the usual way, and Sketchup.active_model.active_entities will return a collection of entities of the active group/component.If you have to check wether selection is a group or not, use this test:
Sketchup.active_model.active_entities.parent.class == Sketchup::ComponentDefinition
(because group are stored as if they are components !)
and if you have to check wether a particular selected object is part of a group or not, use this test:if editing_group? and Sketchup.active_model.active_entities.include?( object_id ) ...
Hope this helps,
Advertisement