How to run SketchUp extensions?

Your “drawbox.rb” needs to wait until the model object is valid.

One way to do this to use an AppObserver. The plugin submodule can itself be an AppObserver object if it has the needed callback methods.

During the startup cycle on Windows, a new empty model is created before the extensions load.
But on Mac, the empty model is not created until after the extensions load.

So you need a trigger to tell you when the model is valid and ready. That trigger might be the
AppObserver#expectsStartupModelNotifications callback, which should not be called until the model object is ready. But anyway, if this callback returns true, then the AppObserver#onNewModel callback will definitely be called when there is a valid and ready empty model object.

So again looking at the 2nd example above, let us add in AppObserver methods, and attach the module itself to the application as an observer instead of adding a manual menu item …

"WenTao_SomePlugin.rb"

module WenTao
  module SomePlugin

    extend self

    @loaded = false if @loaded.nil?

    def periodic_task(model)
      # do stuff 
    end

    def onNewModel(model)
      @timer_id = UI.start_timer(15.0, true) do
        periodic_task(model) # gets called every 15 seconds
      end
    end

    def expectsStartupModelNotifications
      return true # fire the onNewModel callback at end of load cycle 
    end

    if !@loaded
      # Attach this module itself as an AppObserver object during load cycle:
      Sketchup.add_observer(self)
      @loaded = true
    end

  end
end
1 Like