# Find faces component 'lays' on

**URL:** <https://forums.sketchup.com/t/find-faces-component-lays-on/29236>\
**Category:** Ruby API\
**Created:** [August 18, 2016, 10:14pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236 "2016-08-18T22:14:29Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 18, 2016, 10:14pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/1 "2016-08-18T22:14:29Z")

</div>

Is there an easy way to get an array of all the faces that a component lays on?

In the clip below the array returned should be [yellow\_face, green\_face, blue\_face]

 ![](https://global.discourse-cdn.com/sketchup/original/3X/5/b/5b2fc82945c598c6e1758651f5721344818020a7.PNG)

---

<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:** [August 20, 2016, 5:15am UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/2 "2016-08-20T05:15:25Z")

</div>

If it is a **glue\_to** component it can be glued to a single face, and it is easy to check this, via:  
[`Sketchup::ComponentInstance#glued_to()`](http://ruby.sketchup.com/Sketchup/ComponentInstance.html#glued_to-instance_method)

If not, you will need to decide what faces to test.

```ruby
test_faces = ents.grep(Sketchup::Face)

```

You’ll need to decide what vertices of the component will be “laying down” vertices, and get their 3D point objects in an array, let’s call it **`lay_pts`**.

Then iterate the faces checking if any of the lay points are in the faces.

```ruby
mating_faces = test_faces.find_all {|face|
  lay_pts.any? {|pt|
    result = face.classify_point(pt)
    result != Sketchup::Face::PointOutside &&
    result != Sketchup::Face::PointNotOnPlane &&
    result != Sketchup::Face::PointUnknown
  }
}

```

Now this would work using the corner vertices, but would likely miss the green face. So you’ll need to add more points at some grid resolution to the **`lay_pts`** array.

---

<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:** [August 20, 2016, 5:33am UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/3 "2016-08-20T05:33:39Z")

</div>

You might also try the bounding box intersection approach:  
[**`Geom::BoundingBox#intersect()`**](http://ruby.sketchup.com/Geom/BoundingBox.html#intersect-instance_method)

> ⚠ Pay head to the note about results (of this method) prior to SU2015 !

```ruby
cbb = cinst.bounds # transformed bounds
lay_bb = Geom::BoundingBox::new.add(
  cbb.corner(0), cbb.corner(1), cbb.corner(2), cbb.corner(3)
)
mating_faces = test_faces.find_all {|face|
  face.bounds.intersect(lay_bb).valid?
}

```

**Note** that I do not know what orientation the component or faces are, so you may need to adjust what the **`corner()`** argument numbers are. See:  
[**`Geom::BoundingBox#corner()`**](http://ruby.sketchup.com/Geom/BoundingBox.html#corner-instance_method)

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 20, 2016, 1:42pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/4 "2016-08-20T13:42:26Z")

</div>

I will experiment with some of these methods. They are all new to me so thanks for your help.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 20, 2016, 5:09pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/5 "2016-08-20T17:09:12Z")

</div>

That works very well. The only problem is I need a way to check the faces to see if they match the ‘hole cutting plane’ of the component. This is easy if the component is already glued to a face.

Like this:

```ruby
mating_faces = test_faces.find_all {|face|
  face.bounds.intersect(cinst.bounds).valid? && 
  (face.plane==cinst.glued_to.plane)
}

```

I a case where a component is either glued to a group or nothing how can I get:

```ruby
cint.gluing_plane

```

---

<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:** [August 24, 2016, 1:39pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/6 "2016-08-24T13:39:04Z")

</div>

> [@Neil\_Burkholder](#):
>
> In a case where a component is … glued to … nothing

Always check the result of [**`Sketchup::ComponentInstance#glued_to()`**](http://ruby.sketchup.com/Sketchup/ComponentInstance.html#glued_to-instance_method) for a **`nil`** value, before proceeding.

Ie, to break out of a iteration loop:

```ruby
break if cinst.glued_to.nil?

```

or to loop to the next collecton member:

```ruby
next if cinst.glued_to.nil?

```

or to bail from a method:

```ruby
return if cinst.glued_to.nil?

```

> [@Neil\_Burkholder](#):
>
> In a case where a component is either glued to a **group** …

If you glue the component inside the group directly to a face, then the **`glued_to()`** query will return a face object instead.

So, apparently the API is lacking a **`glued_to_face()`** query method, or perhaps an optional argument for the current method, that would drill into groups and other instances and return the glued to face.

* * *

**`Sketchup::Group`** objects do not (yet [**2016**]) have a **`get_glued_instances()`** method like faces have.

> **UPDATE** : `Sketchup::Group` class instances have [`#glued_to()`](https://ruby.sketchup.com/Sketchup/Group.html#glued_to-instance_method) and [`glued_to=()`](https://ruby.sketchup.com/Sketchup/Group.html#glued_to=-instance_method) methods since SketchUp versions **2021.1** and higher.

But perhaps you can go at it from the other direction, and use this to iterate the group’s collection of faces, finding that one which returns your instance as the one that is glued.

Where **`cinst`** is the component, and **`group`** is the object returned by **`cinst.glued_to()`**:

```ruby
faces = group.entities.grep(Sketchup::Face)
found = faces.find {|f| f.get_glued_instances.include?(cinst) }
return if found.nil?
glue_plane = found.plane

```

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 24, 2016, 2:05pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/7 "2016-08-24T14:05:22Z")

</div>

Thanks.

I already understand how to check what (if anything), a face is glued to. My point rather was that if the component is glued to a face I can get the plane of that face. Otherwise I can compare like this: `cint.zaxis == face.normal.`  
The problem is that a face might be ‘aligned’ but not on the same plane as the component.

I realize that the glued\_to face might not always be the same as the gluing plane of the component. It seems strange that SketchUp allows gluing to a face that is not on plane, thereby distorting the face.

What I was really trying to find out; “Is there a way to tell what the gluing plane of a component is, without checking the glued\_to face?”

No worries if that is not possible. In my extension I now display a warning and return, if the component is not glued to a face. I just thought it would have been nice to handle this type of situation as well.

BTW: A component can never be glued a face inside another component of group. Or can it? In a case where it appears to be so the result is that the `component.glued_to` returns the group or component containing the face, and the `face.get_glued_instances` returns .

---

<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:** [August 24, 2016, 2:45pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/8 "2016-08-24T14:45:14Z")

</div>

> [@Neil\_Burkholder](#):
>
> The problem is that a face might be ‘aligned’ but not on the same plane as the component.

Planes in the SketchUp API are represented by arrays.  
_See:_ [http://www.sketchup.com/intl/en/developer/docs/ourdoc/geom.php](http://www.sketchup.com/intl/en/developer/docs/ourdoc/geom.php)  
for some handy module methods.

The API defines comparison methods **`Geom::Point3d#==`** and **`Geom::Vector3d#==`** classes for the 2 element plane array type.

The 4 element array types are Floats representing the coefficent of the plane equation.

_See:_ [Euclidean plane - Wikipedia](https://en.wikipedia.org/wiki/Plane_(geometry)#Point-normal_form_and_general_form_of_the_equation_of_a_plane)

So it is possible to compare them. But be careful with the Floats. You may need to round them to some certain number of decimals for comparison.

> [@Neil\_Burkholder](#):
>
> What I was really trying to find out; “Is there a way to tell what the gluing plane of a component is, without checking the glued\_to face?”

Component definitions (I think) use their local XY plane as the gluing plane.

So for example, you need to draw windows lying flat, when you make them into a gluing component.

Try inserting a **Marvin** window component and hover over the various faces of a cube. (But don’t click the insertion point.) You’ll see how the “ghost” component changes orientation as you move over the sides or top.

> [@Neil\_Burkholder](#):
>
> I realize that the glued\_to face might not always be the same as the gluing plane of the component.

The definition can also be set to glue to faces in any orientation, only horizontal, only vertical, or none (if the component should not allow gluing.)

Other than that, I don’t understand what your trying to say here.

> [@Neil\_Burkholder](#):
>
> It seems strange that SketchUp allows gluing to a face that is not on plane, thereby distorting the face.

I do not know what this means. I have not seen it. Do you have a screen shot of this, or a sample model showing this has occurred ?

---

<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:** [August 24, 2016, 2:49pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/9 "2016-08-24T14:49:14Z")

</div>

> [@Neil\_Burkholder](#):
>
> BTW: A component can never be glued _to_ a face inside another component of group. Or can it?

Yes it can, **IF the object has been opened for edit** , which really means that it is the object’s **definition** that is been edited. So if you insert a gluing component into an object opened for edit, then ALL instances of that object will show the change.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 24, 2016, 6:31pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/10 "2016-08-24T18:31:23Z")

</div>

Thanks for those links. They were very informative. I’ll admit transformations, vectors, and planes, are still confusing to me. If I’d do the research I should, I wouldn’t need to ask for help from nice people like you. 😉

> [@DanRathbun](#):
>
> I do not know what this means. I have not seen it. Do you have a screen shot of this, or a sample model showing this has occurred ?

Slightly annoying if this is not the intended result.

 ![](https://global.discourse-cdn.com/sketchup/original/3X/d/1/d1c66588c5c82a418ae4dfbab25d46ddf384a551.gif)

> [@DanRathbun](#):
>
> Yes it can, IF the object has been opened for edit, which really means that it is the object’s definition that is been edited. So if you insert a gluing component into an object opened for edit, then ALL instances of that object will show the change.

Should probably have been:

> [@Neil\_Burkholder](#):
>
> A component can never be glued to a face inside another component of group, unless it is nested inside it. Or can it?

> [@DanRathbun](#):
>
> But perhaps you can go at it from the other direction, and use this to iterate the group’s collection of faces, finding that one which returns your instance as the one that is glued.
> 
> Where cinst is the component, and group is the object returned by cinst.glued\_to():
> 
> faces = group.entities.grep(Sketchup::Face)  
> found = faces.find {|f| f.get\_glued\_instances.include?(cinst) }  
> return if found.nil?  
> glue\_plane = found.plane

This didn’t seem to work, because if a component is glued to a group, it means it is not nested inside it. If this is the case, none of the faces inside the group will return the component as being glued to them.

Thanks for going out of your way to educate me. 🙂

---

<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:** [August 24, 2016, 7:40pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/11 "2016-08-24T19:40:19Z")

</div>

> [@Neil\_Burkholder](#):
>
> This didn’t seem to work, because if a component is glued to a group, it means it is not nested inside it. If this is the case, none of the faces inside the group will return the component as being glued to them.

OK, I did say “perhaps”. So there is a missing method for the API.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 24, 2016, 10:03pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/12 "2016-08-24T22:03:22Z")

</div>

Just happened to think of it, I actually had added a function for that. I’m sure the same thing could be done for a group.

```ruby
class Sketchup::ComponentInstance
  def get_glued_instances
    return self.parent.entities.grep(Sketchup::ComponentInstance).find_all {|c| c.glued_to == self}
  end
end

```

I’m not sure if adding functionality to SketchUp would be condoned in a public extension.

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [August 25, 2016, 1:02am UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/13 "2016-08-25T01:02:05Z")

</div>

You should never “monkey patch” a class provided in the API, any of the standard Ruby libraries, or any built-in Ruby class. Doing so affects all Ruby code regardless of source and can cause other people’s extensions to fail in strange ways! If you want a custom variation on one of these classes, you should use a refinement instead. @DanRathbun had another recent post about refinements.

> [@Ruby, Excel, Dynamic Components, and JPEG output](http://forums.sketchup.com/t/ruby-excel-dynamic-components-and-jpeg-output/29410/6):
>
> (Moved to the Ruby API category.) @anthonysmithemail, regarding your monkey patch example: class String def numeric? Float(self) != nil rescue false end end … is an absolute NO NO. DO NOT modify Ruby nor API base classes ! SketchUp Ruby is a shared environment. Instead use refinements: [http://ruby-doc.org/core-2.0.0/doc/syntax/refinements\_rdoc.html](http://ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html) ie: module Smith::Refinements refine String do def numeric? Float(self) != nil rescue false end end end using S…

---

<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:** [August 25, 2016, 5:18pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/14 "2016-08-25T17:18:32Z")

</div>

> [@Neil\_Burkholder](#):
>
> I’m not sure if adding functionality to SketchUp would be condoned in a public extension.

It would not, and will not pass inspection for the Extension Warehouse.

For ref: [Ruby 2.0.0 doc/syntax/refinements.rdoc](http://ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html)

```ruby

module Author::RefinedComponents

  refine ::Sketchup::ComponentInstance do

    # Returns an array of the externally glued instances if any. (Empty array if none.)
    # @return [Array(Sketchup::ComponentInstance)] the glued instances if any.
    def get_glued_instances()
      parent.entities.grep(Sketchup::ComponentInstance).find_all {|comp|
        (comp.glued_to == self) rescue false
      }
    end

  end # class refinement

end # refinement module

using Author::RefinedComponents

module Author::SomePlugin

  # Use your refinements here.
  
end # author's plugin module

```

* * *

Just **FYI** , the refinement module definition may be kept in a separate library file, and loaded as needed with **`require()`**.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 25, 2016, 5:34pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/15 "2016-08-25T17:34:25Z")

</div>

> [@DanRathbun](#):
>
> It would not, and will not pass inspection for the Extension Warehouse.

Ok thanks. I was suspicious of that.

I also could go at it like this:

```ruby
def get_glued_instances(cinst)
  return cinst.parent.entities.grep(Sketchup::ComponentInstance).find_all {|c| c.glued_to == cinst}
end

gi = get_glued_instances(cinst)

```

---

<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:** [August 25, 2016, 5:42pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/16 "2016-08-25T17:42:09Z")

</div>

> [@Neil\_Burkholder](#):
>
> I also could go at it like …

Yes, as in a method local to your class or plugin module. This is what many do “in a pinch” just to get the code working.

And it was the preferred pattern before refinements came about in Ruby v2+.

Just an FYI, I have been working on a refinement library. It is in alpha stages at present.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [August 25, 2016, 6:07pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/17 "2016-08-25T18:07:53Z")

</div>

Features to make development easier. Nice.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 5, 2016, 12:38pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/18 "2016-09-05T12:38:05Z")

</div>

@DanRathbun

Thanks for your help. I got my extension in the warehouse Friday.

[Face Cutter Extension](http://extensions.sketchup.com/en/node/3636)

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 5, 2016, 3:58pm UTC](https://forums.sketchup.com/t/find-faces-component-lays-on/29236/19 "2016-09-05T15:58:41Z")

</div>

> [@DanRathbun](#):
>
> I do not know what this means. I have not seen it. Do you have a screen shot of this, or a sample model showing this has occurred ?

I started a new [topic](http://forums.sketchup.com/t/hole-cutting-components-gluing-behavior-odd/29901) for this issue.
