# Deleting Redundant Edges from a Solid

**URL:** <https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585>\
**Category:** Ruby API\
**Created:** [September 28, 2019, 6:13am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585 "2019-09-28T06:13:03Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [September 28, 2019, 6:13am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/1 "2019-09-28T06:13:03Z")

</div>

Yet another weird topological problem. As many of you know I am battling with my complex roof module right now and so far it is proving more difficult than I would like to admit.

I have the plugin creating the roof primitive 99% of the time but in a few cases I end up with an extra edge or two that are not actually required to make the group a solid. I call these redundant edges. The extra edges do not render the group a non-solid but they are splitting up faces that I would rather not split.

Has anyone ever come across and algorithm or method for removing non-essential edges from a solid (manifold)?

 ![Capture](https://global.discourse-cdn.com/sketchup/original/3X/d/c/dcd1269e671a4045fb179721bfe916205588a53d.jpeg)

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [September 28, 2019, 8:17am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/2 "2019-09-28T08:17:09Z")

</div>

Turns out the solution wasn’t too hard to devise:

```
def check_redundant_edges
	
	edge_list = @group1.entities.grep(Sketchup::Edge)

	edge_list.each do |e|
		next unless e.valid?
		next if e.line[1][2].round(5) == 0.0
		next if e.start.position[2] == @Fasciaz || e.end.position[2] == @Fasciaz
		
		if e.start.position[2] > e.end.position[2]
			ve = e.start
		else
			ve = e.end
		end

		edges_ve = ve.edges
		edges_ve.delete(e)
		
		next unless edges_ve[0].line[1].samedirection?(edges_ve[1].line[1])
		
		e.erase!
		puts 'redundant edge removed'
	end
end

```

I will say that this is probably one of my most efficient algorithms to date.

---

<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:** [September 28, 2019, 1:57pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/3 "2019-09-28T13:57:20Z")

</div>

**EDIT … Please see this other thread using plane / vertex comparison …**

- [How to make a selected face be truly flat](https://forums.sketchup.com/t/how-to-make-a-selected-face-be-truly-flat/104622)

* * *

Would something like this also work ?

```ruby
# Erase redundant edges in an entities context. A face is redundant if it is
# used by more than 1 face and all it's faces have parallel normal vectors.
#
# @param context [Sketchup::Entities] can be any entities context (choose from: 
# model.entities, model.active_entities, group.entities or definition.entities.)
#
# @return [Integer] count of deleted edges.
#
def erase_redundant_edges(context)
  edge_list = context.grep(Sketchup::Edge)
  redundant = edge_list.find_all do |e|
    next false unless e.faces > 1
    vector = e.faces.first.normal
    e.faces.all? { |f| f.normal.parallel?(vector) }
  end
  unless redundant.empty?
    context.model.start_operation('Remove redundant edges',true,false,true)
      context.erase_entities(redundant)
    context.model.commit_operation
  end
  redundant.count { |e| e.deleted? }
end

```

* * *

**Edited** : To put whole operation inside `unless redundant.empty?` block.  
**Edit-2** : Corrected first line to remove call to `.entities` as `context` should be an `Sketchup::Entities` instance object. Added docsting for YARD. Adding return a count of deleted edges.  
**Edit-3** : Corrected the `erase_entities` line (in the operation) to remove `.entities` call (as it is now an `entities` context.)

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [September 28, 2019, 7:40pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/4 "2019-09-28T19:40:19Z")

</div>

What is this ‘context’ that you are putting in front of the start and commit operation methods, how does that work? I’ve never seen that before.

---

<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:** [September 28, 2019, 7:45pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/5 "2019-09-28T19:45:21Z")

</div>

‘context’ can be any entities context:- choose from…

- model.entities
- model.active\_entities
- group.entities
- definition.entities

---

<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:** [September 28, 2019, 8:06pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/6 "2019-09-28T20:06:52Z")

</div>

> [@medeek](#):
>
> What is this ‘context’ that you are putting in front of the start and commit operation methods, how does that work?

What I actually did was renamed the `@group1` reference in your code snippet to `context` but one step UP from what TIG described. Perhaps it should be named `object` instead, but it cannot be just any object class. It has to `respond_to?` method calls `:entities` and `:model`.  
So perhaps `container` would be a better name ?

I just realized I may have “outsmarted” myself. I thought at the last minute that you could possibly pass in either: a group, a component or the model proper.  
BUT, now I see that this will not work as written. Ie, a call to `model.model` would produce a `NoMethodError`, as well as if you passed in a ComponentInstance (the `entities` call needs to be made on it’s definition.)

* * *

**So really TIG has the right of it**.

The method’s arg should be any of his listed 4 entities collections …  
and the 1st line should change from …

```ruby
 edge_list = context.entities.grep(Sketchup::Edge)

```

… to …

```ruby
 edge_list = context.grep(Sketchup::Edge)

```

I’ll change the above snippet for future readers and add TIG’s list as method params.

* * *

And also changed the `erase_entities` line (in the operation) to remove `.entities` call as it is now (as always should have been,) an `entities` context.

```ruby
context.entities.erase_entities(redundant)

```

… is now …

```ruby
context.erase_entities(redundant)

```

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [October 2, 2019, 1:38pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/7 "2019-10-02T13:38:27Z")

</div>

Comparing normals of faces is not sufficient to determine if you can erase the edges between them. This is due to tolerances when you get small faces or slither faces etc.

Instead take the plane of one of the faces and see if the vertices of the other faces lies on that same plane. This is now SketchUp compares if faces are planar to each other.

(CleanUp has this implemented: [GitHub - thomthom/cleanup: Cleans up SketchUp models as best it can.](https://github.com/thomthom/cleanup))

---

<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 2, 2019, 4:54pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/8 "2019-10-02T16:54:45Z")

</div>

Which we’ve been discussing in this other thread the past few days …

- [How to make a selected face be truly flat](https://forums.sketchup.com/t/how-to-make-a-selected-face-be-truly-flat/104622)

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 13, 2019, 10:03pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/9 "2019-10-13T22:03:32Z")

</div>

When I do the boolean subtraction to cut out the various roof planes occasionally I get these extra edges (that are redundant). It seems to be unpredictable or somewhat random, sometimes they appear and sometimes they don’t, I’m not entirely sure why this behavior exists:

 ![truss_su573_800](https://global.discourse-cdn.com/sketchup/original/3X/2/e/2ecbcc5f72821bbac124a1fb2780a6c091202c8d.jpeg)

My previous algorithm was quite specific and dealt only with redundant edges on the actual roof planes.

With the interior gables enabled I’m now facing the possibility of redundant edges on the vertical (wall) faces as well.

Looks like I will need to revisit this topic and see if I can come up with a more general solution.

I tend to dislike general solutions since they tend to be more computationally expensive especially when you have a complex shape with many polygons. The downside to the more specific algorithms is that if something new likes this comes along then you have to re-adjust. It’s debatable as to which is the better way of doing things.

---

<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:** [October 13, 2019, 10:06pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/10 "2019-10-13T22:06:48Z")

</div>

is the skp somewhere?

john

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 13, 2019, 10:13pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/11 "2019-10-13T22:13:48Z")

</div>

View model here:

https://3dwarehouse.sketchup.com/embed.html?mid=401c7825-57fb-4973-afd5-69540f114983&width=580&height=326

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 13, 2019, 10:41pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/12 "2019-10-13T22:41:29Z")

</div>

The first thing that is jumping out at me is that the edges in question are all vertical.

I use this chunk of code to detect for horizontal edges (or ridges):

`if edgei.line[1][2].round(5) == 0.0`

What would be the most efficient way to check for vertical edges?

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 13, 2019, 10:46pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/13 "2019-10-13T22:46:26Z")

</div>

Perhaps something like this?

`if edgei.line[1].parallel?(Z_AXIS)`

---

<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:** [October 13, 2019, 10:48pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/14 "2019-10-13T22:48:15Z")

</div>

you must know the start points for all of those already?

can you not find new edges with those start point…

john

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 13, 2019, 10:56pm UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/15 "2019-10-13T22:56:31Z")

</div>

This seems to work quite well:

```
if e.line[1].parallel?(Z_AXIS)
	e.erase!
	puts 'vertical redundant edge removed'
end

```

Its not a general solution but it does remove all of the vertical edges, which in this particular case is exactly what I need.

At some point though I may need the completely general case. I do like the idea of comparing the normal vector of faces that belong to an edge and if they are the same direction then the edge is redundant. If you look at a solid it quickly becomes apparent that any given edge can only belong or border two faces.

Maybe something like this:

```
def remove_redundant_edges
  edge_list = @group1.entities.grep(Sketchup::Edge)
  edge_list.each do |e|
    next unless e.valid?
    edgefaces = e.faces
    if edgefaces[0].normal.parallel?(edgefaces[1].normal)
      e.erase!
      puts 'redundant edge removed'
    end
  end
end

```

---

<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 14, 2019, 2:40am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/16 "2019-10-14T02:40:02Z")

</div>

BUT … you only want to erase vertical edges that divide faces that are planar with one another.

There are other vertical faces at the bottom corners of the roof slopes, and you’ll have vertical edges on dormers.

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [October 14, 2019, 3:01am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/17 "2019-10-14T03:01:37Z")

</div>

True, but I forgot to mention that the bottom section of the roof (fascia) does not exist at first, I push-pull that later. I also don’t have dormers, that will be part of the secondary roof module which will be a completely different roof assembly and have its own roof primitive.

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [October 25, 2019, 10:14am UTC](https://forums.sketchup.com/t/deleting-redundant-edges-from-a-solid/104585/18 "2019-10-25T10:14:13Z")

</div>

> [@medeek](#):
>
> Looks like I will need to revisit this topic and see if I can come up with a more general solution.

Iterate over your edges and for any edge connected to two faces, check if they are co-planar:

> <https://github.com/thomthom/cleanup/blob/eaa4050c7f34d311ae59b50047d07e9f5ef63d2e/src/tt_cleanup/core.rb#L974-L979>
