In SketchUp Pro 2015, I’ve got a two ComponentInstances, contained within a group. When selecting a ComponentInstance, I want to be able to go up the heirarchy, looking at the parent groups. However, entity#parent is not working as I’d expect:
Unfortunately, no simple way. SketchUp keeps track of outside-to-inside nesting because that’s what it needs to draw Entities, but not inside-to-outside, which the engine doesn’t need. Various people have tackled this problem, though none of the solutions are a simple one-liner. Perhaps one of them will chime in…
Because definitions have entities collections, and instances do not.
What may be throwing you off (a bit) here, is that the Group class has an API “shortcut” method that is basically:
This is because groups are really “special” component instances, whose definitions are hidden from the listing in the Component Manager.
So groups (being instances) do not really own entities either. It is their definitions that “own” the entities collection.
Secondly, you are likelyconfounding two separate API methods of the same name. Just because they both use the name “parent” does not mean they do the same thing. (There is, at this time, at least 4 different “parent” methods in the API.)
So calling the parent upon an instance of the Sketchup::Entities collection class, can return different object references than calling the same method upon a Sketchup::Entity subclass instance. Likewise if called upon a Sketchup::AttributeDictionary object (which always returns an instance of a Sketchup::AttributeDictionaries collection.)
But yet there is no way to (easily) set the current editing context in Ruby. The API defines a method to exit or back out of editing contexts, but not enter them.
So I believe that hacks are done .
You save the current selection, and the active context. Then use a system call to cause (simulate) a mouse click upon the object you want until the model.active_entities == target.definition.entities then get the active editing path from the method described above.
Afterward you do an undo n number of times, or back out of the editing context and restore the previous one.
This may provide some ideas. There may be better methods, but I just map out the entire model.
This maps the entire model into an array of Instance Nodes which have a reference the ComponentInstance and its path (or parent Instances). From the path you can get to the parent instance, and also calculate the instance’s transformation in world coordinates.
Use a Hash instead of an Array for faster lookups. I included the Instance name to be able to find instances by name. You can include whatever data about the instances you need.
In the parent instances array will be a component definition that is the group. The component definition will have the method group? set true. Since this is a top down lost of parents, the last item in the list should be the parent group if one is available.
group = entity.parent.instances[-1] if entity && entity.parent.typename == "ComponentDefinition" && entity.parent.group?
Should return the group, or nil if it isn’t in a group. Remove the entity.parent.group? if you want the parent component instead.
g = e.parent.instances[-1] if e && e.parent.typename == "ComponentDefinition" && e.parent.group?