# RhinoCommon - Cap Connected Planar Holes?

#1

Hi Guys,

imagine a cube-Brep where we delete two adjoining faces. We now have a Brep with two connected planar holes, that i would like to cap automatically.

I’ve seen some “Cap Planar Holes Ex” component in Grasshopper, which is capable of doing that.
Is there something similar in RhinoCommon as well?

Best regards, Heinz

(Vicente Soler) #2

My guess of what that component additionally does, I might be wrong:
It extracts the naked boundaries as a closed polyline, runs the Mesh.MeshFromClosedPolyline(), converts the resulting meshes to breps and joins everything together.

(Vicente Soler) #3

This seems to work:

``````  Brep CapPolygonHoles(Brep brep, double tol)
{
brep.CapPlanarHoles(tol);
var naked = brep.DuplicateNakedEdgeCurves(true, true);
var joinedNaked = Curve.JoinCurves(naked);

foreach(Curve curve in joinedNaked)
{
Polyline pl;
if(curve.TryGetPolyline(out pl))
{
var mesh = Mesh.CreateFromClosedPolyline(pl);
var patch = Brep.CreateFromMesh(mesh, true);
var breps = Brep.JoinBreps(new []{brep, patch}, tol);

if(breps.Length == 1)
brep = breps.First();
}
}

brep.MergeCoplanarFaces(tol);
return brep;
}``````

#4

Thanks, @visose, this is a promising workaround! I’m unsure if it will work 100%, since it heavily relies on what the mesher does, but for my needs it should suffice. Also, the MergeCoplanarFaces method is quite expensive, so we’ll see how that will work out for the case that the mesher returns a lot of faces.

Best regards, Heinz

(Vicente Soler) #5

If you’re using Rhino 6, rather than using Brep.MergeCoplanarFaces() you could run mesh.Ngons.AddPlanarNgons() on the mesh first, then loop through the ngons, get the planar boundary and create brep faces using Brep.CreateTrimmedPlane(). Unforunately Brep.CreateFromMesh() ignores ngons even if they’re planar.

This would be equivalent to running MergeCoplanarFaces() only on the patches and not on the final joined brep (if one of the patches is coplanar to the existing brep, it won’t merge them).

#6

@visose, thanks again for your routine, i just tested it.

Unfortunately, i found that my initial example where the curve to cap exhibits only four straight segments may have been a bit too simple.

So, while the extraction of a naked boundary curve works, the mesher won’t accept the curve, since it consists of quite a lot of straight AND curved sub-curves.

Even if the curve would get meshed properly, it would consist of a lot of triangles in order to satisfy my tolerance needs. I like your idea of employing n-gons, but i have been using these already and i think they’re limited to 12 sides, besides having problems with non-convex faces.

In my opinion, it would be better to identify the curve’s two coplanar sub-parts, close each and create a breps from closed planar loops for each sub-curve.

However, that’s not really trivial, since you’d need to generate planes for each triple of successive points, rule out co-linear triples, compare all of these planes with each other, etc. etc. Or similar.

Please let me know if you have any ideas here…

Best regards, Heinz