Clone rotate / Last object of an array


I’ve a couple of question.
Is it possible to do rotate clone via API (like ctrl+rotate in the UI) ?
An example please.
Let’s say i would like to generate via code an hexagon centered in the origin, rotating on its center,
+15 degree creating 3 clone.

Other question always via Ruby:
Is there a way to work only with the last object / component created. For example if a do a clone rotate like in the UI, would like to use the new / last rotated clone created by the transform array (maybe into a loop).

Thanks a lot

Yes if you define a reference to the object(s) that you create. Ruby is a 100% object oriented language.
Everything in Ruby is an object or a reference to an object. Any reference name can be made to “point at” any object, of any class, … and reused to point at any other object (of any class) at any time. (Also known as a weakly typed language. In Ruby you do not define a reference as a certain type/class.)

SketchUp does not create a global reference to the last object created, for use by Ruby code.
But sometimes you can get the last object in an entities collection, via a negative index:


In SketchUp you must use a group or component to prevent geometry from merging immediately when created. (You can always explode groups later to merge geometry.)

ents = Sketchup.active_model.entities
cpt = ents.add_cpoint( ORIGIN )
grp = ents.add_group( cpt )
# puts the cpoint inside the new group, to prevent 
# group from getting garbage collected
grp.entities.add_ngon( ORIGIN, Z_AXIS, 10.0, 6 )
for i in 1..2
  rot = Geom::Transformation::rotation( ORIGIN, Z_AXIS, 15.0*i )
  ents.add_instance( grp.definition, rot )

Then later to get an array of the group instances:

grps = ents.grep(Sketchup::Group)

… and to get the last member of the group:

tail_end_charlie = grps[-1]

… or:

tail_end_charlie = grps.last

Thanks a lot

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.