Add a group to an existing group

I want to add a selected group to a group that already exists. How can I do that?

selectedGroup = Sketchup.active_model.selection.first
group = Sketchup.active_model.active_entities.add_group(selectedGroup)

  • this creates a new group. Instead, I want to add the selectedGroup to an existing group named “myGroup”
ents = Sketchup.active_model.active_entities
found = ents.grep(Sketchup::Group).find {|g| g.name == "MyGroup" }
if found
  clone = found.entities.add_instance(selectedGroup.definition,some_transform)
  clone.material= selectedGroup.material
  # If selectedGroup has attribute dictionaries you'll need
  # to iterate them and copy them to the new clone group.
  clone.name= selectedGroup.name
  # Lastly, erase the source group:
  selectedGroup.erase!
end

Since I do not know anything about your groups, I have no idea how you wish to position the clone within the target group. You will need to produce some_transform to position the clone.

See: Geom::Transformation


FYI: [How to] Post correctly formatted and colorized code on the forum?

Also see past topics …

Search Ruby API category …
https://forums.sketchup.com/search?q=copy%20group%20category%3A13

Sorry, but I didn’t understand what you meant by ‘some_transform’

You need to provide a Geom::Transformation object (or an array that can be coerced into a transform) as the 2nd argument to the Entities#add_instance method.

The some_transform reference is this Geom::Transformation object reference in the above code example.

1 Like