Get an obj object from mesh

rhinocommon
mesh
rhino6
obj

(Visiado Al Ordenador) #1

Hi,
I have a mesh and I’d like to convert it to obj in a string all is fine but I need help to make ‘f’ lines in obj for the moment I have this:

> public static String ExtractRenderMesh(Rhino.RhinoDoc doc)
>         {
>             String s = "";
>             Rhino.DocObjects.ObjRef objRef = null;
>             Rhino.Commands.Result rc = Rhino.Input.RhinoGet.GetOneObject("Select surface or polysurface", false, Rhino.DocObjects.ObjectType.Brep, out objRef);
>             if (rc != Rhino.Commands.Result.Success) return s;
>             Rhino.DocObjects.RhinoObject obj = objRef.Object();
>             if (null == obj) return s;
>             System.Collections.Generic.List<Rhino.DocObjects.RhinoObject> objList = new System.Collections.Generic.List<Rhino.DocObjects.RhinoObject>(1);
>             objList.Add(obj);
>             Rhino.DocObjects.ObjRef[] meshObjRefs = Rhino.DocObjects.RhinoObject.GetRenderMeshes(objList, true, false);
>             if (null != meshObjRefs)
>             {
>                 for (int i = 0; i < meshObjRefs.Length; i++)
>                 {
>                     Rhino.DocObjects.ObjRef meshObjRef = meshObjRefs[i];
>                     if (null != meshObjRef)
>                     {
>                         Rhino.Geometry.Mesh mesh = meshObjRef.Mesh();
>                         s += System.Environment.NewLine + " " + i + " " + System.Environment.NewLine;
>                         foreach (Rhino.Geometry.Point3f point in mesh.Vertices)
>                         {
>                             s += "v " + point.X + " " + point.Y + " " + point.Z +System.Environment.NewLine;
>                         }
>                         foreach (Rhino.Geometry.Point2f point in mesh.TextureCoordinates)
>                         {
>                             s += "vt " + point.X + " " + point.Y + " " + System.Environment.NewLine;
>                         }
>                         foreach (Rhino.Geometry.Vector3f point in mesh.Normals)
>                         {
>                             s += "vn " + point.X + " " + point.Y + " " + point.Z + System.Environment.NewLine;
>                         }
>                         if (null != mesh)
>                             doc.Objects.AddMesh(mesh);
>                     }
>                 }
>                 doc.Views.Redraw();
>             }
>             return s;
>         }

Get obj from RhinoDoc without write in file
Model serialization
(Visiado Al Ordenador) #2

I solved this inthis way:

> meshObjRefs = Rhino.DocObjects.RhinoObject.GetRenderMeshes(doc.Objects.GetObjectList(object_enumerator_settings), true, false);
>             if (null != meshObjRefs)
>             {
>                 for (int i = 0; i < meshObjRefs.Length; i++)
>                 {
>                     Mesh mesh = meshObjRefs[i].Mesh();
>                     foreach (Rhino.Geometry.Point3f point in mesh.Vertices)
>                     {
>                         s.Append("v ");
>                         s.Append(point.X.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Y.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Z.ToString(_allFloatDigits));
>                         s.Append(System.Environment.NewLine);
>                     }
>                     vertex = mesh.Vertices.Count;
>                     foreach (Rhino.Geometry.Point2f point in mesh.TextureCoordinates)
>                     {
>                         s.Append("vt ");
>                         s.Append(point.X.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Y.ToString(_allFloatDigits));
>                         s.Append(System.Environment.NewLine);
>                     }
>                     foreach (Rhino.Geometry.Vector3f point in mesh.Normals)
>                     {
>                         s.Append("vn ");
>                         s.Append(point.X.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Y.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Z.ToString(_allFloatDigits));
>                         s.Append(System.Environment.NewLine);
>                     }
>                     foreach (Rhino.Geometry.MeshFace face in mesh.Faces)
>                     {
>                         A = face.A + shift;
>                         B = face.B + shift;
>                         C = face.C + shift;
>                         s.Append("f ");
>                         s.Append(A);
>                         s.Append("/");
>                         s.Append(A);
>                         s.Append("/");
>                         s.Append(A);
>                         s.Append(" ");
>                         s.Append(B);
>                         s.Append("/");
>                         s.Append(B);
>                         s.Append("/");
>                         s.Append(B);
>                         s.Append(" ");
>                         s.Append(C);
>                         s.Append("/");
>                         s.Append(C);
>                         s.Append("/");
>                         s.Append(C);
>                         if (face.IsQuad)
>                         {
>                             D = face.D + shift;
>                             s.Append(" ");
>                             s.Append(D);
>                             s.Append("/");
>                             s.Append(D);
>                             s.Append("/");
>                             s.Append(D);
>                         }
>                         s.Append(System.Environment.NewLine);
>                     }
>                     foreach (Rhino.Geometry.Vector3f point in mesh.FaceNormals)
>                     {
>                         s.Append("#fn ");
>                         s.Append(point.X.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Y.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Z.ToString(_allFloatDigits));
>                         s.Append(System.Environment.NewLine);
>                     }
>                     foreach (Rhino.Geometry.Point3d point in mesh.TopologyVertices)
>                     {
>                         s.Append("#tv ");
>                         s.Append(point.X.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Y.ToString(_allFloatDigits));
>                         s.Append(" ");
>                         s.Append(point.Z.ToString(_allFloatDigits));
>                         s.Append(System.Environment.NewLine);
>                     }
>                     shift += vertex;
>                 }
>             }

Obj string to valid mesh