How to convert a Rhino Brep to a Rhino Mesh using RhinoCommon. This method is suitable for use in Grasshopper development and the Grasshopper C# component.
Create meshes from Brep
The code below produces a list of customised meshes based upon an input Brep and some mesh settings. The examples are written as static extension classes for compiled components (so you can access it directly as a Brep method), though can be easily adapted for the C# component too.
public static List<Mesh> BrepToMeshes(this Brep brep, double maxEdge) { Mesh[] mesh; MeshingParameters mp = new MeshingParameters(); mp.MaximumEdgeLength = maxEdge; mesh = Mesh.CreateFromBrep(brep, mp); return mesh.ToList<Mesh>(); }
This method essentially replicates the Grasshopper components below:
The MeshingParameters class replicates the ‘Mesh Settings’ component. You can assign settings to your mesh by creating an instance of MeshingParameters and accessing its properties, much as I have done with MaximumEdgeLength.
Joining the meshes
The output is a list of meshes. If you want to truly replicate the Mesh Brep component, you will also need to join all meshes in the list into a single mesh. This can be done with the Append method:
public static Mesh JoinMeshes(this List<Mesh> meshes) { var rtnmesh = new Mesh(); foreach (Mesh mesh in meshes) { rtnmesh.Append(mesh); } return rtnmesh; }
And for completeness, you can call the methods in a single line:
public static Mesh BrepToMesh(this Brep brep, double maxEdge) { return JoinMeshes(BrepToMeshes(brep, maxEdge)); }
If one has mixed geometry input, how can we make it accept meshes also?
So that it makes the brep->mesh for breps, keeps existing meshes, and joins it all together (perhaps joined per GH branch)