Exemple #1
0
 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;
 }
        internal static bool Export(DSMeshData meshData, String filePath)
        {
            try
            {
                StreamWriter file = File.CreateText(filePath);
                file.WriteLine("# 3D Library generated .obj  file");
                file.WriteLine("# DesignScript Studio #");
                file.WriteLine("##");
                file.WriteLine();

                foreach (var vertex in meshData.Vertices)
                {
                    file.WriteLine("v " + vertex.X + " " + vertex.Y + " " + vertex.Z);
                }
                file.WriteLine("# " + meshData.Vertices.Count + " vertices");
                file.WriteLine();

                foreach (var normal in meshData.Normals)
                {
                    file.WriteLine("vn " + normal.X + " " + normal.Y + " " + normal.Z);
                }
                file.WriteLine("# " + meshData.Normals.Count + " vertex normals");
                file.WriteLine();

                //foreach (var texture in meshData.Textures)
                //    file.WriteLine("vt " + texture.X + " " + texture.Y);
                //file.WriteLine("# " + meshData.Textures.Count + " texture vertices");

                foreach (var group in meshData.Groups)
                {
                    file.WriteLine("g " + group.Name);
                    foreach (var face in group.Faces)
                    {
                        file.Write("f");
                        for (int i = 0; i < face.VertexArray.Length; ++i)
                        {
                            file.Write(" " + face.VertexArray[i] + "/" + (face.TextureArray[i] != 0 ? face.TextureArray[i].ToString() : "") + "/" + (face.NormalArray[i] != 0 ? face.NormalArray[i].ToString() : ""));
                        }
                        file.WriteLine();
                    }
                    file.WriteLine("# " + group.Faces.Count + " faces");
                    file.WriteLine();
                }
                file.Close();
                return(true);
            }
            catch (System.UnauthorizedAccessException e)
            {
                throw new System.UnauthorizedAccessException(e.Message);
            }
            catch
            {
                throw new ArgumentException("Path is invalid");
            }
        }
Exemple #3
0
        internal static bool Export(DSMeshData meshData, String filePath)
        {
            try
            {
                StreamWriter file = File.CreateText(filePath);
                file.WriteLine("# 3D Library generated .obj  file");
                file.WriteLine("# DesignScript Studio #");
                file.WriteLine("##");
                file.WriteLine();

                foreach (var vertex in meshData.Vertices)
                    file.WriteLine("v " + vertex.X + " " + vertex.Y + " " + vertex.Z);
                file.WriteLine("# " + meshData.Vertices.Count + " vertices");
                file.WriteLine();

                foreach (var normal in meshData.Normals)
                    file.WriteLine("vn " + normal.X + " " + normal.Y + " " + normal.Z);
                file.WriteLine("# " + meshData.Normals.Count + " vertex normals");
                file.WriteLine();

                //foreach (var texture in meshData.Textures)
                //    file.WriteLine("vt " + texture.X + " " + texture.Y);
                //file.WriteLine("# " + meshData.Textures.Count + " texture vertices");

                foreach (var group in meshData.Groups)
                {
                    file.WriteLine("g " + group.Name);
                    foreach (var face in group.Faces)
                    {
                        file.Write("f");
                        for (int i = 0; i < face.VertexArray.Length; ++i)
                            file.Write(" " + face.VertexArray[i] + "/" + (face.TextureArray[i] != 0 ? face.TextureArray[i].ToString() : "") + "/" + (face.NormalArray[i] != 0 ? face.NormalArray[i].ToString() : ""));
                        file.WriteLine();
                    }
                    file.WriteLine("# " + group.Faces.Count + " faces");
                    file.WriteLine();
                }
                file.Close();
                return true;
            }
            catch (System.UnauthorizedAccessException e)
            {
                throw new System.UnauthorizedAccessException(e.Message);
            }
            catch
            {
                throw new ArgumentException("Path is invalid");
            }
        }
        /// <summary>
        /// Creates an array of Sub Division Mesh from the .Obj file given by the user.
        /// Each Group within the .Obj file is represented by one SubDivsion Mesh.
        /// </summary>
        /// <param name="filePath">The file to be imported</param>
        /// <returns></returns>
        public static DSSubDivisionMesh[] ImportFromOBJ(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new System.ArgumentNullException("filePath");
            }
            filePath = DSGeometryExtension.LocateFile(filePath);
            if (!File.Exists(filePath))
            {
                throw new System.ArgumentException(string.Format(Properties.Resources.FileNotFound, filePath), "filePath");
            }
            DSMeshData result = ObjHandler.Import(filePath);

            return(result.ConvertToSubDivisionMesh());
        }
        /// <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));
        }
 /// <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);
        }