Starting a new Ruby Tool Extension
-
What would be a good example script or scripts to study before starting to program a new Tool extension?
I'd like it to be compatible with versions of SU at least back to 2014 (Ruby 2) and perhaps to v8.
And I'd like it to follow 'best current practice' (or at least 'good current practice').
I do know some of the basics, like how to set up an Extension loader. And to wrap my code in a uniquely named Module.
And I have a couple of published plugins in this site's Plugins store. But they were merely updated to cope with Ruby 2, and probably are no longer current best practice.
Years ago, I started from the then Example linetool.rb, but even the most recent one from the Extension Warehouse doesn't seem to include some of the newer features, such as pdf or svg icons for Su2016.
I've looked at Angular Dimension from Steve Baumgartner, which does include them, so perhaps I can use that as a starting point for that aspect.
But his example installs to the Plugins folder and looks for support files there. For development, at least, I want to be able to run it from an external folder, to share with different versions of SU for testing purposes. What would example code to do that look like?
Is there a 'skeleton' tool plugin example that would have all the basics in it, including the basic picking of input points, which isn't trivial to set up from scratch?
And something with a good example of the Tools 'draw' method for displaying temporary geometry during the use of the tool. I have struggled (and only partially succeeded) in getting a simple version of that to work.
Most of the supplied su_xxxx.rb plugin loaders have scrambled code in their support folder.
If there isn't such a skeleton (and I haven't been able to find one yet, though I've seen pieces of what such a thing might contain on this and the Sketchup Community forum), could one of the experienced SU team members or talented Ruby script writers suggest one, or point me to a few unscrambled Tool plugins they would recommend as good examples to follow?
The Ruby Learning Resources sticky post on this forum is still dated 2008, and even the latest topics in it seem to date back to 2011, so I haven't found them of much help in tackling some of the later developments, though the basics are still of course very relevant.
PS. Just looked again at the Learning Resources - there ARE later posts from earlier this year by Dan Rathbun, including Templates... but they are still empty.
-
John McClenahan,
I agree with Joh (Driven) that the linetool.rb is a good starting place.
It comes with SU 7 and SU 8.I use Dan's start file concept with a few small changes.
The linetool.rb is missing a few things - such as mouse cursor and setting up a tool bar.
-
I touch on a couple of your points...
@johnwmcc said:
...But his example installs to the Plugins folder and looks for support files there. For development, at least, I want to be able to run it from an external folder, to share with different versions of SU for testing purposes. What would example code to do that look like?.
most rubies can be loaded by simply typing
load "<path to file>"
and you can even drag the file between the quotes to get the path... [on a mac a least]
to reload, use the up arrow and hit return, again and again and again....
For the icons you just do a version and platform check and have different images in the same place...
ver = Sketchup.version.to_i if ver >= 16 osx = Sketchup.platform == ;platform_osx ext = osx ? '.pdf' ; '.svg' else ext = '.png' end cmd.large_icon = cmd.small_icon = File.join(File.dirname(__FILE__), 'Resources', 'images', 'sine_circle#{āext}")
@unknownuser said:
a good example of the Tools 'draw' method for displaying temporary geometry
name a plugin with what you think is a good example...
most I have looked into use the linetool class as a template...
john
-
One more thing - I too like to test on multiple versions of sketchup with a single plugin install.
I test with SU 7, SU 8, SU 2013, SU 2014, SU 2015 and SU 2106
I create a 2 line ruby file that I name loader.rb - it contains:
require 'sketchup.rb'
require_all('c:/users/public/documents/sketchup')Then I put my various plugins into the shared folder 'c:/users/public/documents/sketchup' and treat it as the same as the plugins folder for each Sketchup version.
I then run a batch file that prepares and copies all the necessary files into THAT folder.
-
Thanks to all. That has probably given me enough to get started. And there is a more recent but still several years-old (copyright 2012) linetool.rb on the Extension Warehouse as part of the SU Examples download. I'll see how I get on using that as a model.
If I can, over coming weeks rather than days, I'll see if I can create a skeleton for more general use. But perhaps that isn't as simple or maybe even as useful as I first thought!
I already use a 'loader.rb' file as suggested above. What I was trying to ask was what the code would look like so that the extension isn't restricted to having its support folder and files in SU's Plugin folder. And I think you have given me something to try instead.
-
AND...
There are hundreds of non-encrypted RB files in the SCF PluginStore's various RBZs, which are 'Tools' etc and can offer you valuable insights [aka: 'cribs'] into how certain aspects of SketchUp's Ruby and Tools etc are handled by their authors...
[Of course mine are the best ] -
I agree looking at other scripts is a worthwhile experience...
as my tag line says...
john
-
@johnwmcc said:
... What I was trying to ask was what the code would look like so that the extension isn't restricted to having its support folder and files in SU's Plugin folder.
File.join(File.dirname(__FILE__), File.basename(__FILE__, ".*"), 'the_file_your_after' )
have it anywhere for any version of ruby...
you'll often see it split so you can use the different bits for other things...
dir = File.dirname(__FILE__) base = File.basename(__FILE__, ".*") file = 'the_file_your_after' target = File.join(dir, base, file)
john
-
Of course, @TIG, you are quite right... and I use several of yours.
Sent from my SM-G935F using Tapatalk
-
I had started last week on some basic tutorial - included how to set up a basic extension. And one for a a basic line tool.
I attached the line tool example - got lots of comments. Let me know what you think.
-
Here's my 2 cents
Good that you show start_operation and commit
I think it is easier to teach when things are a bit more formal so I use () parenthesis for all functions.
Also I prefer to instantiate InputPoint in the initialize
Also you may want to pass things into the tool such as language dictionaries etc.
I think showing how to set up a tool bar is a good idea for a tool
Perhaps even a context menu for editing (right click).def initialize() @mouse_ip = Sketchup;;InputPoint.new() path = File.dirname(__FILE__) cursor_icon = File.join(path, 'my_cursor.png') @cursor_id = UI.create_cursor(cursor_icon, 0, 32) # for VCB parsing - need to know the delimiter @delim = ("3,7".to_l rescue false) ? ';' ; ',' end def activate() reset_tool(nil) end def onCancel(reason, view) reset_tool(nil) view.invalidate() end def reset_tool(view) @mouse_ip.clear() if (view) view.tooltip = nil end end def onSetCursor() UI.set_cursor(@cursor_id) end def enableVCB?() return true end def onUserText(text, view) # parse a couple of values end end unless file_loaded?(__FILE__) # best practice to set this early - especially if a timer is involved file_loaded(__FILE__) # add toolbar # add menu end
-
@garry k said:
...I think showing how to set up a tool bar is a good idea for a tool
If we are discussing best practice for an Extension that provides a tool, than I believe the 'menu' items should be separate from the tool and be used to initialize the tool...
The Extension .rb loads the lang dictionary and the menu loader code and that's all...
the menu code should load the tools logic and it runs...
if never triggered during a session it is not even loaded...
my 2 pence...
john
-
The example focused on writing a tool. Tool are so complex that in an example I didn't want to muddle everything else into it. In the examples I'm writing I was a separate "tutorial" that describe in detail how to structure the folders and register the extension and add a menu.
Toolbars and localization are good separate topic - each deserving their own attention. I had thought I'd put it on GitHub once it was more fleshed out - but maybe I should try to put out the WIP since we already have a topic on this going here.
As for parenthesis - that's a code style and that is something people have to pick for their own. I used explicit parenthesis myself because I was familiar with other languages that always required them. But in Ruby this isn't the norm. I found that it was easier to just use normal convention for the language - as sharing code and adopting example code required less work.
For Ruby code I use the GitHub style guide which seem to be the de-facto standard. IDE's normally base their inspection rules on this and you'll hear a lot of squawking if you use a different style - unless you spend the time to reconfigure the style rules.
Advertisement