@bobvandevoort said:
Ah I'll try to be a bit more specific.
Let's say I have a model with 3 components, comp1, comp2 and comp3.
comp1 has comp2 inside and comp3 is inside of comp2.
This then means (if I am correct) that comp1 has a transformation, called trans1, comp2 has one called trans2 and comp3 has one called trans3.
If I take comp3 and place it in the model with its own transformation, trans3, it will not be places at its original position in the model. To achieve that it needs to have its transformation multiplied by trans2 and trans1.
So now I want to create a matrix "noembed" that only has components that have no other components nested inside them (like comp3 here). However these components should still retain there original location/position in the model meaning their own transformation should be multiplied by all their parent's transformations.
In which case this code seem to do the trick
cd.instances.each{|ci|
> next unless ci.parent==mod
> trans_matrix << ci.transformation
> noembed << ci
> }
You are correct that the transformation of comp3 not embedded will be the product of its' and the components instances transformations above it. The code above only would only save comp3's transformtion if it is at the model level.
It would be great if you could start at the instance and work up to the top but .parent returns a component definition which doesn't have a transformation associated with it. So you have to start at the top and drill down.
This code does that for two levels. Adds a comp3 at model level and erases the instance that it replaces.
mod = Sketchup.active_model
ent = mod.active_entities
sel = mod.selection
rmv = []
ci0 = ent.grep(Sketchup;;ComponentInstance);#parent is mod
ci0.each{|c0|
next if c0.definition.name=="comp3"
tr0 = c0.transformation
ci1 = c0.definition.entities.grep(Sketchup;;ComponentInstance);#parent is definition
ci1.each{|c1|
tr1 = c1.transformation
if c1.definition.name=="comp3"
tr = tr0*tr1
ent.add_instance(c1.definition,tr)
rmv << c1; break
else
ci2 = c1.definition.entities.grep(Sketchup;;ComponentInstance);#parent is definition
ci2.each{|c2|
tr2 =c2.transformation
if c2.definition.name=="comp3"
tr = tr0*tr1*tr2
ent.add_instance(c2.definition,tr)
rmv << c2; break
end
}
end
}
}
rmv.each{|ci| ci.erase! if ci.valid?}