I know this may have been covered several times already but, not necessarily in one place and in a “This is the best way to do that” sort of way. Can you help me gather current best practices and strategies about this quirky aspect of the API into one place?
Please forgive me if I have misunderstood or misstated the actual facts below as I am trying to understand this. Ideally, I can update this post to be more accurate and useful for future users if you can let me know what needs to be revised.
- You have several existing groups and/or components at the root level in a particular Sketchup model.
- Within those root-level groups and/or components, there are many more nested groups and/or components, each which may have their own nested groups and/or components, and so on.
- You would like to search the entire model and copy every group and/or component at all levels of nesting with a certain attribute into a new parent group
- the new parent group will be created at the root level
It does not matter WHY you want to do this, you just do. Maybe you want a copy of every doorknob in an entire hotel project to be pulled out of their variously nested locations so you can analyze knob surface area… whatever.
Problems (as I understand them):
- There is no
copymethod. Most strategies describe a workaround of some sort and @barry_milliken_droid came up with this custom method Copy entities (@last) but I am not sure if this works across contexts.
- The API is apparently incorrect in its description of the
.add_group()method. It can accept “an Entity, a list of Entities, or an array of Entities” as it’s argument. @slbaumgartner Move all entities to new group
- MOST IMPORTANTLY: Sketchup will Crash when attempting to “copy” the groups and/or components into the new parent group if:
the groups and/or components you want to “copy” already exist in that same context
the context you want to “copy” them to is anything other than
Sketchup.active_model.active_entities(basically you can’t have group A open for editing and attempt to “copy” one of it’s child groups into another group B at say the root level.)
It appears that you can workaround the first Crash problem by using
.add_instance() instead of
add_group() and access the group or component’s definition entities (which do not exist in the active model context).
The second Crash condition appears to be more tricky to workaround. If you want to “copy” a child group or component to your new parent_group at the root level, you are obviously working across contexts.
What are the best workarounds to this second issue?
Ideally there is a way of “copying out” to the root level but that seems to be a non-starter. What if I created an array of matching groups and/or comps as I probe the various levels of the model and then attempt to create a collection of “handles” to copy all matching entities later while at the root level? This is where I appear to be MOST stuck.
Also the Transformation Problem
I also understand that when working between nested and un-nested contexts, there is an issue with positioning where the transformation of each entity needs to be combined with its parent’s transformation. I wanted to see if I can make headway on the first issue before trying to work through this second one because there is a lot to unpack here (for me at least).