Source Code tut_hello_cube.rb
This is the contents of the tut_hello_cube.rb file with the comments stripped out of it that we will be examining in this part of the tutorial.
Registering Our Extension
Create a blank text file in your SketchUp Plugins folder. Name it
tut_hello_cube.rb. Use your favorite Ruby editor to edit the file.
This file is what call the "root" ruby file, or sometimes the "registration" file because it sits in your root plugins folder, it also registers your extension with SketchUp. We're pretty clever, 'eh?!
We expect that this file sets up everything needed to regsiter the extension with SketchUp and nothing else.
The first thing we need to do for our Extension is require a few ruby files provided by SketchUp. Both of these add functionailty that we'll use in the following lines of code. Add these lines to the top of your file:
Next we need to properly namespace your code. In SketchUp Ruby, all code is loaded into a single space. If you don't wrap your own code into your own module names, there is a very real chance that you will break someone else's code, or that they will break yours.
Next we'll guard against the Extension being loaded by SketchUp multiple times. We'll use the method
`file_loaded?` for this. Multiple loading can happen for various reasons - either by development debugging or extension updates etc.
__FILE__ constant is a "magic" Ruby constant that returns a string with the path to the current file. You don't have to use this constant with
file_loaded? - you can use any unique string to represent this file. But
__FILE__ is very convenient for this.
Next we define the extension. The two arguments are the extension name and the file that should be loaded when the extension is enabled.
Note that the file loaded (tut_hello_world/main) must be in a folder named with the same base name as this root file.
Another thing to notice is that we omitted the .rb file extension and wrote `tut_hello_world/main` instead of `tut_hello_world/main.rb`. SketchUp is smart enough to find the file regardless and this is required if you decide to later encrypt the extension.
Next we add some information to the extension. This isn't required, but highly recommended as it helps the user when managing it's installed extensions.
Finally we tell SketchUp to register this extension. Remember to always set the second argument to true - this tells SketchUp to load the extension by default. Otherwise the user have to manually enable it after installing.
This is needed for the load guard to prevent the extension being registered multiple times.