Distinct between entities in instances
-
Hi,
I've came to a problem, I don't have idea how to solve.
Is it possible to treat entities inside instances separately or each change in one instance necessary leads to changes in all other instances?In a plugin I'm working on, I should make distinction between surfaces which are part of various component's instances. My goal is to attach unique id value to each face (as an attribute), but whenever I try that, corresponding faces in all instances are updated to.
Example code
User selects some face in one instance (it is also selected in other instances).
Run next code in Ruby console:[pre:1gler01f]
index=0 selection = Sketchup.active_model.selection face = selection[0] #selected face parent = face.parent if parent.class == Sketchup::ComponentDefinition and parent.count_instances>1 #go through all instances and find selected faces parent.instances.each{ |inst| inst.definition.entities.each{ |ent| if selection.contains?(ent) #attach id to face ent.set_attribute("faceData", "id",index) puts index index = index + 1 end } } end
[/pre:1gler01f]when I have 3 instances of the component output is :
0 1 2
Based on this it seems like in each instance, selected face should have unique attribute value, but it is not the case in fact.After this code when I select face in any instance and type in console
Sketchup.active_model.selection[0].get_attribute("faceData", "id")
Result is always 2 - which means that each time I set attribute it was in fact set in all instances, and only the last value from look is the one saved.Note, I don't want to make instances unique, because it would effect on model geometry (and plugin users don't want that).
Thanks in advance,
Marija -
ComponentInstances don't own separate copies of the primitive geometry, all the geometry is owned by the ComponentDefinition. So, if you alter anything about a primitive (Edge, Face,...) in the ComponentDefinition it immediately affects all of the ComponentInstances. You can only set per-instance values for things that are not shared by all the instances, such as transformation, name,...
-
Thanks,
I came to the same conclusion after various forum's posts reading and my experiments with code. Just wanted to be sure if I understood it well.
-
To recap.
Component definitions can have attributes.
These apply to the definition and are therefore accessible viadefinition.get_attribute(...)
Component definitions can have entities,
These entities can have attributes.
These apply to the entities with the definition, and are therefore accessible viasome_entity.get_attribute(...)
Component instances can have attributes.
These apply to the instance and are therefore accessible viainstance.get_attribute(...)
If you have an attribute set for any entity that is within a
definition.entities
context, then that attribute is accessible allinstance.definition.entities
- i.e. it is the same attribute, and therefore if you '.set_...
' it, it affects every instance sharing that definition - just as if you had painted a face 'red' inside the definition you should expect every instance to show that subsequently.
Advertisement