Hey all, I’m new in programming for SketchUp and I start to follow a video tutorial in youtube of a “stairs maker plugin”. new I try to make it abit better for my purpose and I came out with a problem.
I want the end result will be that the staircase I create will be grouped (I also want that each step will be grouped which I already manage to do)
This is my code:
require 'sketchup.rb'
SKETCHUP_CONSOLE.show
UI.menu("Extensions").add_item("My First Pluing") {
UI.messagebox("Lets Start!")
prompts = ["Number of Staris"]
defaults = ["1"]
input = UI.inputbox(prompts, defaults, "Staricase Creator")
#calling all methods
draw_staris(input)
}
def cm_to_inch(x)
x = x / 2.54
return x
end
def draw_staris(input)
#vars
staris = input[0].to_i
rise = cm_to_inch(17)
run = cm_to_inch(28)
width = cm_to_inch(110)
#GEt handle to our model
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
#loop across this code
for step in 1..staris
x1 = 0
x2 = width
y1 = run * step
y2 = run * (step + 1)
z = rise * step
#create our points using arrays
pt1 = [x1,y1,z]
pt2 = [x2,y1,z]
pt3 = [x2,y2,z]
pt4 = [x1,y2,z]
new_face = ent.add_face pt1, pt2, pt3, pt4
step_group = ent.add_group(new_face)
new_face.material = "red"
new_face.pushpull rise
end
end
any help will be blessed.
btw:
I couldn’t find good tutorials for studying.
I can use the documentations but for a beginner, it’s a bit difficult. there is any good tutorial or book for this specific purpose?
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 …
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:
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
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.)