EntitiesObserver inside not detecting ComponentInstance inside Group

I’ve been using EntitiesObserver to detect if an ComponentInstance is added or removed to the entities set. But I found that EntitiesObserver cannot detect the ComponentInstance if it is added or removed inside a Group or a DynamicComponent. So I add the same EntitiesObserver to the Groups and Components. Unfortunately the onElementAdded and onElementRemoved is not called if a ComponentInstance is added or removed, although it worked for Edge and Faces.

I found a “workaround”, by adding the Group and Components EntitiesObserver first before the active_model EntitiesObserver the methods will be called properly. But I cannot do that to the new components that is added after

Use the DefinitionObserver to detect when instances are added.

A hybrid model observer example …

# encoding: UTF-8

module Author
  module SomeExtension

    extend self

    class MyHybridModelSpy < Sketchup::ModelObserver

      def initialize(model)
        watch_definitions(model)
      end

      def onComponentInstanceAdded(definition, instance)
        puts "onComponentInstanceAdded(#{definition}, #{instance})"
      end

      def onComponentAdded(definitions, definition)
        definition.add_observer(self)
      end

      def watch_definitions(model)
        model.definitions.each {|cdef| cdef.add_observer(self) }
        model.definitions.add_observer(self)
      end

    end # hybrid model observer

    # AppObserver callbacks:

    def expectsStartupModelNotifications
      return true
    end

    def onNewModel(model)
      MyHybridModelSpy.new(model)
    end

    def onOpenModel(model)
      MyHybridModelSpy.new(model)
    end

    if !defined?(@observer_initialized)
      # Add this module as the application observer:
      Sketchup.add_observer(self)
      # Set the flag to true:
      @observer_initialized = true
    end

  end
end

:bulb: