Tool Needed
-
I've yet to write my first SU tool. The time has come, perhaps.
I want to be able to point to something in my model and have its location returned
[r,g,b]
. (A tooltip would be nice, in the clipboard would be nicer, both would be great!)Is this a good first-time tool? And where does one start?
Thanks!
-
Probably the best place to start is "linetool.rb" since it comes with the distribution. Or any other tool script out there that fits the discription better.
PS "Just make sure its OSS so you don't break any Texas laws!! I think they still hang people down there!"
-
EDIT: fixed a few key mistakes that were pointed out later by Dan - Thanks Dan!
This is a tutorial I've been meaning to do for some time now. I think I've written up an outline a couple of times already, but so far I have not finished anything. But here is the very bare bones of making a tool.
` class MR_SCF_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(MR_SCF_Tool.new)`The onMouseMove method I put in there is directly copied from the API. And that is essentially the outline for writing a tool. Just make a class, and use the
Sketchup.active_model.select_tool(class_object)
method in your menu to activate your tool. And that is about all it takes. If you need to implement the draw method to draw anything to the screen (like inference dots and lines) then it starts getting more tricky I think. Anyhow, there is a start. I've also got some great very simple examples of how to use the draw method if you need it later.Chris
-
EDIT: fixed a few key mistakes that were pointed out later by Dan - Thanks Dan!
And I forgot to mention that you don't need any method defined, just the class and the select_tool method. This is a valid tool technically:
class MY_LAME_TOOL end Sketchup.active_model.select_tool(MY_LAME_TOOL.new)
Of course your tool will be useless without any methods in it. But you don't need the onMouseMove or activate methods. I just put them in as examples.
The
activate
method is called everytime the tool is selected from the menu/toolbar in SketchUp. So it is a great place to put the first processing of your script - validation, tool process flow, etc. AndonMouseMove
is useful to track the mouse, one of the main reasons to make a tool.So look at the tool class in the API to see all the methods you can add to your tool that SU will activate.
onResume
is useful,onLButtonUp
comes in handy,onKeyUown
,onReturn
, etc., are all very handy. -
@chris fullmer said:
...
Thanks ever so much, Chris! Wrestling the docs to extract "how to get started" is never fun.
-
@chris fullmer said:
The
activate
method is called everytime the tool is instantiated. So it is a great place to put the first processing of your script - validation, tool process flow, etc.Should read:
"The**initialize**
method is called ONCE whenthe tool is instantiated. So it is a great place to put the first processing of your script - validation, tool process flow, etc."The
**activate**
method is called everytime the tool is selected (from the menu or a toolbar button [via theModel.select_tool
method.]) This method usually sets the @tool_state variable to it's starting value (often 0,) loads the tool's cursor, and prompts the user to do something on the status bar.Other tips on methods:
You can (like any class,) define whatever private methods you wish, in addition to the predefined callback methods, in order to keep your code readable and easier to debug.
A reset method often helps keep things clear (some people just call
activate
internally instead, but I prefer a standalonereset
method.) -
@chris fullmer said:
Sketchup.select_tool(*toolclassobject*)
(in 3 different places.)Chris of course, meant to say:
Sketchup.active_model.select_tool(*toolclassobject*)
I disagree with putting the argument Toolclass.new in the select_tool method without a symbol.
It can easily lead to newbie's creating multiple instances of the tool (when only 1 is ever needed.) And without a symbol (variable) pointing at the tool, you can't easily access it to call public methods.
What public methods? Well I do something a bit weird. I define the tool's UI::Command object INSIDE the tool itself, within the initialize method. ( So the block for the command is {Sketchup.active_model.select_tool(self)}.) I then create several public methods, one called cmd that returns the UI::Command object so any of it's methods can be called, such as .menu_text; also an add_to_toolbar( toolbar ) and an add_to_menu( menu ) method, so the tool can be added to any number of menus or toolbars, after the fact, using the proper menutext and toolbar button images. I also have a set of params for the .new call, that includes (optionally) a menu (defaults to "Plugins") and/or toolbar which the tool will add itself to (internally calling the previously mentioned methods.)
-
Thanks as always for the corrections Dan! The Sketchup.active_model.select_tool I am not sure what was going through my head. I was convinced that method was in the SketchUp class.....yikes! I was just debugging the script I started earlier and realized that I was wrong. So thanks for pointing out my error.
Interseting ideas in the rest of the post there fo sure. I'll need to process some of the Toolbar ideas.
Chris
-
Advertisement