Merge All Coplanar Faces from python

Forgive me if this is an easy to find answer, I’m not finding the forum or api searching useful in this case.

I’ve made an object by BooleanUnion’ing multiple rs.AddBox() results and I can still see the coplanar internal edges. From the UI I can select the _MergeAllCoplanarFaces command easily enough, but I’d like to include it in my script instead.

import rhinoscriptsyntax as rs

obj = rs.GetObject()

brep = rs.coercebrep(obj)

success = brep.MergeCoplanarFaces(.001)

print('merged? {}'.format(success))

That was my naive attempt, but it doesn’t do what I want (and yet returns True). What am I missing?

Here’s the test object I’m using that demonstrates my issue.

test.3dm (2.7 MB)

Hi @adarcher,

I am not seeing any co-planar faces to merge. Am I confused?

– Dale

Maybe that’s why I’m confused as well. If you hit run the _MergeAllCoplanarFaces and select the object though, it’ll simplify the isolines–I’m assuming that’s merging the faces/surfaces?

Oh bother, I attached the wrong one. I’ll update.

I downloaded this one and it should have the issue. Here’s a SS of what I’m seeing:

test.3dm (182.3 KB)

The issue is that you first reference a brep from Rhino, then copy it into memory by coercing it to “become” a Rhino.Geometry.Brep. Now you merge the coplanar faces of this copied brep object, but the one in Rhino remains the same.

What you need to do is bake the merged brep to Rhino and delete the old one:

import rhinoscriptsyntax as rs
import scriptcontext as sc

obj = rs.GetObject()
brep = rs.coercebrep(obj)

success = brep.MergeCoplanarFaces(.001)

sc.doc.Objects.AddBrep(brep)  # bake the brep object
sc.doc.Objects.Delete(obj, True)  # delete the Rhino brep

Interesting. Is there a “better” way to go about what I’m doing? Or will I always be making a duplicate brep and having to delete the old one?

I honestly don’t know.

You can use sc.doc.Replace(obj_id,brep) instead of add/delete.

Sorry for the rather terse answer, I was on my phone before. Here is some more complete code which uses Replace() and also preserves the original object attributes (color, layer, object name, custom display mode, etc.)

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def TestMergeFaces():
    msg="Select a surface or polysurface to merge"
    if success:


Right now I’m trying to keep things simple as I debug issues with other things in the script. I’ll keep this around to beautify the final result though.