Union multiple instances

On the forum, please use color lexed code blocks instead of images.

A couple of notes:

* You created each definition with the same name.
(SketchUp may add a "#2" and "#3" to the second and third definition name.)

* Geom::Transformation::new() with no arguments creates an identity transformation.
You do this 3 times. Once is enough. However the API already defines a global identity transform with the IDENTITY reference that you can use for cases like this. (Use it but don’t change it.)

* Ruby does not need semicolons at the end of lines. (This is just “noise”.)

* Arrays can be defined literally …

points = Array.new
points[0] = [11, 0, 0]
points[1] = [12, 0, 0]
points[2] = [12, 1, 0]
points[3] = [11, 1, 0]

… can also be done like …

points = [
  [11, 0, 0],
  [12, 0, 0],
  [12, 1, 0],
  [11, 1, 0]

… or …

points = [ [11, 0, 0], [12, 0, 0],  [12, 1, 0], [11, 1, 0] ]

In this code …

ents = Sketchup.active_model.active_entities
ents.each {|i| 

… the reference i is not an index, it is an object reference, so you cannot add 1 to it.

If you need to access an index use each_with_index

collection.each_with_index {|obj,i|
 # code 

But keep in mind within the block, the reference i is just pointing at an integer. So you’d need to use that with a [] method call …


Also you cannot change the enumerable collection at the same time you are iterating it, or the iterator will lose track of where it is, and items in the collection will be skipped or processed more than once.

In your attempt above, you call a destructive union method with the next member of the collection in the step before it becomes the iterator. In the next loop that object will not exist because union destroys the arguments and creates new group objects (as Christina mentions.)

Whenever we need to change or erase items from an Enumerable collection during an iteration, we make an array copy of it, (using the .to_a method,) and iterate the copy.

entities.to_a.each {|ent| ent.erase! }