algorithmic modeling for Rhino
Hello All,
I am a beginner in C#. I am trying to, as a first step, to decompose a Quad Mesh and make a list of Meshes each Mesh consisting of one face and 4 vertices (ie. Mesh Explode).
Issue is, with the code written, the Vertices for All Meshes in the list are equal to the value of the Points corresponding to the last iteration in the loop... I guess I must be having a pointer issue ?
Thanks in advance.
Kind regards,
Jean P.
PS. Tried several codes including a foreach... However this did not address the issue. Here is perhaps not the most effective code.
Here is the piece of code:
private void RunScript(Mesh mesh, ref object A)
{
Point3d[] lstPt = new Point3d[4];
// meshOne is the List of Single Face Meshes
List <Mesh> meshOne = new List<Mesh>();
MeshFace tempface = new MeshFace(0, 1, 2, 3);
// temporary Mesh created for use in Loop
Mesh tmpMesh = new Mesh();
// a is the vertice count which will be iterated
int a = mesh.Vertices.Count;
for (int i = 0; i <a; i += 4){
lstPt[0] = mesh.Vertices[i];
lstPt[1] = mesh.Vertices[i + 1];
lstPt[2] = mesh.Vertices[i + 2];
lstPt[3] = mesh.Vertices[i + 3];
tmpMesh.Vertices.SetVertex(0, lstPt[0]);
tmpMesh.Vertices.SetVertex(1, lstPt[1]);
tmpMesh.Vertices.SetVertex(2, lstPt[2]);
tmpMesh.Vertices.SetVertex(3, lstPt[3]);
tmpMesh.Faces.SetFace(0, tempface);
meshOne.Add(tmpMesh);
}
A = meshOne;
}
Tags:
There were several issues w. the code.
Firstly, I should have iterated over the Face (as opposed to vertices).
Secondly, the instantiation of tmpMesh should have happend in the loop. This solves the issue of the pointer (?).
For sake of completness, the correct approach can be found in this link:
http://james-ramsden.com/how-to-explode-a-mesh-in-grasshopper/
Best, Jean P.
private void RunScript(Mesh M, ref object F)
{
List<Mesh> meshlist = new List<Mesh>();
for (int i = 0; i <= M.Faces.Count - 1; i++) {
//The MeshFace for each loop.
MeshFace face = M.Faces[i];
//The new mesh.
Mesh newMesh = new Mesh();
//Adding the vertex of face.
newMesh.Vertices.Add(M.Vertices[face.A]);
newMesh.Vertices.Add(M.Vertices[face.B]);
newMesh.Vertices.Add(M.Vertices[face.C]);
//Keep the colors.
if (M.VertexColors.Count > 0) {
newMesh.VertexColors.SetColor(0, M.VertexColors[face.A]);
newMesh.VertexColors.SetColor(1, M.VertexColors[face.B]);
newMesh.VertexColors.SetColor(2, M.VertexColors[face.C]);
}
//Adding the MeshFace.
if ((face.IsTriangle)) {
newMesh.Faces.AddFace(0, 1, 2);
} else {
newMesh.Vertices.Add(M.Vertices[face.D]);
newMesh.Faces.AddFace(0, 1, 2, 3);
if (M.VertexColors.Count > 0) {
newMesh.VertexColors.SetColor(3, M.VertexColors[face.D]);
}
}
//To list.
meshlist.Add(newMesh);
}
F = meshlist;
}
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2025 Created by Scott Davidson.
Powered by