/// <summary> /// Exports the SubDivision meshes provided by the user to an .Obj file. Each Subvision Mesh /// is represented by a group. Groups are automatically named. /// </summary> /// <param name="mesh">The SubDivision Mesh array to be exported in one file</param> /// <param name="filePath">The meshes are exported to this file Path, or the active directory if only file name is provided</param> /// <returns></returns> public static bool ExportToOBJ(DSSubDivisionMesh[] mesh, string filePath) { if (mesh == null || mesh.Length == 0) { throw new System.ArgumentNullException("mesh"); } if (string.IsNullOrWhiteSpace(filePath)) { throw new System.ArgumentNullException("filePath"); } DSMeshData meshData = new DSMeshData(); for (int i = 0; i < mesh.Length; ++i) { meshData.AddGroup("Group_" + i); foreach (var vertices in mesh[i].FaceIndices) { for (int j = 0; j < vertices.Length; ++j) { vertices[j] += meshData.Vertices.Count + 1; } meshData.Groups[i].AddFace(vertices); } meshData.Vertices.AddRange(mesh[i].Vertices); } if (!filePath.EndsWith(".obj")) { filePath += ".obj"; } if (!Path.IsPathRooted(filePath)) { string foldername = Path.GetDirectoryName(DSGeometrySettings.RootModulePath); filePath = Path.Combine(foldername, filePath); } return(ObjHandler.Export(meshData, filePath)); }
internal static DSMeshData Import(String filePath) { StreamReader fileStream = new StreamReader(filePath); DSMeshData meshData = new DSMeshData(); List <string> invalidData = new List <string>(); while (!fileStream.EndOfStream) { String line = fileStream.ReadLine().Trim(); if (String.IsNullOrEmpty(line) || line[0] == '#') { continue; } String[] data = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (data.Length < 2) { invalidData.Add(line); continue; } KeyValuePair <string, string> parsedLineData = new KeyValuePair <string, string>(data[0], data[1]); switch (parsedLineData.Key) { case "v": //vertex if (!meshData.AddVertex(parsedLineData.Value)) { invalidData.Add(line); } break; case "f": //face if (!meshData.AddFace(parsedLineData.Value)) { invalidData.Add(line); } break; case "g": //group if (!meshData.AddGroup(parsedLineData.Value)) { invalidData.Add(line); } break; case "vn": //vertex normal if (!meshData.AddNormal(parsedLineData.Value)) { invalidData.Add(line); } break; case "vt": //texture if (!meshData.AddTexture(parsedLineData.Value)) { invalidData.Add(line); } break; case "mtllib": //material library if (!meshData.AddMaterial(parsedLineData.Value)) { invalidData.Add(line); } break; case "usemtl": //use material library meshData.SetMaterial(parsedLineData.Value); break; default: invalidData.Add(line); break; } } return(meshData); }
/// <summary> /// Exports the SubDivision meshes provided by the user to an .Obj file. Each Subvision Mesh /// is represented by a group. Groups are automatically named. /// </summary> /// <param name="mesh">The SubDivision Mesh array to be exported in one file</param> /// <param name="filePath">The meshes are exported to this file Path, or the active directory if only file name is provided</param> /// <returns></returns> public static bool ExportToOBJ(DSSubDivisionMesh[] mesh, string filePath) { if (mesh == null || mesh.Length == 0) throw new System.ArgumentNullException("mesh"); if (string.IsNullOrWhiteSpace(filePath)) throw new System.ArgumentNullException("filePath"); DSMeshData meshData = new DSMeshData(); for (int i = 0; i < mesh.Length; ++i) { meshData.AddGroup("Group_" + i); foreach (var vertices in mesh[i].FaceIndices) { for (int j = 0; j < vertices.Length; ++j) vertices[j] += meshData.Vertices.Count + 1; meshData.Groups[i].AddFace(vertices); } meshData.Vertices.AddRange(mesh[i].Vertices); } if (!filePath.EndsWith(".obj")) filePath += ".obj"; if (!Path.IsPathRooted(filePath)) { string foldername = Path.GetDirectoryName(DSGeometrySettings.RootModulePath); filePath = Path.Combine(foldername, filePath); } return ObjHandler.Export(meshData, filePath); }
internal static DSMeshData Import(String filePath) { StreamReader fileStream = new StreamReader(filePath); DSMeshData meshData = new DSMeshData(); List<string> invalidData = new List<string>(); while (!fileStream.EndOfStream) { String line = fileStream.ReadLine().Trim(); if (String.IsNullOrEmpty(line) || line[0] == '#') continue; String[] data = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (data.Length < 2) { invalidData.Add(line); continue; } KeyValuePair<string, string> parsedLineData = new KeyValuePair<string, string>(data[0], data[1]); switch (parsedLineData.Key) { case "v": //vertex if (!meshData.AddVertex(parsedLineData.Value)) invalidData.Add(line); break; case "f": //face if (!meshData.AddFace(parsedLineData.Value)) invalidData.Add(line); break; case "g": //group if (!meshData.AddGroup(parsedLineData.Value)) invalidData.Add(line); break; case "vn": //vertex normal if (!meshData.AddNormal(parsedLineData.Value)) invalidData.Add(line); break; case "vt": //texture if (!meshData.AddTexture(parsedLineData.Value)) invalidData.Add(line); break; case "mtllib": //material library if (!meshData.AddMaterial(parsedLineData.Value)) invalidData.Add(line); break; case "usemtl": //use material library meshData.SetMaterial(parsedLineData.Value); break; default: invalidData.Add(line); break; } } return meshData; }