As we all know,when we draw a face,it has its entityID(number)and typename(“Face”), but these elements are fixed,whether the face can be named like a component, similar to the method “description” .
You could add an attribute dictionary containing the name. But, so far as I know, there is no official attribute for “name” on edge or face.
This is not entirely true. Eg. In a current drawing context if you are drawing an edge or face “over” or “beside” a the face/edge will split/merge. But there will be ‘no such effect’ on group or componentinstance…
Quick example:
(copy paste to Ruby console)
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the 'face' to the entities in the model
face = entities.add_face(pts)
edge = entities.add_edges(pts[1], pts[3])
# now the 'face' is 'just a half of the original one'
selection = model.selection.add face
Sir, if I want to search for the entity through “each” function, how can I accurately find the face I want
Something like (untested - off the top of my head)
ents.grep(Sketchup::Face).find {|face| face.attribute_dictionaries("yourDictName") != nil}
where ents is the Entities collection you want to search (model.entities or some ComponentDefinition.entities) and “yourDictName” is the AttributeDictionary name you use to name a Face. This should return the named Face or nil if it fails.
Correction … you need to use the singular #attribute_dictionary
method to test for a specific dictionary. (The plural method does not take an argument.)
# At the top of your extension submodule:
DICT ||= "YourNamespace_YourExtensionName"
# Elsewhere in the code ...
# Find the first face with the extension's dictionary.
def find_face(ents)
ents.grep(Sketchup::Face).find {|f| f.attribute_dictionary(DICT) }
end
# Find all the faces with the extension's dictionary.
def find_faces(ents)
ents.grep(Sketchup::Face).select {|f| f.attribute_dictionary(DICT) }
end
I’m a visual learner… and I would like to draw your attention:
def test_pid_id_attr
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the 'face' to the entities in the model
face1 = entities.add_face(pts)
#set attribute for face1
face1.set_attribute( "testdictionary", "name", "face1" )
pid_face1 = face1.persistent_id
id_face1 = face1.entityID
p "face1: #{face1} pid: #{pid_face1} id: #{id_face1} attr: #{face1.get_attribute( 'testdictionary', 'name' )}"
# divide the original face1 to two faces
edge = entities.add_edges(pts[1], pts[3])
# Query the two new face
entities.grep(Sketchup::Face){|face|
p "face : #{face} pid: #{face.persistent_id} id: #{face.entityID} attr: #{face.get_attribute( 'testdictionary', 'name' )} "
}
return
end
test_pid_id_attr
Result:
"face1: #<Sketchup::Face:0x0000018e95e02750> pid: 207 id: 465 attr: face1"
"face : #<Sketchup::Face:0x0000018e95e02750> pid: 207 id: 465 attr: face1 "
"face : #<Sketchup::Face:0x0000018e95e02110> pid: 209 id: 477 attr: face1 "
As you can see the original face1 is same as the first one after split. Moreover both of the split faces have a same attributes and entityID
s…
Maybe Dan can explain it why?
EDIT:
Ups there was a typo…
LOL … I was going to point out that error, and suddenly “poof” … you fixed it.
Yes … so we see that the new split off face has it’s own persistent_id
and entityID
, but gets a copy of the attribute dictionary. The code might need to rename the new half-face.
Yes sorry.
But still do not get how can be identical two faces if one is half of the original.
(The attributes could be inherited that seems to be understandable…)
They are identical (mirror images of each other) because you divided a symmetrical rectangle using an edge drawn along it’s diagonal.
I don’t think…
The original “big” face1 and one of the new ones are “identical” (The first two rows)
"face1: #<Sketchup::Face:0x0000018e95e02750> pid: 207 id: 465 attr: face1"
"face : #<Sketchup::Face:0x0000018e95e02750> pid: 207 id: 465 attr: face1 "
The two new face are not identical, Just the attributes (The last two row)
"face : #<Sketchup::Face:0x0000018e95e02750> pid: 207 id: 465 attr: face1 "
"face : #<Sketchup::Face:0x0000018e95e02110> pid: 209 id: 477 attr: face1 "
???
And how do we will know which one (from the new ones) will inherit the original ? The “left” or the “right” one? Is that depends of the split “edge direction” or what?
No. Before the split, face1
is a rectangle. Afterward it is one of the triangles.
SketchUp reuses the reference and IDs.
I don’t know what the rule is. You might have to experiment. Or ask @tt_su.
Merging/splitting means the faces are rebuilt, the fact that a face reference remains is implementation detail.
How to handle that isn’t easy to dictate on a general basis. Entirely depends on the situation because there are too many permutations.
In this particular situation you could use the face’s bounds to determine “left”/“right”. That assumes this happens under controlled situations where orientation is consistent/predictable.