# Intersect Faces with Model via Ruby API

**URL:** <https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799>\
**Category:** Ruby API\
**Created:** [March 10, 2016, 6:46pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799 "2016-03-10T18:46:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 6:46pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/1 "2016-03-10T18:46:14Z")

</div>

Hello all, I’ve written a script that puts a line grid on a given face. The issue I am having is that even though the lines/edges are on the face, not all of them are being combined with the face.

 ![](https://global.discourse-cdn.com/sketchup/original/2X/6/65007b7bcca00829c1cedfb4656191c156c4b5c1.PNG)  
If I select everything(grid lines and face), right click\>Intersect Faces\>With Model, I get the desired result of all the lines that are on the face, becoming part of the face.

I haven’t however had any luck duplicating this result programmatically. I’ve tried grouping the lines and exploding them, which got me close but had some very strange effects, such as erasing sections of the existing face.

Please Help! Thanks in advance!

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [March 10, 2016, 6:56pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/2 "2016-03-10T18:56:24Z")

</div>

Assuming

`model = Sketchup.active_model`

And

`ents = model.active_entities`

And

`ss = model.selection`

And

`tr = Geom::Transformation.new()`

Use something like:

```
ents.intersect_with( false, tr, ents, tr, true, ents.to_a )

```

[http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#intersect\_with](http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#intersect_with)

It is NOT a simple method !  
There are many permutations of the arguments - entities, transformations and so on.  
BUT what you want is probably the simplest…

---

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 7:02pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/3 "2016-03-10T19:02:36Z")

</div>

Thanks TIG, this seems like exactly what I was looking for, but when I run this code, this happens…

 ![](https://global.discourse-cdn.com/sketchup/original/2X/f/f078bc868cbe57edc5fea8e2e15259649e715ab8.PNG)  
This is what was happening when I grouped and exploded the lines as well.  
Any thoughts as to why this would happen?

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [March 10, 2016, 7:25pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/4 "2016-03-10T19:25:36Z")

</div>

have you tried **`edges.find_faces`** …

john

---

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 7:34pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/5 "2016-03-10T19:34:50Z")

</div>

> [@john\_drivenupthewall](#):
>
> have you tried edges.find\_faces…

John, I did try that, it had a similar effect, though not as severe. Also, after I solve this problem, I was intending to remove all the edges that are not part of a face. So that would be an issue.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [March 10, 2016, 7:40pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/6 "2016-03-10T19:40:15Z")

</div>

Now you say the native tools do it as well !  
How big is the grid you are intersecting ?  
Very small dimensions can be flaky…

You could perhaps scale everything by x10, then intersect, then scale by x1/10 ?  
Does this help with the native route ??

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [March 10, 2016, 7:42pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/7 "2016-03-10T19:42:47Z")

</div>

what if you delete the off face ones first…  
something like…

```ruby
edges.each do |e|
next if e.faces.length == 2
e.erase! if e.faces.length == 0
next if e.deleted?
e.find_faces
end

```

---

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 7:55pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/8 "2016-03-10T19:55:53Z")

</div>

> [@TIG](#):
>
> Now you say the native tools do it as well !

When I did this from the UI as originally described, the result was perfect. When I group and explode(programmatically or through the UI), the results are ‘flaky’.[quote=“TIG, post:6, topic:21799”]  
You could perhaps scale everything by x10, then intersect, then scale by x1/10 ?  
[/quote]  
I just tried, the grid being 150 units, unfortunately this didn’t fix it.

---

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 8:00pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/9 "2016-03-10T20:00:55Z")

</div>

> [@john\_drivenupthewall](#):
>
> what if you delete the off face ones first…something like…
> 
> edges.each do |e|  
> next if e.faces.length == 2  
> e.erase! if e.faces.length == 0  
> next if e.deleted?  
> e.find\_faces  
> end

This also gave me some wacky results

 ![](https://global.discourse-cdn.com/sketchup/original/2X/7/7cb418c7904443b44f5da54e08cd4665473d39c4.PNG)

---

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [March 10, 2016, 8:37pm UTC](https://forums.sketchup.com/t/intersect-faces-with-model-via-ruby-api/21799/10 "2016-03-10T20:37:46Z")

</div>

Victory! I was able to get this to work by implementing this…

> [@TIG](#):
>
> Use something like:
> 
> ents.intersect\_with( false, tr, ents, tr, true, ents.to\_a )

but instead of selecting the entire grid, I added it to an iteration per edge.  
Thanks for your help guys!
