How to reproduce:
1. Make a group
2. Copy the group a couple of times. (Entity Info will display the number of copies)
Each of these copies all refer to the same definition via .entities.parent
3. Open one of the groups - you don't even have to edit it. It's now unique. However, it's .entities.parent isn't correct.
If you save the model and reload it, the references will be corrected. But until the user do so, you get the wrong parent via the API.
Warning
Don't use this example as-is. Modifying base classes is not good practice.
Workaround:
- Code: Select all
class Sketchup::Group
# Some times the group.entities.parent refer to the wrong definition. This method checks
# for this error and locates the correct parent definition.
def real_parent
if self.entities.parent.instances.include?(self)
return self.entities.parent
else
Sketchup.active_model.definitions.each { |definition|
return definition if definition.instances.include?(self)
}
end
return nil # Should not happen.
end
end
Usage: group_entity.real_parent
I haven't dared to make it build a cache of mis-referenced group definitions. I'm afraid it might cause problems if I keep hold of entity references. (What I suspect is causing problems for my DoubleCut plugin.) So for the time being it will have to search the definitions each time it comes across a mis-reference. Optimise your code to call the method as few times as possible.