[Code] Group.real_parent

[Code] Group.real_parent

Postby thomthom » Wed Jun 10, 2009 3:03 pm

Problem: Group.entities.parent doesn't always return the correct definition.

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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code] Group.real_parent

Postby thomthom » Thu Sep 30, 2010 6:33 pm

Standalone version - doesn't extent the base class and also returns the definition for Group, ComponentInstance and Image entities:

Code: Select all
# Returns the definition for a +Group+, +ComponentInstance+ or +Image+
   #
   # @param [Sketchup::ComponentInstance, Sketchup::Group, Sketchup::Image] instance
   #
   # @return [Sketchup::ComponentDefinition]
   # @since 2.0.0
  def self.definition(instance)
    if instance.is_a?(Sketchup::ComponentInstance)
      # ComponentInstance
      return instance.definition
    elsif instance.is_a?(Sketchup::Group)
      # Group
      #
      # (i) group.entities.parent should return the definition of a group.
      # But because of a SketchUp bug we must verify that group.entities.parent returns
      # the correct definition. If the returned definition doesn't include our group instance
      # then we must search through all the definitions to locate it.
      if instance.entities.parent.instances.include?(instance)
        return instance.entities.parent
      else
        Sketchup.active_model.definitions.each { |definition|
          return definition if definition.instances.include?(instance)
        }
      end
    elsif instance.is_a?(Sketchup::Image)
      Sketchup.active_model.definitions.each { |definition|
        return definition if definition.image? && definition.instances.include?(instance)
      }
    end
    return nil # Error. We should never exit here.
  end
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 8 guests

Visit our sponsors: