Thursday, September 25, 2014

#Ruby Day 1 - Baby Steps

This was actually my first time learning ruby language, and it's through sketchup. These are actually example of a script on how to make stairs. However, these stairs are actually fixed, built on the origin coordinate.

How to use it:

Open up you favourie notepad editor (a simple notepad will do) or you can download Notepad++ (Notepad++ download), copy and paste this script on your notepad. Save as .rb file. This will then make the file as ruby file.

Save the file name as "stairs" (...or anything you want). Put in on your plugins folder where your computer put all of your sketchup plugins folder. It's usually situated in this path if you're using SketchUp 2014 :(C:\Users\user\AppData\Roaming\SketchUp\SketchUp 2014\SketchUp\Plugins)

Save the file again on the notepad. Open up Sketchup.

There's a menu under the "Plugin" tab, "Draw Stairs". When you you click it it will state a command line on Ruby API, then it will execute the command. The resulting command actually produce 10 steps of stairs right on the origin.


Here's the script:

# Standard API
require 'sketchup.rb'

# Show Ruby console at start-up so we can see any programming errors
SKETCHUP_CONSOLE.show

# Add a menu item to launch our plugin.
UI.menu("Plugins").add_item("Draw stairs") {
 UI.messagebox("I'm about to draw stairs")
 # Call our new method
 draw_stairs
 }

def draw_stairs
# Create some variables
stairs = 10
rise = 8
run = 12
width = 100
thickness = 3

# Get "handles" to our model and the entities collection it contains.
model = Sketchup.active_model
entities = model.entities

#Loop across the same code several times
for step in 1..stairs

# Calculate our stairs corners
x1 = 0
x2 = width
y1 = run * step
y2 = run * (step + 1)
z = rise * step

# Create a series of "points", each a 3-item array containing x,y, and z
pt1 = [x1, y1, z]
pt2 = [x2, y1, z]
pt3 = [x2, y2, z]
pt4 = [x1, y2, z]

# Call methods on the Entities collection to draw stuff
new_face = entities.add_face pt1, pt2, pt3, pt4
new_face.pushpull thickness
end
end

It will look something like this on your notepad (or any note editor):



After executing the file it will look something like this (minus all of the other Plugins :P):



SOURCE:  Ruby Language Sketchup Tutorial - How to make stairs


GLHF!
AND ONCE AGAIN. THANK YOU GOOGLE!

No comments:

Post a Comment