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

    Identify/locate the Subclass

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 68 Views 2 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.
    • A Offline
      Anton_S
      last edited by

      During coding I came up to the point where I needed to figure whether the selected object is a sublcass of a class. I mean like Sketchup.active_model.select_tool only accepts subclasses, right? And it only works if the tool includes some of its methods, such as onMouseMove.

      I can predict how it works. Maybe it uses @selected_tool.include?(certain_method), If method not inclduded, then the tool jst ignores the callback to current message. But with this assumption the ...select_tool method will also accept anything, but not nil, as it describes in the API description.

      Is there a way to actually know whether the selected tool is actually a subclass of a class?

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Specifically.. about SketchUp API Tool classes:

        There is NO defined Tool superclass.

        That means that your custom tool class definition will be a subclass of Object.

        Now, because of that.. Sketchup.active_model.select_tool() does NOT do any typechecking, other than to check if a nil argument was passed.

        So.. when you pass an object to Sketchup.active_model.select_tool() (and it's not nil,) SketchUp expects it to have the minimum of the Tool class callbacks. (I think activate would be the minimum.)

        To ask an object if it has a public activate method:

        obj.respond_to?(:activate)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          About classes in general:

          The logical methods can apply at the class level.

          For instance, class Class, is a subclass of class Module.

          so: Class < Module returns true

          and: Class > Module returns false

          further, Object is THE supreme class, the common progenitor of all other classes.
          so: Class > Object returns false
          and Object is always greater than any other class.

          So IF you decide to set up your own tool protoclass... Anton::Tool
          and you wanted to see if a specific tool instance was a subclass:

          nifty_tool.is_a?(Anton::Tool)
          or
          nifty_tool.class < Anton::Tool

          ... would return true, if nifty_tool's class was a descendant class of Anton::Tool.

          If you wanted to see a list of nifty_tool's ancestors:
          puts nifty_tool.class.ancestors

          💭

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • A Offline
            Anton_S
            last edited by

            @dan rathbun said:

            obj.respond_to?(:activate)

            Thanks Dan, description - sure will help.

            Edited: Previously, I used obj.methods.include?(someMethod). - That will give an error if the object wouldn't be a class, nor module. But using respond_to? will work on any selected object without errors. Jst true or false

            I was wrong in blue words. Actually, .methods can be implemented in any object. 134.methods, nil.methods, "dsfg".methods, false.methods, Class.methods will always work.

            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