Sketchup Ruby CodeCamp - Final
Sketchup Ruby CodeCamp - Final
CodeCamp
Mikhael Johanes
Automating routines
and repetitive activities
Why Scripting?
Design experimentation
by multiple versioning
Lupton & Phillips, 2008
Lupton & Phillips, 2008
Reas, 2010
Reas, 2010
Terzidis, 2006
Script as Active Instructions
interactin
Installing
Atom Code Editor
https://atom.io/
Create a “CodeCamp” folder
Set the folder as Project Folder
You will see CodeCamp Folder is set as
Project Folder
1. Create new file: File → New File
4. Save File as “ ”
5. You should see the “ ” file in “CodeCamp” folder
Open the Sketchup Ruby Console
Type:
https://www.tutorialspoint.com/ruby/ruby_syntax.htm
Ruby Arithmetic Operators
Operator Description Example
+ Addition − Adds values on a + b will give 30
either side of the operator.
https://www.tutorialspoint.com/ruby/ruby_operators.htm
Create a new file and save as “ ”
!= Checks if the value of two operands are equal or not, if values are not equal then condition (a != b) is true.
becomes true.
> Checks if the value of left operand is greater than the value of right operand, if yes then (a > b) is not true.
condition becomes true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition (a < b) is true.
becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes (a >= b) is not true.
then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes (a <= b) is true.
then condition becomes true.
<=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is (a <=> b) returns -1.
greater than the second and -1 if first operand is less than the second.
=== Used to test equality within a when clause of a case statement. (1...10) === 5 returns true.
.eql? True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns true, but 1.eql?(1.0) is
false.
equal? if aObj is duplicate of bObj then aObj
True if the receiver and argument have the same object id. == bObj is true, a.equal?bObj is false
but a.equal?aObj is true.
Create a new file and save as “ ”
Ruby Assignment Operators
Operator Description Example
= Simple assignment operator, assigns values from right c = a + b will assign the value of a + b into
side operands to left side operand. c
Always define a variable name in lowercase and use underscore (_) instead of [space]
This is allowed:
Hint:
don’t use single variable for the boxes
Use variable and instead
Creating a box using single variable
Save as “ ” to “ ”
Set as a value to
define the dimension of the
box
Save as “ ” to “ ”
a = 100 mm
b = 30 mm
3*a
5*a
10 * b
Creating box using origin point
Save as “ ” to “ ”
) is used to
create a Point3d object, and
assigned to
5*a
origin
a
Save as “ ” to “ ”
Create a box with origin point at the center of the box
P2 = …
P3 = …
P4 = …
Repetitions, Loops, and Iterations
Save as “ ”
https://www.tutorialspoint.com/ruby/ruby_loops.htm
Save as “ ”
50
60
70
80
90
.
.
.
150
Hint:
create a loop from 5..15
multiply it by 10
Creating a series of boxes using loop
Save as
“ ” to
“ ”
Try to create :
3*a
5 boxes with distance that
a correspond to the width
a
a 5*a
of the box
a
a
Randomise
→ random float between 0 and 1
Save as
“ ”
to
“ ”
Assign with
random integer from 1..10
Put variable to
correspond with random
integer
Save as
“ ”
to “ ”
0 1 2 3
i
Grid of boxes
Start from
“ ” (series
of boxes),
save as “mygeom_06_grid.rb”
save as “exercise_06_grid_rand_height.rb”
Exercise yourself with
your own idea!
exercise_06_your_idea
exercise_06_your_idea
Generating choices
Start from “
Save as
By adding and
Save as
Transformation
Translation (move)
Rotation
Scale
Translation
Start from
“ ” (making
a group of solid box)
Save as
“ ”
Remember:
Full rotation =
Start from
“ ”
Save as “ ”
Remember:
Full rotation =
Boolean Operation
Start from “ ”
Save as “ ”
Save as “ ”
Start from “ ”
Save as “ ”
https://ruby.sketchup.com
https://ruby.sketchup.com/Geom.html
https://ruby.sketchup.com/Geom/Point3d.html
https://ruby.sketchup.com/Sketchup.html