sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Observer Usage

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 203 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      hanks
      last edited by

      This may be a stupid question to some of you wizards, but can anyone tell me why this code does not work when pasted into a .rb file and dropped into the plugins directory?

      straight out of the API...

      
      # This is an example of an observer that watches tool interactions.
           class MyToolsObserver < Sketchup;;ToolsObserver
             def onActiveToolChanged(tools, tool_name, tool_id)
               UI.messagebox("onActiveToolChanged; " + tool_name.to_s)
             end
           end
      
           # Attach the observer.
           Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
      
      

      This is the resulting error...

      @unknownuser said:

      Error Loading File section_view.rb
      undefined method `tools' for nil:NilClass

      The script works perfectly however if I call the last part manually using the Ruby Console...

      Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
      

      I suspect that my inability to understand this line of the class intro from the API is why I'm lost...

      @unknownuser said:

      To implement this observer, create a Ruby class of this type, override the desired methods, and add an instance of the observer to the Tools object.

      WTF does override the desired methods mean!!??

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        I think it means active_model is nil, which I guess is a possibility on Macs. What happens if you wrap the code in a function and call it from the Ruby Console?

        Hi

        1 Reply Last reply Reply Quote 0
        • H Offline
          hanks
          last edited by

          Thanks Jim. Your intuition was right - a call from within a function worked. It's annoying that the API isn't a little clearer about this but I am a noob and there are many things that remain unclear to me.

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            Hanks, lots of us are going through it. There is a thread at the top of this forum called API typos and something or another. If you have suggestions for the API, put it in that thread. The Google guys who write the API and SketchUp Ruby watch that thread and incorporate the suggestions and typos that are posted there. If it was confusing to you, it probably will be for someone else!

            Chris

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement