Convert OpenNURBS OnMesh to rhinocommon mesh object

rhinocommon

#1

Hello,

I am trying to convert a rhino.NET mesh object to rhinocommon mesh object and I always get the value to be undefined. Could any one suggest how I should go about it?
Rhino::Geometry::Mesh^ ConvertToCommonMesh(RMA::OpenNURBS::OnMesh^ mesh)
{
Rhino::Geometry::Mesh^ meshCommon = Rhino::Runtime::Interop::FromOnMesh(mesh);
return meshCommon;
}


(Dale Fugier) #2

This works from a C#, RhinoCommon plug-in:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var on_mesh = new RMA.OpenNURBS.OnMesh(6, 8, false, false);
  on_mesh.SetVertex(0, 0.5, 0.5, 0.5);
  on_mesh.SetVertex(1, 0.5, 0.5, -0.5);
  on_mesh.SetVertex(2, 0.5, -0.5, 0.5);
  on_mesh.SetVertex(3, 0.5, -0.5, -0.5);
  on_mesh.SetVertex(4, -0.5, 0.5, 0.5);
  on_mesh.SetVertex(5, -0.5, 0.5, -0.5);
  on_mesh.SetVertex(6, -0.5, -0.5, 0.5);
  on_mesh.SetVertex(7, -0.5, -0.5, -0.5);
  on_mesh.SetQuad(0, 0, 1, 5, 4);
  on_mesh.SetQuad(1, 0, 4, 6, 2);
  on_mesh.SetQuad(2, 0, 2, 3, 1);
  on_mesh.SetQuad(3, 7, 3, 2, 6);
  on_mesh.SetQuad(4, 7, 6, 4, 5);
  on_mesh.SetQuad(5, 7, 5, 1, 3);
  on_mesh.ComputeVertexNormals();
  on_mesh.Compact();

  if (on_mesh.IsValid())
  {
    var rh_mesh = Rhino.Runtime.Interop.FromOnMesh(on_mesh);
    if (null != rh_mesh && rh_mesh.IsValid)
    {
      doc.Objects.AddMesh(rh_mesh);
      doc.Views.Redraw();
    }
  }

  return Result.Success;
}

Perhaps your function should validate the mesh parameter before trying to convert?

– Dale


#3

Hi Dale,

Thank you for your input. But I am still stuck with the same problem, this is my function currently:

Rhino::Geometry::Mesh^ ConvertToCommonMesh(RMA::OpenNURBS::OnMesh^ mesh) { Rhino::Geometry::Mesh^ meshCommon = gcnew Rhino::Geometry::Mesh(); if (mesh->IsValid()) { meshCommon->Append(Rhino::Runtime::Interop::FromOnMesh(mesh)); System::String^ logStr; meshCommon->IsValidWithLog(logStr); } }

In this case the value that logStr takes is:
</>ON_Mesh.m_F.Count() < 1 (should be at least 1).


#4

Hi Dale,

I tried with your code in C++/CLI and it works well. This is the code I wrote

RMA::OpenNURBS::OnMesh^ on_mesh = gcnew RMA::OpenNURBS::OnMesh ( 6, 8, false, false );
              on_mesh->SetVertex ( 0, 0.5, 0.5, 0.5 );
              on_mesh->SetVertex ( 1, 0.5, 0.5, -0.5 );
              on_mesh->SetVertex ( 2, 0.5, -0.5, 0.5 );
              on_mesh->SetVertex ( 3, 0.5, -0.5, -0.5 );
              on_mesh->SetVertex ( 4, -0.5, 0.5, 0.5 );
              on_mesh->SetVertex ( 5, -0.5, 0.5, -0.5 );
              on_mesh->SetVertex ( 6, -0.5, -0.5, 0.5 );
              on_mesh->SetVertex ( 7, -0.5, -0.5, -0.5 );
              on_mesh->SetQuad ( 0, 0, 1, 5, 4 );
              on_mesh->SetQuad ( 1, 0, 4, 6, 2 );
              on_mesh->SetQuad ( 2, 0, 2, 3, 1 );
              on_mesh->SetQuad ( 3, 7, 3, 2, 6 );
              on_mesh->SetQuad ( 4, 7, 6, 4, 5 );
              on_mesh->SetQuad ( 5, 7, 5, 1, 3 );
              on_mesh->ComputeVertexNormals ();
              on_mesh->Compact ();
              if (on_mesh->IsValid ())
              {
                Rhino::Geometry::Mesh^ rh_mesh = Rhino::Runtime::Interop::FromOnMesh ( on_mesh );
                if (rh_mesh != nullptr && rh_mesh->IsValid)
                {
                 ...
                }
              }

So I guess then the problem is with my mesh object. I will try with a different mesh.

Thanks
Sunayana