Creating a group inside of a group with existing entities

I apologize for the revisit, but I’m still having difficulty navigating this. I’m building some Classes, and one of my Classes inherits functionality from another and extends a function using super. That all works fine, my child Class creates the same base geometry as the parent Class and I’m working to modify it. What I have:

@entity  # Sketchup::Group
container # Sketchup::Entities, container = @entity.entities

I need to put all of the entities from container into a subgroup - I intend to create a second subgroup, add entities directly to that, and then use a solid operation to subtract the subgroups with the intent of ending up again with raw geometry inside @entity.

I’ve gone through the numerous threads:
Add an array of entities into an existing group
Understanding the move an existing group into another group Challenge
Entities.add_group causes Bugsplat!
Move all entities to new group

So what I’ve tried:

model = Sketchup.active_model 
entities = model.active_entities

newgroup = entities.add_group(container.to_a)  # working in active context (I hope)
# the above line also duplicates & offsets the geometry by the bounding box
instance = container.add_instance(newgroup.definition, IDENTITY) # move instance to desired context
newgroup.explode

That results in ungrouped raw geometry and a group within a group of the same geometry except the groups are offset by the bounding box of the original geometry. I’m almost there, but the offset is driving me insane and I’m not sure how to fix it. I know that’s consistent with the bug @eneroth3 posted here but maybe I’m not working in the current context? Attempts to explode ‘instance’ result in a massive crash, not even a BugSplat (SU2021).

UPDATE: I am still curious about learning what I’m doing wrong, but in the meantime I created a new group in active_entities, added entities to that, then did the solid operation with @entity and my new group, assigning the output difference operation back to @entity and I’m able to get on with my life.

Are you trying to move a group (@entity) from one entities collection to another?

If not, where are @entity coming from? A group your extension created? Or a group the user created and you got a reference to?

If you are trying to “move” a group then you can do something like this:

model = Sketchup.active_model 
entities = model.active_entities

group = entities.grep(Sketchup::Group).first # Just grab the first group we find

model.start_operation("Move Group", true)
new_group = entities.add_group
new_group.entities.add_instance(group.definition, group.transformation) # Create a copy
group.erase! # Move the original group - effectively "moving" the group.
model.commit_operation

Thanks for the reply Tom! Yes, @entity is coming from a group my extension created. Thanks for the help, that does it. Maybe I had been looking at it too long, I was really close but I was adding an instance to my existing entities as opposed to the new group. It seems really obvious now. Since my extension uses the instance variable @entity as the group, I just added a line so @entity = new_group after I erase the original @entity.

Also, it seems like the IDENTITY transformation works fine as well when adding the instance, but that is probably only true if the group has not been moved from where it was originally created. Using the group’s transformation is a safer approach for all situations, is that correct?

I think so yes, in the general case. But this all depends on the particulars of what your extension is doing.