100% found this document useful (1 vote)
108 views88 pages

Sketchup Ruby CodeCamp - Final

The document provides an introduction to scripting in Sketchup using Ruby. It discusses reasons for scripting such as automating routines and enabling design experimentation through multiple versioning. It then covers setting up the Atom code editor and Ruby development environment for writing Sketchup scripts. The document demonstrates some basic scripts for printing messages, performing calculations, and creating geometric shapes. It also introduces Ruby concepts like variables, comparisons, assignments and loops.

Uploaded by

agliekaje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
108 views88 pages

Sketchup Ruby CodeCamp - Final

The document provides an introduction to scripting in Sketchup using Ruby. It discusses reasons for scripting such as automating routines and enabling design experimentation through multiple versioning. It then covers setting up the Atom code editor and Ruby development environment for writing Sketchup scripts. The document demonstrates some basic scripts for printing messages, performing calculations, and creating geometric shapes. It also introduces Ruby concepts like variables, comparisons, assignments and loops.

Uploaded by

agliekaje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 88

Sketchup Ruby

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

“giving specific instructions


to the computer with which they are
interacting”
Burry, 2011 – Scripting Cultures

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

2. Right Click -> Copy Full Path on “Code Camp” folder

3. Type in the newly created file

4. Save File as “ ”
5. You should see the “ ” file in “CodeCamp” folder
Open the Sketchup Ruby Console
Type:

Sketchup Plugin folder will be shown afterwards

Identifying Sketchup’s Plugin Folder


Copy – Paste “load_codecamp.rb”
to Sketchup’s Plugins folder

Restart your Sketchup


My First Script

1. Create a new file and save as “ ”


1. Open Ruby Console
2. Type load “helloworld.rb” in console
You’re success to connect
your script to Sketchup!
General ruby tutorial:

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.

− Subtraction − Subtracts right a - b will give -10


hand operand from left hand
operand.

* Multiplication − Multiplies a * b will give 200


values on either side of the
operator.

/ Division − Divides left hand b / a will give 2


operand by right hand operand.

% Modulus − Divides left hand b % a will give 0


operand by right hand operand
and returns remainder.

** Exponent − Performs a**b will give 10 to the power


exponential (power) calculation 20
on operators.

https://www.tutorialspoint.com/ruby/ruby_operators.htm
Create a new file and save as “ ”

Run the script in console by typing:


Pay attention to the results inside the console
Ruby Comparison Operators
Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (a == b) is not true.

!= 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

+= Add AND assignment operator, adds right operand to the c += a is equivalent to c = c + a


left operand and assign the result to left operand.

-= Subtract AND assignment operator, subtracts right c -= a is equivalent to c = c - a


operand from the left operand and assign the result to left
operand.
*= Multiply AND assignment operator, multiplies right c *= a is equivalent to c = c * a
operand with the left operand and assign the result to left
operand.
/= Divide AND assignment operator, divides left operand c /= a is equivalent to c = c / a
with the right operand and assign the result to left
operand.
%= Modulus AND assignment operator, takes modulus using c %= a is equivalent to c = c % a
two operands and assign the result to left operand.

**= Exponent AND assignment operator, performs c **= a is equivalent to c = c ** a


exponential (power) calculation on operators and assign
value to the left operand.
Create a new file and save as “ ”
Things to remember on defining a local variable

Always define a variable name in lowercase and use underscore (_) instead of [space]

This is allowed:

This is not allowed


(do not start your variable with number)
(do not start your variable with capital letter)
(this will be processed by Ruby, but camelCase style is not the convention in Ruby)
Creating geometries in Sketchup
Create a new file “ ”
Save as ” to “ ”

Create two boxes with:

Box 1: origin point at (0, 0, 0),


w 100 mm
l 200 mm
h 300 mm

Box 2: origin point at (100 mm, 0, 100 mm)


w 300 mm
l 400 mm
h 100 mm
Creating geometries in Sketchup using group
Save as “ ” to “ ”
Save as “ ” into “ ”

Put the two boxes into each group

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 “ ”

Modify the script to create


a box with shown ratio

