# Finding all connected groups

**URL:** <https://forums.sketchup.com/t/finding-all-connected-groups/137672>\
**Category:** Ruby API\
**Created:** [October 1, 2020, 7:41am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672 "2020-10-01T07:41:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 7:41am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/1 "2020-10-01T07:41:26Z")

</div>

Dear Friends,  
I need to find all connected groups to my\_group (if any) in ruby sketchup. Can you help me for it?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 8:14am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/2 "2020-10-01T08:14:47Z")

</div>

> [@majid866](#):
>
> find all connected groups

What does it mean?  
There are many topic about it…  
…and a result is : There is no “simple” method for this.

> [@How to know that two components are touching each other in sketchup using ruby?](https://forums.sketchup.com/t/how-to-know-that-two-components-are-touching-each-other-in-sketchup-using-ruby/135979):
>
> if any two component are touching each other then which function i can use to check it??

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 8:29am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/3 "2020-10-01T08:29:13Z")

</div>

My goal is make two (or more) connected groups as one group.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 8:43am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/4 "2020-10-01T08:43:36Z")

</div>

There are dozens of possibilities for groups to be connected.  
Can you tell us your interpretation of “connected groups”? Please explain in sufficient detail!

I guess there will be no solution for your request as “two-line” code…

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 8:51am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/5 "2020-10-01T08:51:25Z")

</div>

In picture you can see some connected groups that I wish to find.

 ![Screenshot 2020-10-01 121955](https://global.discourse-cdn.com/sketchup/original/3X/e/1/e1364629fc8fcdb3eda31d6e877a537e34c10e3e.jpeg). I wish to solve this problem in different steps. In first step I need to know name of other group that is connected to my group.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 11:41am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/6 "2020-10-01T11:41:44Z")

</div>

An idea.  
You have to get an array of all vertices position of all the faces of “my group”. Transformation of group must be taken into consideration when you retrieving the position. You will get Point3d array. You may need to make it uniq!.

You have to get an array of all group in a model excluding “my group”.  
You have to iterate through each of this group entities and check the faces for all above mentioned points with .classify\_point method if it is on it or not. (This time you heve to use both PointInside , PointOnEdge, PointOnVertex for.classify\_point condition). Transformation of group must be taken into consideration when you make the compassion.  
If you get ‘hit’ you can store the group to an array for later processing…check the names, etc.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 12:37pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/7 "2020-10-01T12:37:11Z")

</div>

Lets have a simple 2D example. Following we have face1 (one point in common), face2 (One line in common), face3 (an area in common), face4 (Nothing in common) and face\_main. If we have only face\_main, how can we find face1, face2 and face3?

```ruby
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection

pt1 = [20, 20, 0]
pt2 = [15, 30, 0]
pt3 = [25, 30, 0]
face1 = ent.add_face pt1, pt2, pt3
pt1 = [13, 10, 0]
pt2 = [17, 10, 0]
pt3 = [15, 5, 0]
face2 = ent.add_face pt1, pt2, pt3
pt1 = [15, 15, 0]
pt2 = [30, 10, 0]
pt3 = [30, 20, 0]
face3 = ent.add_face pt1, pt2, pt3
pt1 = [0, 0, 0]
pt2 = [10, 0, 0]
pt3 = [0, 10, 0]
face4 = ent.add_face pt1, pt2, pt3
pt1 = [10, 10, 0]
pt2 = [10, 20, 0]
pt3 = [20, 20, 0]
pt4 = [20, 10, 0]
face_main = ent.add_face pt1, pt2, pt3, pt4

```

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 2:52pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/8 "2020-10-01T14:52:09Z")

</div>

!! dirty & cheap !!

```ruby
#the snippet of your previous post continued like:

all_faces = ent.grep(Sketchup::Face).to_a
other_faces = all_faces - [face_main]
p_v = Sketchup::Face::PointOnVertex 
p_e = Sketchup::Face::PointOnEdge 
p_i = Sketchup::Face::PointInside
faces_con = []
all_faces.each{|face_a|
  face_a.vertices.each{|vert|
    if face_a == face_main
      other_faces.each{|face_o|
        result = face_o.classify_point(vert.position)
        if result == p_v || result == p_e || result == p_i 
          faces_con<<face_o
        end
      }
    else
      result = face_main.classify_point(vert.position)
      if result == p_v || result == p_e || result == p_i 
        faces_con<<face_a
      end
    end
  }
}
puts "------"
puts "found total #{faces_con.size} connected face(s)..."
faces_con.uniq!
puts "...and : #{faces_con.size} of them are unique"
puts "The face(s) connected to face_main (#{face_main}) are:#{faces_con} "
sel.clear
puts "The number of face(s) in selection:"
puts "------"
sel.add(faces_con)

```

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 3:56pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/9 "2020-10-01T15:56:08Z")

</div>

I wish to merge face2 and face3 to face\_main if faces are in same height. Can you also help me for it?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 4:21pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/10 "2020-10-01T16:21:05Z")

</div>

There are dozens of possibilities for “merge” and “same height”.  
Please explain in sufficient detail!

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 1, 2020, 4:42pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/11 "2020-10-01T16:42:26Z")

</div>

Same as picture ![Screenshot 2020-10-01 201144](https://global.discourse-cdn.com/sketchup/original/3X/2/9/293451f90186893d1b67f1eef8e2c9b2c82b2531.jpeg)

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 1, 2020, 5:42pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/12 "2020-10-01T17:42:29Z")

