I have code that explodes selected groups and components down to the lowest “leaf” group or component, leaving these lowest groups and components un-exploded. Think of a bunch of rectangular solid groups/components that represent board and panels, which are built-up into assemblies of nested groups and components, like furniture or buildings. I want to explode these assemblies to their most basic parts in one step so that they may be laid flat and nested for CNC manufacturing,
The following code works, but I’ve been trying to figure out exactly why a subsequent undo operation will bugsplat if there are groups at the root context level. If editing is within a group or component (read, the active_entities is a group or component and not the root model) and break_apart is called and then undone, all is fine. It’s the presence of a top-level group in the selection that cannot be reconstructed by SU during the undo operation.
I have done quite a bit of troubleshooting…
- It happens in both the last release of 2023 and the new 2024 release
- It happens with different files and different models
- It happens when run from the ruby console in 2023 or 2024, and when run from Ruby Console+ and Ruby Code Editor in 2023
- It happens when only the default plugins are loaded (tested in 2024 because I haven’t migrated my 2023 setup to 2024 yet)
- Loading the “Bomb” extension from Smustard and exploding all groups and components also causes the undo crash
But, it does not happen when I have only components at the root level. To work around the crash, I added the following test and conversion to turn any groups into components right before the explode::
grp.to_component if grp.instance_of? Sketchup::Group
I’m familiar with issues about grouping entities in the root context and in differing contexts, but this is an “ungroup” operation I’m doing. I have read past posts where Colin was chasing ungroup crashes, so maybe there is some fundamental issue that has never been fixed in SU that has to do with the unique behavior of groups?
Looking for any insight or confirmation on this really odd crash behavior. Thanks.
def break_apart(ents)
@explodable = []
def recurse_entity(ent)
if (ent.instance_of? Sketchup::ComponentInstance) or (ent.instance_of? Sketchup::Group)
if (ent.definition.entities.grep(Sketchup::ComponentInstance).length + ent.definition.entities.grep(Sketchup::Group).length) != 0
@explodable.push(ent)
# TO DO: Need to deal with case of group instances being non-unique?
ent.definition.entities.each{ |e|
recurse_entity(e)
}
end
end
end
ents.to_a.each{ |ent|
recurse_entity(ent)
}
while !@explodable.empty?
grp = @explodable.pop
# NOTE: Had to add the group.to_component below to avoid an undo crash if the selection contains a group at the root model context
grp = grp.to_component if grp.instance_of? Sketchup::Group
grp.explode if grp.valid?
end
end
model = Sketchup.active_model
sel = model.selection
model.start_operation('Break Apart', true)
break_apart(sel)
model.commit_operation
Here’s a test file that I’ve used. Three “crates” with two of them being grouped. Comment-out the grp.to_component line, select the group, break_apart and then undo. You should see the crash.
nested components.skp (2.5 MB)