Get/Set entity tag programmatically

I need to group a bunch of things together, then move them, then explode the group. The problem is that the new entities that result from the explode do not have the tags that I assigned to the originals.

How do I get/set the tags so that, after the move, I can tag the resulting entities properly?
Or is there a way that already exists to save the tags when I group/explode the entities.

Add an attribute to each entity which contains its layer.name [tag],
then explode the container group, using exploded_entities=group.explode
Iterate through exploded_entities, get the attribute from each entity and reapply the layer [by name] to that entity

To set the attribute use something like, on each entity in turn:
entity.set_attribute("Johno3535", "entity_tag", entity.layer.name)

Later on, after the explode… iterate the exploded_entities:
entity.layer=Sketchup.active_model.layers[entity.get_attribute("Johno3535", "entity_tag", "Layer0")]

Can I ask why you group then explode?

If you can provide some context to what you are doing (high level) I wonder if there’s another way to do this without using temporary groups.

Of course.

I have a wall with a bunch of windows and doors. Each are a separate componentinstance with metadata assigned. They need to be treated as separate entities.

I have a resize/move dialog for the wall. It will remove any windows/doors/IFCOPENING metadata that are no longer valid if you resize the wall too small. And of course if the wall moves, all of the windows/doors within the wall need to move. I found it easiest to move them as a group instead of figuring out the transformations that would need to be done for each window and door.

I’d recommend you avoid temporary groups and explodes. Explode is an expensive non-trivial operation. As you experience, not all data is preserved. It’s a destructive operation.

Given that you group then and move the group, it sounds like you should be able to just apply that transformation to all the instances separately. I don’t see why grouping should make a difference.

Maybe you can provide an example model with some screenshot of what you would like to see happen?

I am not at my work computer at the moment but essentially, I have a wall
10000Lx100Wx6000H at 0x,0y,0z with no rotation.
I have windows in the wall…
1000Lx30Wx2000H at (1000x,50y,1500z), (3000x,50y,1500z), (5000x,50y,1500z) and (7000x,50y,1500z).

I want to move the wall to 1000x, 4000y, 1000z with zRotation of 45 degrees.
I use new transfromation ([1000,4000,1000]) * transformation.rotation 45 degrees.

I group the windows with the wall when I do the transformation because I wouldnt know what transformation to use for the windows so that they still sit in the same place relative to the wall.

If I use the same transformation, wouldn’t all the windows (a) be on top of each other and (b) not align with the holes in the wall.

I am not sure how clear I am making things. If this info doesnt help I will post pictures when I get back to my computer. I cannot use the model because of the associated proprietary metadata.

You can use something like …

vec   = Geom::Vecter3d::new([1000,4000,1000])
pivot = ORIGIN
move  = Geom::Transformation::translation(vec) 
rot   = Geom::Transformation::rotation(pivot, Z_AXIS, 45.degrees)

A translational transform moves the object(s) along a vector from their current position.
From math, please remember that a vector has a direction and length (magnitude) but not a position.

Use one of the batch form method(s) from the Entities class:

Where ents is the parent collection …

objects = [wall, window1, window2, window3, window4]

ents.transform_entities(rot, objects)
ents.transform_entities(move, objects)

So …

I don’t think so. You can try it …

tr = rot * move
ents.transform_entities(tr, objects)

If you do the move first, then the rotational pivot point would no longer be the model’s ORIGIN, rather …

pivot = ORIGIN.transform(move)

(Just make sure you never change the global reference ORIGIN point as other extensions also use it. In the above example a new pivot point object is created translated from the existing ORIGIN point.)

1 Like

I think you have me up until

Where ents is the parent collection …

I am not sure what you mean by parent collection. “objects” would be my collection. And I want to move the wall and all of its windows and doors.

NEVER MIND! Thank you. That works. I may have a lot of code updating to do. But this is much better than mine.

No the parent Entities collection is that in which reside the members of the objects array. (Ie, the Ruby array contains references into the parent Entities collection.)

P.S. - (Here in the forum) I usually only use the word “collection” to mean the Ruby API class objects that wrap the internal C++ collections. (But yes, generally, Ruby arrays, sets, structs and hashes could be thought of as collection-type classes.)

I almost was going to type something :grin: