Create NGon Mesh Rhinocommon c#

Hi, I am giving PolylineCurve List as input. That means I have 100 closed planer Polyline Curve and I want a single mesh with ngons .
With single Ngon I could work out how it works.

–Can I take multiple NGons as input and get a single mesh ?
–Does NGon supported in grasshopper Rhino 6?

I tried to draw Ngon mesh, but it is giving only the Ngon for the last Polyline curve in the list.
Just one Ngon.
I never used Ngon before, so please let me know what I did wrong below:

It can be a silly mistake but I am not able to find it ;

public static Mesh MeshFromPolyline(List<PolylineCurve> plineR)
        {
            Mesh mesh = null;
            int countd = 0;
            int countf = 0;
            for (int i = 0; i < plineR.Count; i++)
            {
                if (null != plineR[i] &&
                plineR[i].IsClosed &&
                plineR[i].IsPlanar() &&
                plineR[i].PointCount >= 4
                )
                {
                 mesh = Mesh.CreateFromClosedPolyline(plineR[i].ToPolyline());
                 if (null != mesh)
                 {
                        var vertices_ind_list = new List<int>(mesh.Vertices.Count);
                        for (var vi = 0; vi < mesh.Vertices.Count; vi++)
                        {
                            vertices_ind_list.Add(countd + vi);
                        }
                            countd = countd + mesh.Vertices.Count;
                            var faces_ind_list = new List<int>(mesh.Faces.Count);
                        for (var fi = 0; fi < mesh.Faces.Count; fi++)
                        {
                            faces_ind_list.Add(countf + fi);
                        }
                            countf = countf + mesh.Faces.Count;

                            var ngon = MeshNgon.Create(vertices_ind_list.ToArray(), 
                            faces_ind_list.ToArray());
                            if (null != ngon)
                                mesh.Ngons.AddNgon(ngon);

                        }
                    }
                }
          

           

            if (null != mesh)
                    {
                        mesh.FaceNormals.ComputeFaceNormals();
                        mesh.Normals.ComputeNormals();
                        mesh.Compact();
                        if (!mesh.IsValid)
                            mesh = null;
                    }
                
           
            return mesh;
            
        }

1 Like

Hi @tsiddikee

the simplest method I can think of is this one:

    Mesh result = new Mesh();
    foreach(var poly in P)
    {
      Mesh temp = Mesh.CreateFromClosedPolyline(poly);
      temp.Ngons.AddPlanarNgons(0.01); // some meaningful tolerance
      result.Append(temp);
    }

    A = result;

polyline.gh (6.7 KB)

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Hi Giulio, Thanks for the reply. Does that mean, NGon is supported from grasshopper plugin in Grasshopper and Rhinocommon plugin in visual studio ?I am using it inside a quite long code.
When I am running my component in gh, it is running endlessly. But it works well, when I am making a Rhino plugin.
So my doubt is, NGon is not still supported in Grasshopper plugin from Visual Studio.
But your example says it should be supported already.

Please let me know.
Many thanks !

There isn’t so much support for NGons in GH1, but when you create them, they show in the viewport and you are able to bake them. Like, with my file. Are you able to bake it?

Thanks

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi @piac,

Can we get an example of an ngon mesh creation with closed non-planar polyline?

I think you’ll have to just set up the Ngon from scratch.
Maybe @dale already has an example for that?

Hi @djordje,

Rhino does not have a function that will take this type of curve and return a collection of closed, planar polylines. So, you will need to provide one. Once you do, cooking up a mesh (with or without ngons) should be straight forward.

– Dale

Hi @dale,

Thank you for the reply.
Can you please correct the example attached below?

I am getting a regular mesh, without ngons.
Here is a screenshot of how the mesh looks like:

import Rhino

tempMesh = Rhino.Geometry.Mesh()

for pt in pts:  # [v0, v1, v2, v3, v4]
    tempMesh.Vertices.Add(pt)

for i in range(3):  # [F1, F2, F3]
    tempMesh.Faces.AddFace(0,1,2)
    tempMesh.Faces.AddFace(0,2,3)
    tempMesh.Faces.AddFace(0,3,4)

outerBoundaryMeshVertexIndices = [0,1,2,3,4]
innerNgonMeshFacesIndices = [0,1,2]
ngon = Rhino.Geometry.MeshNgon.Create(outerBoundaryMeshVertexIndices, innerNgonMeshFacesIndices)
tempMesh.Ngons.AddNgon(ngon)

finalMesh = Rhino.Geometry.Mesh()
finalMesh.Append(tempMesh)

And another question: does an ngon have to be consisted of triangular faces, or it can contain a combinations of triangles and quads?

Thank you.

create_ngon_mesh.gh (4.8 KB)
create_ngon_mesh.3dm (32.3 KB)

Hi @djordje,

Try this:

import Rhino

m = Rhino.Geometry.Mesh()

for pt in pts:  # [v0, v1, v2, v3, v4]
    m.Vertices.Add(pt)

m.Faces.AddFace(0,2,1)
m.Faces.AddFace(0,3,2)
m.Faces.AddFace(0,4,3)

m.Ngons.AddPlanarNgons(0.001);
m.FaceNormals.ComputeFaceNormals();
m.Normals.ComputeNormals();
m.Compact();

if m.IsValid:
    finalMesh = m

And yes, ngons can contain quad and triangles.

– Dale

1 Like

Hi @djordje

your code works here if I fix a bug relating to setting the faces in triplicate. (line 18)

test-djordje.py (883 Bytes)

1 Like

Thank you for the working example @dale!
@piac, thank you too! And for correcting my stupid mistake.