Beginner Ruby question - push/pull face while rotating (loop)


#1

I’m attempting one of my first Ruby Scripts. Total noob. I have some experience with programming though, so hopefully I’ll be able to follow. I’m trying to write a script that will generate a “vase” that I can use for 3D printing. Basicially, I want to manually draw a face in SketchUp, then I want the Ruby script to Push/Pull the face 1", then rotate the resulting face 15° (while autofolding). It would then loop through that multiple times to create the vase, very similar to the below image.

I really don’t know where to begin. I can’t figure out how to get a script to recognize a selected face, so for now I’m just drawing a square inside Ruby, and Push/pulling it. But from there, I don’t know how to “select” the new push/pulled face in order to rotate it, then Push/pull it again.

This is really lame, but this is pretty much just a copy/paste from the Ruby API

depth = 100
 width = 100
 model = Sketchup.active_model
 entities = model.active_entities
 pts = []
 pts[0] = [0, 0, 0]
 pts[1] = [width, 0, 0]
 pts[2] = [width, depth, 0]
 pts[3] = [0, depth, 0]
 # Add the face to the entities in the model
 face = entities.add_face(pts)
 face.reverse!
 status = face.pushpull(100, true)

So once I’ve generated the push/pull, how do I manipulate the new face? Let’s start there I guess… Any help would be greatly appreciated.


#2

Better to do it slightly differently if you want to rotate the top extruded face…
Make the geometry inside a group.

group=entities.add_group()
entities=group.entities

Do the pushpull.
The find top face:

top_face=entities.grep(Sketchup::Face).select{|e| e.normal==Z_AXIS }[0]

Now transform the face by a rotation - say 30.degrees.

trans=Geom::Transformation.rotation(top_face.bounds.center, Z_AXIS, 30.degrees)
entities.transform_entities(trans, [top_face])

Next use the top_face as the face, pushpull again find the new top_face and repeat the trans etc…
It’d be easier to do say a 12.times{ ### } loop to do the iterations once the first face is made…


#3

TIG, thank you so much, that was incredibly helpful! I was able to get the loop working and have control over the number of copies, rotation, etc.

Here’s what I ended up writing:

 depth = 100
 width = 100
 model = Sketchup.active_model
 entities = model.active_entities
 pts = []
 pts[0] = [0, 0, 0]
 pts[1] = [width, 0, 0]
 pts[2] = [width, depth, 0]
 pts[3] = [0, depth, 0]
 
 #Create new group called vasegroup
 vasegroup=entities.add_group()
 #Create object vaseentities for the entities inside the group
 vaseentities=vasegroup.entities

 # Add the face to the entities in the model
 face = vaseentities.add_face(pts)
 face.reverse!
 status = face.pushpull(100, true)
 
 #Find the top face 
 top_face=vaseentities.grep(Sketchup::Face).select{|e| e.normal==Z_AXIS }[1]
 #create rotation transformation
 trans=Geom::Transformation.rotation(top_face.bounds.center, Z_AXIS, 30.degrees)
 #Execute Rotation
 vaseentities.transform_entities(trans, [top_face])
 
#Create variable for number of copies
copies = 4
#Create counter
counter = 1
while counter < copies
  #Find top face
  top_face=vaseentities.grep(Sketchup::Face).select{|e| e.normal==Z_AXIS }[1]
  #top_face.material = "black" #test to make sure I was selecting correct face
  #push/pull
  status = top_face.pushpull(100, false)
  #Find new top face again
  top_face=vaseentities.grep(Sketchup::Face).select{|e| e.normal==Z_AXIS }[1]
  #Execute Rotation
  vaseentities.transform_entities(trans, [top_face])
  #add to counter
  counter += 1
end

Now, instead of drawing a rectangle using Ruby, I want to draw my own shape manually in SketchUp, select it, then run the script to generate the vase. How do I get Ruby to read my currently selected face?


#4

Select the existing flat face.
In code: assuming you have made the group in model.active_entities

face=model.selection
entities=group.entities
entities.add_group(face)
face=entities.grep(Sketchup::Face)[0]

Now process the face as if you had made it in code…
Take care when adding to a group this way.
The newly made group and the entities [here the ‘face’] you add to it as you create it must be in the same entities context - otherwise you’ll get a BugSplat!
In this case it’s safe because the selection is made in the active_entities where the group is made…

Otherwise make a new empty group and add geometry [or instances etc] into its entities context - as in the original code.