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

    Retrive the tool selected before launching my plugin

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 458 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      tools = Sketchup.active_model.tools
      current_tool_id = tools.active_tool_id
      # may be 0 if no active tool, so watch out!
      # this might happen just after SketchUp starts
      
      # you can get active tool name IF, and only IF
      # active_tool_id != 0. If it is 0 and you call
      # active_tool_name, you cause a BugSplat! crash.
      current_tool_name =( tools.active_tool_id != 0 ? tools.active_tool_name ; "SelectionTool" )
      

      Then set your custom tool as the current tool,
      when done you can use the previous id or name
      to call Sketchup.send_action but
      you have to have a action string hash, because
      some of then differ from the names returned by
      the Tools.active_tool_name method.

      See these posts for a lookup hash examples:
      http://forums.sketchucation.com/viewtopic.php?t=18124

      In this one (below) you'd have to cut & paste the hash into your OWN namespace:
      (Do not distibute it so that it modifys the Sketchup::Tools class. It's an example for Google/Trimble only, of how the API class might be modified in the future.)
      http://forums.sketchucation.com/viewtopic.php?f=315&t=36177

      Also be aware that if the previous tool was a custom tool, then the Tools.active_tool_name method ALWAYS returns the string "RubyTool".
      IF this happens you can call the previous tool on Windows by passing the tool id as an integer argument (not a string,) to Sketchup.send_action(), but not on Mac/OSX.

      I'm not here much anymore.

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

        You can also use a Tools observer subclass:
        https://developers.google.com/sketchup/docs/ourdoc/toolsobserver

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • K Offline
          kwalkerman
          last edited by

          I think what you want to do is re-select the tool the user originally had selected when they activated your plugin. To do it:

          original_tool = Sketchup.active_model.tools.pop_tool
          
          myTool.activate
          
          # Now execute your code.  When you're finished, maybe in the tool's deactivate method;
          
          Sketchup.active_model.tools.push original_tool
          

          --
          Karen

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

            Karen that will not work (presently) as the pop_tool() method is bugged. It returns boolean, not the tool object. (I think I entered an API bug for this.) It can also set up the situation where the tool stack is empty if there was only 1 tool in it, and the active_tool_id is 0 until some valid tool is selected.

            In addition the push_tool() method only accepts a UI::Tool class object (or the API dictionary says that is what it expects. But since there really is no UI::Tool protoclass, does the method do any typechecking at all?)

            Apparently not as:
            Sketchup.active_model.tools.push_tool 21013
            Does not select the Component Placement tool, but it returns true anyway.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • N Offline
              njeremy2
              last edited by

              I'm gonna test it right now πŸ˜„

              1 Reply Last reply Reply Quote 0
              • N Offline
                njeremy2
                last edited by

                Hmmmmmmm
                I a little bit hard to use ...

                To begin easily, I made a plugin which display the name of selected tool
                I will show you the progress πŸ˜‰

                1 Reply Last reply Reply Quote 0
                • N Offline
                  njeremy2
                  last edited by

                  Hmmm, it's little bit weird :

                  When I retrive the ID of the tool with your solution, I obtain an ID number but when I launch a second time the plugin, I have a second ID number. WHY ??? πŸ˜’

                  But, I've try this:

                  @model = Sketchup.active_model
                  @tools = @model.tools
                  @name = @tools.active_tool_name
                  @name2 = "select#{@name};"
                  
                  Sketchup.send_action "#{@name2}"
                  

                  It works for most of tools except for LineTool (it's called "SketchTool" instead of "Linetool"), polygon, axis tool.

                  Maybe if I can retrive the ID with "@id = @tools.active_tool_id" and "Sketchup.send_action (@id)" it should be better but how do we do ?

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

                    @njeremy2 said:

                    Maybe if I can retrive the ID with @id = @tools.active_tool_id and Sketchup.send_action (@id) it should be better but how do we do ?

                    1) Yes you CAN use id, but ONLY on PC/Windows.

                    It does NOT (currently) work for Mac/OSX.
                    2) NEVER put a space between the method name and the ( of it's argument list.

                    I'm not here much anymore.

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

                      @njeremy2 said:

                      It works for most of tools except for LineTool (it's called "SketchTool" instead of "Linetool"), polygon, axis tool.

                      We know. This is why I told you (in post http://forums.sketchucation.com/posting.php?mode=quote&f=180&p=405232#pr404805 ) that you need a send action lookup hash.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • N Offline
                        njeremy2
                        last edited by

                        @dan rathbun said:

                        @njeremy2 said:

                        It works for most of tools except for LineTool (it's called "SketchTool" instead of "Linetool"), polygon, axis tool.

                        We know. This is why I told you (in post http://forums.sketchucation.com/posting.php?mode=quote&f=180&p=405232#pr404805 ) that you need a send action lookup hash.

                        ahhh ok !! lol sorry !

                        You write too much text and google translation doesn't translate very well ! πŸ˜„

                        1 Reply Last reply Reply Quote 0
                        • N Offline
                          njeremy2
                          last edited by

                          Now, I'm using name to identify the tool

                          For this moment it works πŸ‘

                          I don't understand the ID method, maybe when I feel ill at ease with ruby, I can understand how it works

                          Thank you Dan and Karen !!

                          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