• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

How do I select the built-in Rotate Tool in a script?

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 2 Posters 445 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.
  • J Offline
    johnwmcc
    last edited by 16 Oct 2008, 23:51

    I'm trying to adapt an existing script to add a step at the end, after it creates and inserts a component, leaving it selected. I want to allow the user to rotate the inserted component about the insertion point.

    I thought it ought to be possible to get the script to activate the built-in Rotate tool, locate it at the component insertion point, ensure is is rotating about the blue axis, then let the user rotate the component to a picked angle, or a value in the VCB.

    But I just can't find any tutorial or sample script that illustrates this - though I've hunted the forums for several hours looking. I'm having real difficulty in general getting into Ruby programming, for want of examples of how to USE the Class and Method definitions beyond the simple examples included in the Plugins/Examples, though I've used other object-oriented programming languages in the past. And other people's scripts are easy to adapt in some ways, but only if I understand how they work, which isn't always easy for me.

    Can anyone help please?

    I've got as far as identifying the selected component:

    model = Sketchup.active_model
    ss = model.selection
    

    Then what? I found that the name of the tool I want is RotateTool, by selecting it in the drawing area, then running the script line in the Ruby console

    name = Sketchup.active_model.tools.active_tool_name

    but I can't see how to activate it from the script.

    model.select_tool seems only to apply to user-defined tools - at least, model.select_tool RotateTool or RotateTool.new both give errors.

    tools=model.tools just returns a set of currently defined (?user) tools, but I can't see what to do with it. It doesn't have a method called RotateTool.

    Perhaps I'm on the wrong track altogether - do I need instead to apply a Geom::Transformation in some way to the selected object instead? If so, I then have to implement something to find the insertion point and pick the angle, and again, I can't find an example to show me the way.

    Any ideas for a rookie Ruby programmer gratefully received.

    JohnMcC

    1 Reply Last reply Reply Quote 0
    • T Online
      TIG Moderator
      last edited by 17 Oct 2008, 07:00

      Sketchup.send_action "selectRotateTool;"
      

      Most tools are capable of being activated using 'send_action'...

      TIG

      1 Reply Last reply Reply Quote 0
      • J Offline
        johnwmcc
        last edited by 17 Oct 2008, 08:40

        Many thanks for such a prompt and helpful reply. I'll try it later today - have to go out shortly now.

        As a more general question, how could I have found this out for myself? I'm not (I think) stupid, but I don't recall seeing anything anywhere I looked in Ruby tutorials, or examples, that would have pointed me to this solution. I suppose, with hindsight, I could have seen that there is a Sketchup class, send_action method, but how should I have known to look there?

        JohnMcC

        1 Reply Last reply Reply Quote 0
        • T Online
          TIG Moderator
          last edited by 17 Oct 2008, 10:45

          @johnwmcc said:

          Many thanks for such a prompt and helpful reply. I'll try it later today - have to go out shortly now.

          As a more general question, how could I have found this out for myself? I'm not (I think) stupid, but I don't recall seeing anything anywhere I looked in Ruby tutorials, or examples, that would have pointed me to this solution. I suppose, with hindsight, I could have seen that there is a Sketchup class, send_action method, but how should I have known to look there?

          JohnMcC

          http://download.sketchup.com/OnlineDoc/gsu6_ruby/Docs/ruby-sketchup.html#send_action

          The API help is not always very helpful though... everyone asks questions now and again...

          TIG

          1 Reply Last reply Reply Quote 0
          • J Offline
            johnwmcc
            last edited by 17 Oct 2008, 16:04

            That's a very useful reference, thanks again. Now I know how to select ANY programmable action. But it doesn't seem to include the action I now think I have to perform first, before rotating.

            I've experimented with the command to select the Rotate tool, and find a further problem in what I'm trying to do. If I just add the script line to choose the RotateTool at this point, it interrupts the place_component command which was the previous line in the script, where the user was left to pick the insertion point. So the inserted component disappears, not inserted, while the Move tool that is waiting for a click to place the component is replaced by the Rotate tool in the user interface, with nothing now selected to place OR rotate.

            Is there a way to tell Ruby to wait till the user has completed the previous command (to pick the insert point), before then selecting the Rotate tool to rotate the inserted component? I can't see where to look for how to do this, and if you can point me in the right direction, I'd be very grateful. I've searched both in Google in general, and in this forum, for different permutations of the words 'sketchup ruby wait user input' without success so far.

            The only hint I've found, which looks extremely complex, is to define a new tool, based on, for example, the supplied scripts Linetool.rb or the TrackMouseTool in utilities.rb which can do some or all of the picking within the script. But it seems a lot of work, when the right one-line command might do what I need here, if only I could see what it might be!

            JohnMcC

            1 Reply Last reply Reply Quote 0
            • T Online
              TIG Moderator
              last edited by 17 Oct 2008, 16:53

              Try an 'observer' that waits until the 'place' is done... then do the 'rotate' ?

              .

              TIG

              1 Reply Last reply Reply Quote 0
              • J Offline
                johnwmcc
                last edited by 17 Oct 2008, 18:54

                I get the general idea. Can you point me to a script that uses an observer in this way, or something like it? As always, the hardest part for me is seeing how the various classes and methods work in practice, and how to string them in a usable sequence.

                In principle, I assume I set up an observer to start watching before the place_component, wait till it 'sees' a pick to place the component, then somehow 'lets go' after the pick, to allow the rotate tool to be selected. But the details of how to do that are still not easy for me to work out just from the class and method definitions. Many of them have either no examples, or extremely basic ones that do no more than illustrate simple syntax, with little idea of the semantics or meaning of the code, or how to build up to more complex uses.

                Anyway, I'll have a go.

                JohnMcC

                1 Reply Last reply Reply Quote 0
                • J Offline
                  johnwmcc
                  last edited by 17 Oct 2008, 22:20

                  Well, progress of a useful kind. I've worked out from the sketchy API for observers how to define an observer to monitor the 'onComponentInstanceAdded' event, and got it to pop up a messagebox when the user has picked the insertion point after the script generates and inserts the component.

                  Now to add the code to select the Rotate tool, instead of the messagebox (which is where I came in!)

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

                  Advertisement