// ----------------------------------------------------------------------- // // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/ // // ----------------------------------------------------------------------- namespace TriangleNet.IO { using System; using System.Collections.Generic; using TriangleNet.Geometry; using TriangleNet.Meshing; public static class FileProcessor { static List formats; static FileProcessor() { formats = new List(); // Add Triangle file format as default. formats.Add(new TriangleFormat()); } public static void Add(IFileFormat format) { formats.Add(format); } public static bool IsSupported(string file) { foreach (var format in formats) { if (format.IsSupported(file)) { return true; } } return false; } #region Polygon read/write /// /// Read a file containing polygon geometry. /// /// The path of the file to read. /// An instance of the class. public static IPolygon Read(string filename) { foreach (IPolygonFormat format in formats) { if (format != null && format.IsSupported(filename)) { return format.Read(filename); } } throw new Exception("File format not supported."); } /// /// Save a polygon geometry to disk. /// /// An instance of the class. /// The path of the file to save. public static void Write(IPolygon polygon, string filename) { foreach (IPolygonFormat format in formats) { if (format != null && format.IsSupported(filename)) { format.Write(polygon, filename); return; } } throw new Exception("File format not supported."); } #endregion #region Mesh read/write /// /// Read a file containing a mesh. /// /// The path of the file to read. /// An instance of the interface. public static IMesh Import(string filename) { foreach (IMeshFormat format in formats) { if (format != null && format.IsSupported(filename)) { return format.Import(filename); } } throw new Exception("File format not supported."); } /// /// Save a mesh to disk. /// /// An instance of the interface. /// The path of the file to save. public static void Write(IMesh mesh, string filename) { foreach (IMeshFormat format in formats) { if (format != null && format.IsSupported(filename)) { format.Write(mesh, filename); return; } } throw new Exception("File format not supported."); } #endregion } }