a = 100 mm
b = 30 mm
3*a

5*a

10 * b
Creating box using origin point
Save as “ ” to “ ”

Set a as of the box

) is used to
create a Point3d object, and
assigned to

The origin point components (x ,y


z) can be retrieved one by one by
using:
3*a

5*a

origin
a
Save as “ ” to “ ”
Create a box with origin point at the center of the box

P1 = (origin.x - width/2), (origin.y - length/2), (origin.z - height/2)

P2 = …

P3 = …

P4 = …
Repetitions, Loops, and Iterations

Save as “ ”

https://www.tutorialspoint.com/ruby/ruby_loops.htm
Save as “ ”

Create a loop that shows this in your Sketchup Ruby Console:

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
“ ”

Put the whole script within the loop


from

Set the x component of origin point


to :
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

→ random integer in 9..20

→ random float between 1.4 and 2.5


Create a new file as “ ”
Boxes with random value

Save as
“ ”
to
“ ”

Assign with
random integer from 1..10

Put variable to
correspond with random
integer
Save as
“ ”
to “ ”

Random width from


0.5 to 2.0

Random height from


1..10

Random length from


1..10
Save as “ ”
(box with center origin point)
to “ ”

Set the box ratio to 1:1:1 with

Modify it into loop of 10 boxes

Put random and position to


origin point
Save as “ ”
(box with center origin point)
to “ ”
Save as “ ”
(box with center origin point)
to “ ”
Exercise yourself with your own idea!

Printscreen your results and save as


2 dimensional grid: loop in loop
Save as

4 [0, 4] [1, 4] [2, 4] [3, 4]

3 [0, 3] [1, 3] [2, 3] [3, 3]

2 [0, 2] [1, 2] [2, 2] [3, 2]

1 [0, 1] [1, 1] [2, 1] [3, 1]

0 [0, 0] [1, 0] [2, 0] [3, 0]


j

0 1 2 3
i
Grid of boxes
Start from
“ ” (series
of boxes),

save as “mygeom_06_grid.rb”

Set the box ratio to 1:1:1

Create a loop within existing loop

Set the origin point of the boxes to


correspond with and value
Put random value on its height

save as “exercise_06_grid_rand_height.rb”
Exercise yourself with
your own idea!

exercise_06_your_idea

Printscreen your results and save as

exercise_06_your_idea
Generating choices
Start from “

Save as

Create two loop and outside the


existing loop

Set the and to correspond with

and components of boxes’ origin point

By adding and

to the origin point definition


Exercise yourself with
your own idea!

Save as
Transformation

Translation (move)

Rotation

Scale
Translation
Start from
“ ” (making
a group of solid box)

Save as
“ ”

Set the ratio of the box


accordingly

Copy the group and apply


translation to the copied
group
Rotation
Start from
“ ”
Save as
“ ”

Remove the translation


portion from the script and
add rotation
transformation
Scaling
Start from
“ ”
Save as
“ ”

Add scale transformation


to the rotated group
Start from
“ ”
Save as
“ ”

Create a series of box in z axis

Set the ratio accordingly

Apply rotation incrementally


from the bottom:

Remember:

Full rotation =
Start from
“ ”
Save as “ ”

Create a series of box in z axis

Set the ratio accordingly

Create a new outer loop j

Apply rotation incrementally from the


bottom to top and left to right

Remember:

Full rotation =
Boolean Operation
Start from “ ”
Save as “ ”

Adjust the dimension of box

Create two boxes by using translation


transformation

Create a union from the two boxes


Start from “ ”

Save as “ ”
Start from “ ”
Save as “ ”

Changes the union method to subtract


Start from Save as “ ”
Ruby Math Module
https://ruby-doc.org/core-2.2.0/Math.html
Start from “ ”
Save as “ ”
Start from
“ ”
Save as
“ ”
Understanding Sketchup Ruby API

https://ruby.sketchup.com
https://ruby.sketchup.com/Geom.html
https://ruby.sketchup.com/Geom/Point3d.html
https://ruby.sketchup.com/Sketchup.html

You might also like