C++ add ngon to mesh

Would it be possible to get a sample how to add ngon to mesh using c++?

Also why mesh.SetTriangle(int,int,int,int) and mesh.SetQuad(int,int,int,int,int) have +1 number?
For setting up triangle what does the first int represent - is it id?

Is it something like this for defining hex mesh as ngon?
Or there are other option to do this?

int face_count = 6;
int vertex_count = 12;
BOOL bVertexNormals = FALSE;
BOOL bTextureCoordinates = FALSE;

ON_Mesh mesh(face_count, vertex_count, bVertexNormals, bTextureCoordinates);

mesh.SetVertex(0, ON_3fPoint(0.45f, 5.37f, 0.0f));
mesh.SetVertex(1, ON_3fPoint(-0.16f, 4.32f, 0.0f));
mesh.SetVertex(2, ON_3fPoint(0.44f, 3.26f, 0.0f));
mesh.SetVertex(3, ON_3fPoint(1.66f, 3.25f, 0.0f));
mesh.SetVertex(4, ON_3fPoint(2.28f, 4.31f, 0.0f));
mesh.SetVertex(5, ON_3fPoint(1.67f, 5.37f, 0.0f));


mesh.SetTriangle(0,0,2,1);
mesh.SetTriangle(1,5,2,0);
mesh.SetTriangle(2,3,2,5);
mesh.SetTriangle(3,5,4,3);


mesh.ComputeVertexNormals();
mesh.Compact();

unsigned  int m_Vcount = 6;
const unsigned  int m_vi[8] = { 5,4,3,2,1,0 };
unsigned  int m_Fcount = 4;
const unsigned  int m_fi[6] = {0,1,2,3};


mesh.AddNgon(m_Vcount, m_vi, m_Fcount, m_fi);

context.m_doc.AddMeshObject(mesh);
context.m_doc.Redraw();

Hi @Petras_Vestartas,

Here is a simple example, very similar to what you’ve typed up.

https://github.com/mcneel/rhino-developer-samples/blob/6/cpp/SampleCommands/cmdSampleCreatePlanarMeshes.cpp

– Dale

Thank you :slight_smile:

Hi I was going trough the code of creating ngon meshes.

I would like to ask about github line 70 :
const ON_PolylineCurve* curve = curves[0];

If I use i instead of first id:
const ON_PolylineCurve* curve = curves[i];

I create multiple meshes as image attached:

Could you show how to append all meshes into one single mesh?

Here these meshes are added to rhino:

	ON_SimpleArray<ON_Mesh*> meshes;
	RhCreatePlanarMeshes(curves, meshes);
	for (int i = 0; i < meshes.Count(); i++)
	{
		CRhinoMeshObject* mesh_obj = new CRhinoMeshObject();
		mesh_obj->SetMesh(meshes[i]);
		context.m_doc.AddObject(mesh_obj);
	}

But I would like to know how to append all of them to one empty mesh and clean coincident points.
Something like pseudo code below, but I do not know C++ how correctly append meshes to other mesh.

CRhinoMeshObject* polymesh = new CRhinoMeshObject();

ON_SimpleArray<ON_Mesh*> meshes;
RhCreatePlanarMeshes(curves, meshes);


for (int i = 0; i < meshes.Count(); i++)
     polymesh.Append(meshes[i]);


context.m_doc.AddObject(polymesh);

Use ON_Mesh::Append.

– Dale