</div>

!! MORE dirty & cheap !!

```ruby
#the snippet of my previous snippet continued like:

faces_con.each{|f|
  gr_temp = ent.add_group
  gr_temp.entities.add_face f.vertices
  gr_temp.explode
}
2.times{
  ent.grep(Sketchup::Edge).to_a.each{|e|
    next unless e.valid?
    if e.start.edges.size == 1 || e.end.edges.size == 1 
      e.erase!
      next
    end
    next if e.faces.size != 2
    e.erase!
  }
}

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 1, 2020, 11:26pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/13 "2020-10-01T23:26:46Z")

</div>

If you take at look at the top right example, you’ll see that it has no vertices lying on or in the other group.  
Also, none of the bounding box corners would be within the other group’s bounds.

For this scenario we have suggested in the past that a boolean intersect be done on two copies of the groups. And then the result examined to see if it has an length or volume. (Lastly the temporary result is deleted.)

The drawback to this approach is that it makes modifications to the model, so some have used this approach within an undo operation and then call `Sketchup.undo` or abort the operation after doing the determination of intersect.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 2, 2020, 12:46pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/14 "2020-10-02T12:46:59Z")

</div>

Thank you so much. I will back to this codes many times…

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 2, 2020, 12:49pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/15 "2020-10-02T12:49:28Z")

</div>

Thank you for advice. I will check it carefully.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 3, 2020, 7:30am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/16 "2020-10-03T07:30:41Z")

</div>

Dear Dezmo,  
Lets make it more interesting… In following example we have faces in different groups. Can you help me to solve problem for this scenario?

```ruby
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection

pt1 = [20, 20, 0]
pt2 = [15, 30, 0]
pt3 = [25, 30, 0]
grp1 = Sketchup.active_model.active_entities.add_group
face1 = grp1.entities.add_face pt1, pt2, pt3
pt1 = [13, 10, 0]
pt2 = [17, 10, 0]
pt3 = [15, 5, 0]
grp2 = Sketchup.active_model.active_entities.add_group
face2 = grp2.entities.add_face pt1, pt2, pt3
pt1 = [15, 15, 0]
pt2 = [30, 10, 0]
pt3 = [30, 20, 0]
grp3 = Sketchup.active_model.active_entities.add_group
face3 = grp3.entities.add_face pt1, pt2, pt3
pt1 = [0, 0, 0]
pt2 = [10, 0, 0]
pt3 = [0, 10, 0]
grp4 = Sketchup.active_model.active_entities.add_group
face4 = grp4.entities.add_face pt1, pt2, pt3
pt1 = [0, 0, 0]
pt2 = [0, 10, 0]
pt3 = [10, 10, 0]
pt4 = [10, 0, 0]
grp_main = Sketchup.active_model.active_entities.add_group
face_main = grp_main.entities.add_face pt1, pt2, pt3, pt4
tr = Geom::Transformation.new [10, 10, 0]
grp_main.transform! tr
ents = Sketchup.active_model.active_entities
all_faces = []
ents.each{ |entity|
  case entity
  when Sketchup::ComponentInstance, Sketchup::Group
  entg = entity.definition.entities
  entg.each{ |entityg|
    case entityg
      when Sketchup::Face
      if entityg.normal == [0, 0, -1] 
        all_faces << entityg 
      end  
    end
  }  
  end  
}      
other_faces = all_faces - [face_main]
p_v = Sketchup::Face::PointOnVertex 
p_e = Sketchup::Face::PointOnEdge 
p_i = Sketchup::Face::PointInside

```

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [October 3, 2020, 4:35pm UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/17 "2020-10-03T16:35:08Z")

</div>

I asked twice in this topic: “Please explain in sufficient detail!”  
I will not ask a third time.

> [@majid866](#):
>
> Can you help me to solve problem for this scenario?

From the randomly copy-pasted code snippest above, I came to the conclusion that the help so far has had no effect.  
Instead of sitting down and looking at the basics, you’re waiting for someone else to write it. It won’t be me.  
Sorry.

[Ruby learning resources](https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861/3)

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 4, 2020, 2:58am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/18 "2020-10-04T02:58:00Z")

</div>

Apologize my mistake. In fact after I put faces in groups you codes work but I could not see. After I deleted groups I find one merged face.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 4, 2020, 6:52am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/19 "2020-10-04T06:52:12Z")

</div>

I added following codes and deleted connected faces and their parent group. My problem solved.

```ruby
num = faces_con.size.to_i
loop do  
  face_b = faces_con.pop
  grp_f = face_b.parent.instances[0]
  grp_f.erase!
  num -=1
  break if num == 0
end
main_grp.erase!

```

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [October 5, 2020, 2:14am UTC](https://forums.sketchup.com/t/finding-all-connected-groups/137672/20 "2020-10-05T02:14:53Z")

</div>

For friends whom are interest on this topic… You need to use following code.  
" entities.intersect\_with recurse, transformation1, entities1, transformation2, hidden, entities2 ".  
It looks confusing but you can find clear explanation and simple example in this link. [About 'intersect\_with' method • sketchUcation • 1](https://sketchucation.com/forums/viewtopic.php?f=180&t=14525). This method will return you an array. If array.size is one means 2 groups has one line in common. if two means one face in common. 3 or more means volume in common.

[Next page](https://forums.sketchup.com/t/finding-all-connected-groups/137672.md?page=2)
