ExtractMeshfaces or DeleteMeshFaces c# RhinoCommon 6

Can I extractMeshFaces or DeleteMeshFaces in RhinoCommon c#.
I am developing plugins for Rhino 6.

I do not think that syntax is very much different that in 5

x is mesh

Extract mesh faces:

var faces = x.Faces; //get full collection of faces
MeshFace firstMeshFace = x.Faces[0]; //Get first one

Delete mesh faces:

 x.Faces.RemoveAt(0); //remove first face
  x.Faces.Clear(); //remove all of them

If you intend to delete multiple MeshFaces (after collecting indicies to the MeshFaces later to be deleted), then a typical error is to delete the faces in arbitrary order with a given set of face indicies, say indicies 1 and 2.

The problem: If you delete face[1] and then delete face[2], then the last deletion actually deleted the original face[3] (since deleting the first face[1] the entire list shifted one step towards the left), like so

[A|B|C|D|E]

and after deleting face[1] the list looks like so:

[A|C|D|E]

If now deleting face[2] the list will end up like this:

[A|C|E]

To solve this kind of problem, first sort the list of deletion indicies and then delete the highest indicies first, by traversing the mesh.Faces from the end of the face list towards the start of the list while deleting.

Example with a list of (face) indicies to be deleted from the mesh (this code also removes any duplicates):

var faceIndicesToDelete = new List<int>();
//
// ... collect your face indices
faceIndicesToDelete.Add(index);
// ...
// Finally sort and delete:
var sortedNoDupes = faceIndicesToDelete.Distinct().OrderByDescending(x => x);
m_mesh.Faces.DeleteFaces(sortedNoDupes);
m_mesh.Compact();

This code uses System.Linq. If sorting like this, the first example then would first delete index 2 and then index 1 resulting in C and the B being removed, like so:

[A|D|E]

At last, if removing multiple faces one by one, the removal is extremely slow while the method mesh.Faces.DeleteFaces(...) is very fast in comparison.

// Rolf

2 Likes

Thanks for the detail explanation. But I have done re-meshing through selected faces. To me re-meshing, I mean creating new mesh through selected faces, looks more reliable than deleting face. Again good to know. Many thanks !
Best wishes and happy new Year !
T