Yes, see the comment at the top of the file snippet.
How are you running the code?
Look at the conditional expressions within the context menu handler creation block.
(1) The model’s selection must not be empty and only a single object must be selected.
(2) The selected object (obj
) must have your extension’s dictionary attached …
… otherwise the context menu will not have the “Edit Box…” command added.
Re your latest main file snippet:
A. It is customary to have the definition of local constants at the top of the first file loaded ("main.rb"
in this case.)
B. Also, the requiring of other support files are usually also up near the top of the first file. Very often, SketchUp extensions use a dedicated “loader” file that does nothing but require all the other files of the extension. But this is not absolutely necessary. The first file can load all the others.
But ensure that needed resources (like local constants, methods, etc.,) are defined before any of the support files code might reference them.
C. Any and all calls to create GUI objects should be within the “run once at startup” load guard conditional block.
So, this also means your call to the Toolbar_add()
method, (which should be named toolbar_add()
.)
The main reason for this is so you can use the global load()
method to reload individual files of your extension as your tweak and test them during development. Closing and restarting SketchUp after making a code change will get mighty tedious very quickly.
D. Your naming of methods and files lacks a consistent convention. In Ruby, method names are all lower case within words separated by underscore.
E. Files normally follow the method naming convention for like 97 percent of .rb
files, (referring to Ruby’s standard library.) There have been some issues from time to time where files load in a weird order because upper case characters collate differently than lower case. So, it is convention that .rb
filenames are all lower case, words separated with underscores.
Now the natural alpha load order (of course) does not come into play when your code is controlling the load order via explicit require
statements. But does effect the load order of all the extensions in the "Plugins"
folder.
Another deviation in file naming can be the SketchUp extension registrar scripts in the "Plugins"
folder. They must match the extension subfolder name, and usually (for most extension authors) parrot the module namspacing names, which as SnakeCase (aka CamelCase.) But yes, some authors (notably the Trimble SketchUp team,) will downcase even the registrar file and extension folder name (and usually abbreviate their Sketchup
namespace to "su_"
in file and folder names.) In my opinion, using abbreviations increases the chance of conflicting extension files.
F. Your show_dict
method (if it is part of the extension,) can also use the DICT
constant.
Also directly using the #attribute_dictionaries
method with a chaining #[]
call is dangerous. If an object does not have any dictionary objects attached, then the return value is nil
and the NilClass
does not have a #[]
method defined. So you can get a NoMethodError
with a "undefined method '[]' for NilClass"
message.
It is much safer to test using Entity#attribute_dictionary()
, but it is also good defensive coding to always test the result for nil
. You might also get a NoMethodError
with a "undefined method 'each' for NilClass"
message, so adding in a conditional to test for nil
and output a "No such dictionary"
message is “playing it safe.”