Union multiple instances

Hi all :slight_smile:
I want to union multiple instances(preferably using loops, like “each”) but not sure how it can be done
I tried to put together some codes like this
image

but it doesn’t work

I even tried using some naive methods like
image

but obviously it’s not how it works :frowning:

Is there a way for it be done?

thank you!

The native boolean operations are not very useful at all or developers. They create a completely new group (always a group) instead of editing the actual instances passed to them. Also they skrew up material inheritance by removing materials from the resulting group itself and instead applying the material to each individual face.

This is why I made my own solid operations, which I later open sourced. They are not as stable as the native ones regarding the actual geometrical operations, but they don’t mess up the model either.

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 …

collection[i]

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! }

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