How to check if an entity is connected to a face?

Hi, i need to find if an entity is connected to a face.
I saw that method - connected_entities = face.all_connected and it returns all entities connected to a face, but what I need is to check if just at least one entity is connected to a face. Something like that I want to do:

collection_of_entities = [] 
collection_of_entities.each { |collection|
  collection.each { |entity|
  if face.is_connected(entity)
       # --- I delete whole collection of entities ----
                    ----
  end
}

You code is jumbled…
You set up collection_of_entities to be an empty array []
Then you immediately iterate it - which will do nothing since it’s empty
The face reference is not set in this code - perhaps we have a partial example ?

if face.all_connected.include?(entity)
  ###

Using this might work, but all_connected only includes edges and faces, and from your earlier questions I suspect that you are dealing with other things ?
If they are groups, for example you can check that the group’s insertion ‘point’ [group.transformation.origin], or its bounds.min, or its bounds.max or bounds.center both with their z reset to be bounds.min.z
And check the point falling on the face.
You use this method… https://ruby.sketchup.com/Sketchup/Face.html#classify_point-instance_method
result = face.classify_point(point)
The result varies depending on the points location relative to the face.
There are 5 possibilities…
e.g. point is inside the face or outside the face or on an edge or vertex, or not on the face.plane at all !
From that you can deduce it the group has some part falling on the face.
Of course with more convoluted group geometry you could have unexpected results, then you need to check the group’s vertices positions, transformed as needed, and see if any of those land on the face.plane ??

1 Like

Some note and question

This method even the face is stand alone (nothing else connected to it) will result the face itself and the bounding edges array.

Are you interested only for the directly connected entities only? Or all the entities (faces, edges) like triple click on face?
As you see e.g. adjacent face sharing one edge can be considered as not directly connected since there is an edge in between.
So would you please explain a little more specifically, maybe screenshot of simple model highlighted / marked…?

Sorry, I just intended to show that there is an array, array in real case is not empty.
I have some kind of roof tiles, which are groups of pushpulled face.
And I need to delete some roof tiles which are situated on some face, but I did not succeed with that.
But I noticed, when I explode all groups of tiles, when I triple click on external face, I have in selection all tiles which I want to save. Here is a screen:


And if I use external_face.all_connected after explode process, I get elements which I don’t need to erase. So, because I don’t know how to see if an tiles entity is connected to a face, I iterate through all items to see want I need to delete.
I wrote this code, and it worked, but I surely understand, it’s stupid as hell:) but works.

exploded_tiles_array = []
        new_group_array.each {|tile|
            exploded_tiles_array.push(tile.explode)
        }
        
        valids = max_face.all_connected
        
        exploded_tiles_array.each {|tile|
          entities = tile.grep(Sketchup::Entity)
          need_to_delete = true
          entities.each {|entity|
            if entity != nil && entity.valid?
              for i in 0..valids.length()
                if entity == valids[i]
                    need_to_delete = false
                    break
                end
              end
            end}
            if need_to_delete
              tile.each {|e|
                if !(e.is_a? Sketchup::Vertex)  && !(e.is_a? Sketchup::Loop) && !(e.is_a? Sketchup::EdgeUse) && (e.valid?)
                  e.erase!
                end} 
            end

         }


And I get this after that code:

I also tried to play with classify_point on bottom faces of tiles. but I don’t know why I didn’t get results I expected, just some faces showed that are on vertex. Maybe I should play more with that.

I hope my previous reply could show what I expect.
face.all_connected give me entities which I don’t need to delete, so I was looking for some method to say me if an entity is connected to a face, not to iterate in all_connected to see what not to delete.

If you are working with raw geometry [exploded] which you then erase I suggest that you can grep the result more cleanly.

if need_to_delete
  tile.each{|e|
    e.erase! if e.valid? && e.is_a?(Sketchup::Edge)
}

The faces can’t exist without their edges…

Also please indent your code snippets by just 2 spaces each time…

1 Like

Another way would be to collect everything in an array [best if inside a group.entities etc] - let’s call it: all
Then get the face and its face.all_connected as an array, let’s call it: to_keep
to_go = all - to_keep
Now erase the to_go collection…
No need to iterate it, use:
entities.erase_entities(to_go)
Where entities is where the objects are - e.g. the group.entities

1 Like