Ruby code and Automatic Sketchup

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

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…

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

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

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

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…

1 Like

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

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?

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 for some light reading…

john

1 Like

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. :wink:

Continuing the discussion from Ruby code and Automatic Sketchup:

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(y2+x2)
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