How do I use linetool.rb script?
-
I am interested in adding mouse (and cursor) info as part of my ruby script inputs. So I thought I'd study the linetool.rb to see how it works.
But how do I use it? I know it's part of Examples that comes with Sketchup and can be activated with extensions (I think) but where do I select the tool and use it?
What am I missing? I feel like I'm asking a really basic (dumb) question here.
Marc
-
Here is a MUCH more basic version of a tool. Essentially you just need to make a class, then activate your tool using the method
Sketchup.active_model.select_tool(My_Tool.new)
. Pass an object of your class as shown. That is how you start your tool. Below is an entire example of a functioning tool. Open the ruby console when you run this tool and it will display things as you move the mouse.` class My_Tool
def activate
puts "Been Activated"
enddef onMouseMove(flags, x, y, view)
puts "onMouseMove: flags = " + flags.to_s
puts " x = " + x.to_s
puts " y = " + y.to_s
puts " view = " + view.to_s
endend
Sketchup.active_model.select_tool(My_Tool.new)`
There is also a thread where we talked a little bit about this here:
http://forums.sketchucation.com/viewtopic.php?f=180&t=26039
Read through that, it might be a little more helpful than just my quick example. Good luck, come back with more questions as needed
Chris
-
Thanks for jumping in to help me here Chris. However, I'm very new to Ruby and still confused. I copied your routine into my ruby file as a start. But I don't see how you use it?
That is, for the Examples->Box script (box.rb), it was straight forward since it puts a menu item for Box under the Draw menu using the UI.menu command. Then you select it and enter the parameters.
In your example (like the linetool.rb example that comes with Sketchup) I don't see how you "activate" it. What menu, tool palette, etc. do you select to get this script running? Or do I manually load it from the Ruby console? If so, how?
I'd prefer to see a completed piece of code (just a little more fleshed out from your example) that makes this script a tool menu selection and tells me when it reads a mouse click or tells me the point position as an example. Could you show me such code?
Marc
-
Well, if you play around with Sketchup enough, you can answer your own questions. I think I did it. I just put my Sketchup.active_model.select_tool() command with a UI.menu. Did I do this right?
**UI.menu("Tool").add_item("my tool") {
Sketchup.send_action "showRubyPanel:"
Sketchup.active_model.select_tool(My_Tool.new)
}class My_Tool
def activate
puts "My tool has been activated"
enddef deactivate(view)
puts "My tool has been deactivated."
view.invalidate if @drawn
enddef onMouseMove(flags, x, y, view)
puts "onMouseMove: flags = " + flags.to_s
puts " x = " + x.to_s
puts " y = " + y.to_s
puts " view = " + view.to_s
enddef onLButtonDown(flags, x, y, view)
puts "left mouse button clicked."
enddef onLButtonUp(flags, x, y, view)
puts "left mouse button released."
endend #end of class My_Tool
~**
-
Yeah, that looks about right. Generally the menu loading portion comes down at the end. But I guess if it works, I can't think of a reason why it would have to be at the end, as long as the script gets a chance to load before you try to call any of the methods defined below.
So that is a very basic tool. Look around at the other methods of the tool class. Many are straightforward - just add them and they will do their thing (onmousemove fires each time the mouse moves, etc). But some are trickier. Just ask questions if something is not working or not making sense. Many of us are not programming gurus. SketchUp ruby is my first programming language.
Chris
-
@chris fullmer said:
Generally the menu loading portion comes down at the end.
I put mine at the beginning so it's immediately visible to anyone viewing the code where the UI entry points are.
-
The 'convention' is
Usage Notes
Require Code
Main Code
Menu Codebut some 'renegades' like to move things around - it doesn't really matter - best if Notes are at the start I think, and I can see the efficacy of having the menu immediately following that though...
-
Okay. Yes, I've programmed quite a bit in C++ and it would make more sense to define your classes first and then put the main code afterwards.
But how do I convert these x, y coordinates into something useful, like RGB (or true x,y,z) cartesian coordinates? I know is has something to do with the InputPoint method.
My next step would be to change the cursor to something different so the user knows he's got my tool. Isn't this an option most people like to have?
-
@mgianzero said:
But how do I convert these x, y coordinates into something useful, like RGB (or true x,y,z) cartesian coordinates? I know is has something to do with the InputPoint method.
Yes - you create in InputPoint object and call the .pick method with the
x
andy
arguments given from the Tool's mouse events. After the pick you can extract the various data you want. For X,Y,Z useinputpoint.position
.Note that the InputPoint class uses the SU inference system. If you don't want infering you can use the PickHelper.
-
@thomthom said:
@mgianzero said:
But how do I convert these x, y coordinates into something useful, like RGB (or true x,y,z) cartesian coordinates? I know is has something to do with the InputPoint method.
Yes - you create in InputPoint object and call the .pick method with the
x
andy
arguments given from the Tool's mouse events. After the pick you can extract the various data you want. For X,Y,Z useinputpoint.position
.Thanks for the comments. But could you please show exactly what you mean by example? Perhaps using my code above and modify it.
Sorry but I find that I can follow these suggestions better when I have a piece of code I can work off of.
-
@mgianzero said:
@thomthom said:
@mgianzero said:
But how do I convert these x, y coordinates into something useful, like RGB (or true x,y,z) cartesian coordinates? I know is has something to do with the InputPoint method.
Yes - you create in InputPoint object and call the .pick method with the
x
andy
arguments given from the Tool's mouse events. After the pick you can extract the various data you want. For X,Y,Z useinputpoint.position
.Thanks for the comments. But could you please show exactly what you mean by example? Perhaps using my code above and modify it.
Sorry but I find that I can follow these suggestions better when I have a piece of code I can work off of.
Hi Marc,
I have just started leaning ruby and not a programmer, so can't help with programming info. But look at Plugins\Utilities\utilitiestools.rb as an example. You have to activate the extension "Utilites Tools". You use the tool from the Tools menu in sketchup => Tools/Utilities/"Query Tool". It displays cursor position in the bottom title space and the VCB.
Using that as an example, helped me to write a plugin. With a little cut and paste and trial/error. My plugin just rotates everything, that is initially selected, to be parallel with an axis. You select, two points to indicate a line. And the line provides the angles for the rotation.
-Kwok
Advertisement