How to group groups

Reassigned this topic thread to the Ruby API subcategory.


FYI, Please read how to post color lexed code in the forum …


As said many times here in other threads, the API documentation is a programmer’s reference.
It is not a teaching resource, and it’s code snippets range from worthless to erroneous.

See the wiki lists of resources for learning Ruby and then how the SketchUp API extends the Ruby core…

In addition … ALL of your code, for ANY of your extensions, must be within a company or author namespace module. Then each of your extensions should be in a separate submodule within your namespace module.

I long ago posted a simple template for Basic extensions …


This would need to be removed after development, or you could make it dependent upon a debug / dev flag variable in your module.

But it is probably easier to use a console opener extension.
https://extensions.sketchup.com/extension/461ee6e6-1387-45dc-a071-8f136ad2376b/eneroth-console-opener

You do not need to require other files in your file, unless your file uses features defined in that other file.

In this case … SketchUp itself will already have loaded all 3 of the “special” files in it’s Tools subfolder (ie, "sketchup.rb", "langhandler.rb", and "extensions.rb",) before any custom extension (including yours) has even begun to load.


Proper SketchUp extensions require a extension subfolder and a registrar script of the same name in the “Plugins” directory folder. See:


Looking at your code quickly …

1. Ruby uses 2 space indentation. Some of your indents are 3 spaces.

2. "Plugin" is misspelled "Pluing"

3. "Stairs" is misspelled "Staris"

4. "Staircase" is misspelled "Staricase"

5. Use the active entities and you never know at what level the user will want to create their stairs:

   ents = mod.active_entities

6. The “Lets Start!” messagebox is frivolous and the input dialog does a good job on it’s own of signalling the beginning of the command cycle. (Both of them are modal dialogs that force the user to deal with them, so just use the input dialog.)

7. Your menu item proc should only call a command method. This way you can tweak your method code, reload your main file, and it will redefine the command method. Your way, you have to exit SketchUp and restart the application just to see the new functionality.

Example …

    def create_staircase_command
      prompts = ["Number of Stairs"]
      defaults = ["1"]
      input = UI.inputbox(prompts, defaults, "Staircase Creator")
      # Always check if user cancelled:
      return unless input # evals falsely if user cancels inpubox
      # User did not cancel and input is an array, so draw the stairs:
      draw_stairs(input)
    end

… and down at the bottom of your module …

    unless @loaded
      UI.menu("Extensions").add_item("Create Staircase...") {
        create_staircase_command()
      }
      @loaded = true
    end

Notice how the creation of UI menu items (commands, toolbars and buttons) must be done within a conditional that only happens upon first load. This prevents duplicate menu items when you reload files during development (which you’ll be doing many times.)


8. You do not need to write your own unit conversion methods. The SketchUp API added many conversion methods to the Numeric class, which passes them on down to Integer, Float, etc.
See:

In addition, the UI.inpubox will correctly convert between model units and internal Length (inches).
This means you do not need to write your code specifically for metric or imperial units. Read ThomThom’s primer blog on dealing with units and inputboxes …

Oh … and my last thought is you’ll need to move rise, run and tread width into the inputbox so users can tailor the staircase to their own needs (and local building codes.)

1 Like