# Ruby code and Automatic Sketchup

**URL:** <https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101>\
**Category:** Developers\
**Created:** [January 13, 2016, 2:59am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101 "2016-01-13T02:59:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilwu21](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ilwu21/32/13740_2.png) [@ilwu21](https://forums.sketchup.com/u/ilwu21)\
**Post date:** [January 13, 2016, 2:59am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/1 "2016-01-13T02:59:18Z")

</div>

Hi my name is Jim and I am having a very hard time just displaying my first ruby Sketchup code on the sketchup page.  
I have Windows 8  
Sketchup 2016 pro

The code in the file below is basically copied out of the book " Automatic Sketchup "  
I am so new to Ruby that I can’t detect what is being left out.  
There must be something else needed to make it run.  
It is suppose to add an icosahedron to the sketch.

If I could just get over this hump, I would be so happy.  
If someone could point me in the right direction I would be forever indebted for your help.

mod = Sketchup.active\_model # Open model  
ent = mod.entities # All entities in model  
sel = mod.selection # Current selection  
view = mod.active\_view  
Ss = 1.11935599 # Short side  
Gr = 1.65742 # Long side

pt0 = Geom:: Point3d.new 0,Ss,Gr  
pt1 = Geom:: Point3d.new 0,-Ss,GS  
pt2 = Geom:: Point3d.new Gr,0,Ss  
pt3 = Geom:: Point3d.new -Gr,0,Ss  
pt4 = Geom:: Point3d.new Ss,-Gr,0  
pt5 = Geom:: Point3d.new -Ss,-Gr,0  
pt6 = Geom:: Point3d.new 0,Ss,-Gr  
pt7 = Geom:: Point3d.new 0,-Ss,-Gr  
pt8 = Geom:: Point3d.new Gr,0.-Ss  
pt9 = Geom:: Point3d.new -Gr,0,-Ss  
pt10 = Geom:: Point3d.new Ss,Gr,0  
pt11 = Geom:: Point3d.new -Ss,Gr,0

pm = Geom:: PolygonMesh.new 12, 20  
#Top half  
pm.add\_polygon pt0, pt1, pt2  
pm.add\_polygon pt0, pt1, pt3  
pm.add\_polygon pt1, pt4, pt5  
pm.add\_polygon pt1, pt4, pt2  
pm.add\_polygon pt1, pt3, pt5

# Middle

pm.add\_polygon pt4, pt5, pt7  
pm.add\_polygon pt2, pt8, pt4  
pm.add\_polygon pt10, pt11, pt0  
pm.add\_polygon pt3, pt9, pt5  
pm.add\_polygon pt2, pt8, pt10  
pm.add\_polygon pt2, pt0, pt10  
pm.add\_polygon pt9, pt5, pt7  
pm.add\_polygon pt7, pt8, pt4  
pm.add\_polygon pt11, pt9, pt3  
pm.add\_polygon pt11, pt3, pt0

# Bottom half

pm.add\_polygon pt6, pt7, pt8  
pm.add\_polygon pt6, pt7, pt9  
pm.add\_polygon pt6, pt10, pt11  
pm.add\_polygon pt6, pt10, pt8  
pm.add\_polygon pt6, pt9, pt11

# Draw face in the mesh

ent = Sketchup.active\_mod.entities  
ent.add\_faces\_from\_mesh pm

Thank you  
Jim

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [January 13, 2016, 4:01am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/2 "2016-01-13T04:01:51Z")

</div>

there’s a few un-needed things and a couple of typos…

it also makes reverse faces, but I’ll let you try a fix that…

```ruby
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
ss = 1.11935599 # Short side
gr = 1.65742 # Long side

pt0 = Geom::Point3d.new(0, ss, gr)
pt1 = Geom::Point3d.new(0, -ss, gr)
pt2 = Geom::Point3d.new(gr, 0, ss)
pt3 = Geom::Point3d.new(-gr, 0, ss)
pt4 = Geom::Point3d.new(ss, -gr, 0)
pt5 = Geom::Point3d.new(-ss, -gr, 0)
pt6 = Geom::Point3d.new(0, ss, -gr)
pt7 = Geom::Point3d.new(0, -ss, -gr)
pt8 = Geom::Point3d.new(gr, 0, -ss)
pt9 = Geom::Point3d.new(-gr, 0, -ss)
pt10 = Geom::Point3d.new(ss, gr, 0)
pt11 = Geom::Point3d.new(-ss, gr, 0)

pm = Geom::PolygonMesh.new(12, 20)

# Top half
pm.add_polygon(pt0, pt1, pt2)
pm.add_polygon(pt0, pt1, pt3)
pm.add_polygon(pt1, pt4, pt5)
pm.add_polygon(pt1, pt4, pt2)
pm.add_polygon(pt1, pt3, pt5)

# Middle
pm.add_polygon(pt4, pt5, pt7)
pm.add_polygon(pt2, pt8, pt4)
pm.add_polygon(pt10, pt11, pt0)
pm.add_polygon(pt3, pt9, pt5)
pm.add_polygon(pt2, pt8, pt10)
pm.add_polygon(pt2, pt0, pt10)
pm.add_polygon(pt9, pt5, pt7)
pm.add_polygon(pt7, pt8, pt4)
pm.add_polygon(pt11, pt9, pt3)
pm.add_polygon(pt11, pt3, pt0)

# Bottom half
pm.add_polygon(pt6, pt7, pt8)
pm.add_polygon(pt6, pt7, pt9)
pm.add_polygon(pt6, pt10, pt11)
pm.add_polygon(pt6, pt10, pt8)
pm.add_polygon(pt6, pt9, pt11)

# Draw face in the mesh
ent.add_faces_from_mesh pm

```

john

---

<div class="post-metadata">

**Author:** ![ilwu21](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ilwu21/32/13740_2.png) [@ilwu21](https://forums.sketchup.com/u/ilwu21)\
**Post date:** [January 14, 2016, 11:00pm UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/3 "2016-01-14T23:00:22Z")

</div>

Hi John, thank you for responding.  
I’m not sure what you meant but I’m thinking that the version of ruby that I have is not compatible with Sketchup 2016 pro.  
Thank you very much for your time.  
Jim

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [January 14, 2016, 11:11pm UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/4 "2016-01-14T23:11:44Z")

</div>

> [@ilwu21](#):
>
> the version of ruby that I have is not compatible with Sketchup

SU comes with it’s own Ruby built in…

it uses Ruby 2, with a couple of tweaks, so what you have installed elsewhere is not important…

you do need to run the code in SU’s ‘Ruby Console’ [RC] for it to work…

Mat’s ruby code is old, so need’s a little updating [that’s all I did]…

If you run mine in ‘RC’ you will see half the faces are reversed…

this is due to the order of the points given to SU, and half need re-ordering to face outwards…

john

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 14, 2016, 11:12pm UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/5 "2016-01-14T23:12:19Z")

</div>

Sketchup comes with its own Ruby version - v2.0 for v2016.

If you look at the example offered you will see some basic changes…

Ruby now fails if there is a space between parenthesized () arguments and the methods, or spaces in a command - like:  
`pt0 = Geom:: Point3d.new 0,Ss,Gr`  
which should be e.g.  
pt0 = Geom::Point3d.new(0, Ss, Gr)  
etc… then later on…  
pm.add\_polygon(pt0, pt1, pt2)  
finally  
`ent.add_faces_from_mes(pm)`

Although this original would not now be regarded as ‘good’ coding today…

---

<div class="post-metadata">

**Author:** ![ilwu21](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ilwu21/32/13740_2.png) [@ilwu21](https://forums.sketchup.com/u/ilwu21)\
**Post date:** [January 15, 2016, 12:02am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/6 "2016-01-15T00:02:36Z")

</div>

Thank you John, I’ve been beating my head against the wall not knowing what I was doing wrong.  
Now I can at least get started.  
Jim

---

<div class="post-metadata">

**Author:** ![ilwu21](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ilwu21/32/13740_2.png) [@ilwu21](https://forums.sketchup.com/u/ilwu21)\
**Post date:** [January 15, 2016, 12:06am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/7 "2016-01-15T00:06:17Z")

</div>

Thanks TIG, this has been such a great help. I just didn’t know where to turn. I am just getting started so I didn’t know where to turn.  
Thank again.  
Jim  
PS is there any good sites or books I should be reading?

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [January 15, 2016, 12:25am UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/8 "2016-01-15T00:25:41Z")

</div>

fill in your forum profile so we can offer some platform dependent advice regarding editors etc…

Mat’s book **is** good for a SU ruby overview, but some code examples will fail in Ruby 2…

there are downloadable example ruby’s and the [SU API](http://www.sketchup.com/intl/en/developer/docs/classes) for some light reading…

john

---

<div class="post-metadata">

**Author:** ![Barry](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/barry/32/1204_2.png) [@Barry](https://forums.sketchup.com/u/Barry)\
**Post date:** [January 15, 2016, 6:04pm UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/9 "2016-01-15T18:04:25Z")

</div>

> [@ilwu21](#):
>
> Automatic Sketchup

That book was released on March 8, 2010. In order to keep up, I’d look for something with online updates. Get to know sites like stackoverflow and others. Most developers do. As a matter of fact, 2 days ago, someone named “Jim” posted ‘Ruby code “Automatic Sketchup” icosahedron’, and it was answered by a person on the SketchUp team. 😉

---

<div class="post-metadata">

**Author:** ![ilwu21](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ilwu21/32/13740_2.png) [@ilwu21](https://forums.sketchup.com/u/ilwu21)\
**Post date:** [March 7, 2016, 8:49pm UTC](https://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/10 "2016-03-07T20:49:37Z")

</div>

Continuing the discussion from [Ruby code and Automatic Sketchup](http://forums.sketchup.com/t/ruby-code-and-automatic-sketchup/19101/5):

Hi Tig,  
I’ve gone back and revised the coding from “Automatic Sketchup”  
First I rescaled the golden rectangles so as to have a radius of one, making the trigonometry easer.  
I have created new code and have displayed all twenty triangles made into groups and then components.  
What I want to do next is to have all twety triangles be the same component. I have worked and worked on this and am not sure how to proceed.  
Is there a way to just go into the component info of each of the twenty triangles and make them the same identical component?  
Or is there a way to take one component triangle and rotate it into all the other nineteen positions so as to have twenty identical triangles?  
I have been studying my linear algebra and matrix mathematics and the Sketchup API but all the methods and translations and copies and transforms are confusing to get a grip on.  
I know this could be a difficult problem or a simple one but I could use some more direction at this point.  
Below I’ve added some of my code so you can see where I’m starting from.

mod = Sketchup.active\_model # Open model  
ent = mod.entities # All entities in model  
sel = mod.selection # Current selection  
t=Math.sqrt(5)+1  
gr=t/2 #Golden rectangle  
Cent\_ang\_rad=Math::atan2(1,gr) # converting the radius to one  
y=Math::sin(Cent\_ang\_rad)  
x=Math::cos(Cent\_ang\_rad)  
r=Math::sqrt(y **2+x** 2)  
ssh=y #," ssh = Short side"  
grh=x # Long side

#Golden rectangle face 1  
grt1=  
grt1[0] = [-grh,ssh,0] #P11  
grt1[1] = [grh,ssh,0] #P12  
grt1[2] = [grh,-ssh,0] #P13  
grt1[3] = [-grh,-ssh,0] #P14  
ent = mod.active\_entities # Add the face to the entities in the model  
group = ent.add\_group # Add the group to the entities in the model  
entities 1 = group.entities # Get the entities within the group  
face1 = entities1.add\_face grt1 # Add a face to within the group

#Golden rectangle face 2  
grt2=  
grt2[0] = [-ssh, 0,grh] #P21  
grt2[1] = [ssh, 0, grh] #P22  
grt2[2] = [ssh,0,-grh] #P23  
grt2[3] = [-ssh, 0,-grh] #P24  
ent = mod.active\_entities # Add the face to the entities in the model  
group = ent.add\_group # Add the group to the entities in the model  
entities = group.entities # Get the entities within the group  
face2 = entities 2.add\_face grt2 # Add a face to within the group

#Golden rectangle face 3  
grt3=  
grt3[0] = [0, -grh, ssh] #P31  
grt3[1] = [0, grh, ssh] #P32  
grt3[2] = [0, grh, -ssh] #P33  
grt3[3] = [0, -grh, -ssh] #P34  
ent = mod.active\_entities # Add the face to the entities in the model  
group = ent.add\_group # Add the group to the entities in the model  
entities = group.entities # Get the entities within the group  
face3 = entities 3.add\_face grt3 # Add a face to within the group

* * *

Now just two examples of triangles, all the rest are the same

# Tryangl 1

pt21=Geom::Point3d.new(-ssh,0,grh) #pt2(1)  
pt22=Geom::Point3d.new(ssh,0,grh) #pt2(2)  
pt31=Geom::Point3d.new(0,-grh,ssh) #pt3(1)  
#Tryangl 2  
pt22=Geom::Point3d.new(-ssh,0,grh) #pt2(1)  
pt31=Geom::Point3d.new(0,-grh,ssh) #pt3(1)  
pt14=Geom::Point3d.new(-grh,-ssh,0) #pt1(4)  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*ent =Sketchup.active\_model.entities  
pm=Geom::PolygonMesh.new 12,20

#adding triangles to the sketch.

# Tryangl 1

pm.add\_polygon pt21,pt22,pt31  
try1=  
try1[0]=[-ssh,0,grh]  
try1[1]=[ssh,0,grh]  
try1[2]=[0,-grh,ssh]  
ent=mod.active\_entities # Add the face to the entities in the model  
group=ent.add\_group # Add the group to the entities in the model  
entities 1=group.entities # Get the entities within the group  
face1 = entities1.add\_face try1 # Add a face to within the group  
group.to\_component

#Tryangl 2  
pm.add\_polygon pt21,pt31,pt14  
try2=  
try2[0]=[-ssh,0,grh]  
try2[1]=[0,-grh,ssh]  
try2[2]=[-grh,-ssh,0]  
ent=mod.active\_entities # Add the face to the entities in the model  
group=ent.add\_group # Add the group to the entities in the model  
entities 2=group.entities # Get the entities within the group  
face2= entities 2.add\_face try2 # Add a face to within the group  
group.to\_component
