-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathObjExport.cs
73 lines (69 loc) · 2.77 KB
/
ObjExport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.Collections.Generic;
using Rhino.Geometry;
namespace Rhino.Compute
{
public static class ObjExport
{
public static void ExportBrepsToObj(string filePath, IEnumerable<Brep> breps)
{
List<Mesh> meshes = new List<Mesh>();
foreach (Brep brep in breps)
{
Mesh[] brepMeshes = MeshCompute.CreateFromBrep(brep);
foreach (Mesh brepMesh in brepMeshes)
{
meshes.Add(brepMesh);
}
}
ExportMeshesToObj(filePath, meshes);
}
public static void ExportMeshesToObj(string filePath, IEnumerable<Mesh> meshes)
{
using (StreamWriter file = new StreamWriter(filePath, false))
{
int meshIndex = 0;
int vertexOffset = 1;
foreach (Mesh mesh in meshes)
{
if (mesh.Vertices.Count != mesh.Normals.Count)
{
throw new System.Exception("Different number of vertices and normals in the mesh.");
}
file.WriteLine("g Mesh-{0}", meshIndex);
foreach (Point3f vertex in mesh.Vertices)
{
file.WriteLine("v {0} {1} {2}", vertex.X, vertex.Y, vertex.Z);
}
foreach (Vector3f normal in mesh.Normals)
{
file.WriteLine("vn {0} {1} {2}", normal.X, normal.Y, normal.Z);
}
foreach (MeshFace face in mesh.Faces)
{
if (face.IsTriangle)
{
file.WriteLine("f {0}//{1} {2}//{3} {4}//{5}",
vertexOffset + face.A, vertexOffset + face.A,
vertexOffset + face.B, vertexOffset + face.B,
vertexOffset + face.C, vertexOffset + face.C
);
}
else if (face.IsQuad)
{
file.WriteLine("f {0}//{1} {2}//{3} {4}//{5} {6}//{7}",
vertexOffset + face.A, vertexOffset + face.A,
vertexOffset + face.B, vertexOffset + face.B,
vertexOffset + face.C, vertexOffset + face.C,
vertexOffset + face.D, vertexOffset + face.D
);
}
}
meshIndex += 1;
vertexOffset += mesh.Vertices.Count;
}
}
}
}
}