[code] Detecting when a user changes the axes

This is an example of a simple hybrid observer that detects when the singleton Axes object is changed.

# encoding: UTF-8
#
# A simple Axes Observer to detect when a user changes axes.

module Author
  class AxesSpy < Sketchup::EntityObserver
    
    @@instance ||= self::new

    def self::attach( mod = Sketchup.active_model )
      mod.axes.add_observer(@@instance)
    end

    def onNewModel(mod)
      self::attach(mod)
    end
    alias_method :onOpenModel, :onNewModel

    def expectsStartupModelNotifications()
      true
    end

    def onChangeEntity(ent)
      puts "AxesSpy: #{ent.inspect} changed for #{ent.model.inspect}."
    end

    Sketchup.add_observer(@@instance)

  end
end

* Note that the Axes object was first exposed with SketchUp 2016.

Edit: Fixed alias_method call. args were swapped, in error.
(I always seem to get them backwards. I always think “alias this as that”,
but it is more like “newalias that of this”.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.