/// <summary> /// Tries the read binary. /// </summary> /// <param name="stream">The stream.</param> /// <param name="offData">The off data.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="NotImplementedException"></exception> /// <exception cref="System.NotImplementedException"></exception> /// <exception cref="System.IO.EndOfStreamException">Incomplete file</exception> private static bool TryReadBinary(Stream stream, out OFFFileData offData) { offData = null; return(false); throw new NotImplementedException(); }
/// <summary> /// Opens the specified stream, s. Note that as a Portable class library /// </summary> /// <param name="s">The s.</param> /// <param name="filename">The filename.</param> /// <param name="solid">The solid.</param> /// <returns>TessellatedSolid.</returns> /// <exception cref="Exception">Cannot open file without extension (e.g. f00.stl). /// or /// This function has been recently removed. /// or /// Cannot determine format from extension (not .stl, .ply, .3ds, .lwo, .obj, .objx, or .off.</exception> /// <exception cref="System.Exception">Cannot open file without extension (e.g. f00.stl). /// or /// This function has been recently removed. /// or /// Cannot determine format from extension (not .stl, .ply, .3ds, .lwo, .obj, .objx, or .off.</exception> public static void Open(Stream s, string filename, out TessellatedSolid solid) { try { var extension = GetFileTypeFromExtension(Path.GetExtension(filename)); switch (extension) { case FileType.STL_ASCII: case FileType.STL_Binary: solid = STLFileData.OpenSolids(s, filename)[0]; // Standard Tessellation or StereoLithography break; case FileType.ThreeMF: solid = ThreeMFFileData.OpenSolids(s, filename)[0]; break; case FileType.Model3MF: solid = ThreeMFFileData.OpenModelFile(s, filename)[0]; break; case FileType.AMF: solid = AMFFileData.OpenSolids(s, filename)[0]; break; case FileType.OFF: solid = OFFFileData.OpenSolid(s, filename); // http://en.wikipedia.org/wiki/OFF_(file_format) break; case FileType.PLY_ASCII: case FileType.PLY_Binary: solid = PLYFileData.OpenSolid(s, filename); break; case FileType.SHELL: solid = ShellFileData.OpenSolids(s, filename)[0]; break; case FileType.STEP: solid = STEPFileData.OpenSolids(s, filename)[0]; break; default: var serializer = new JsonSerializer(); var sr = new StreamReader(s); using (var reader = new JsonTextReader(sr)) solid = serializer.Deserialize <TessellatedSolid>(reader); break; } } catch (Exception exc) { throw new Exception("Cannot open file. Message: " + exc.Message); } }
/// <summary> /// Saves the specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="solid">The solid.</param> /// <param name="fileType">Type of the file.</param> /// <returns>System.Boolean.</returns> public static bool Save(Stream stream, Solid solid, FileType fileType = FileType.TVGL) { switch (fileType) { case FileType.STL_ASCII: return(STLFileData.SaveASCII(stream, new[] { (TessellatedSolid)solid })); case FileType.STL_Binary: return(STLFileData.SaveBinary(stream, new[] { (TessellatedSolid)solid })); case FileType.AMF: return(AMFFileData.SaveSolids(stream, new[] { (TessellatedSolid)solid })); case FileType.ThreeMF: return(ThreeMFFileData.Save(stream, new[] { (TessellatedSolid)solid })); case FileType.Model3MF: return(ThreeMFFileData.SaveModel(stream, new[] { (TessellatedSolid)solid })); case FileType.OFF: return(OFFFileData.SaveSolid(stream, (TessellatedSolid)solid)); case FileType.PLY_ASCII: return(PLYFileData.SaveSolidASCII(stream, (TessellatedSolid)solid)); case FileType.PLY_Binary: return(PLYFileData.SaveSolidBinary(stream, (TessellatedSolid)solid)); case FileType.SHELL: return(ShellFileData.Save(stream, (TessellatedSolid)solid)); default: JsonSerializer serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Auto, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }; var sw = new StreamWriter(stream); using (var writer = new JsonTextWriter(sw)) { var jObject = JObject.FromObject(solid, serializer); var solidType = solid.GetType(); jObject.AddFirst(new JProperty("TVGLSolidType", solid.GetType().FullName)); if (!Assembly.GetExecutingAssembly().Equals(solidType.Assembly)) { jObject.AddFirst(new JProperty("InAssembly", solidType.Assembly.Location)); } jObject.WriteTo(writer); } return(true); } }
/// <summary> /// Opens the specified stream, s. Note that as a Portable class library /// </summary> /// <param name="s">The s.</param> /// <param name="filename">The filename.</param> /// <param name="solid">The solid.</param> /// <returns>TessellatedSolid.</returns> /// <exception cref="Exception">Cannot open file without extension (e.g. f00.stl). /// or /// This function has been recently removed. /// or /// Cannot determine format from extension (not .stl, .ply, .3ds, .lwo, .obj, .objx, or .off.</exception> /// <exception cref="System.Exception">Cannot open file without extension (e.g. f00.stl). /// or /// This function has been recently removed. /// or /// Cannot determine format from extension (not .stl, .ply, .3ds, .lwo, .obj, .objx, or .off.</exception> public static void Open(Stream s, string filename, out TessellatedSolid solid) { var extension = GetExtensionFromFileName(filename); switch (extension) { case "stl": solid = STLFileData.OpenSolids(s, filename)[0]; // Standard Tessellation or StereoLithography break; case "3mf": #if net40 throw new NotSupportedException("The loading or saving of .3mf files are not supported in the .NET4.0 version of TVGL."); #else solid = ThreeMFFileData.OpenSolids(s, filename)[0]; break; #endif case "model": solid = ThreeMFFileData.OpenModelFile(s, filename)[0]; break; case "amf": solid = AMFFileData.OpenSolids(s, filename)[0]; break; case "off": solid = OFFFileData.OpenSolid(s, filename); // http://en.wikipedia.org/wiki/OFF_(file_format) break; case "ply": solid = PLYFileData.OpenSolid(s, filename); break; case "shell": solid = ShellFileData.OpenSolids(s, filename)[0]; break; case "xml": solid = (TessellatedSolid)TVGLFileData.OpenSolids(s, filename)[0]; break; default: throw new Exception( "Cannot determine format from extension (not .stl, .ply, .3ds, .lwo, .obj, .objx, or .off."); } }
/// <summary> /// Saves the specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="solid">The solid.</param> /// <param name="fileType">Type of the file.</param> /// <returns>System.Boolean.</returns> public static bool Save(Stream stream, Solid solid, FileType fileType = FileType.TVGL) { if (solid is TessellatedSolid) { var ts = (TessellatedSolid)solid; switch (fileType) { case FileType.STL_ASCII: return(STLFileData.SaveASCII(stream, new[] { ts })); case FileType.STL_Binary: return(STLFileData.SaveBinary(stream, new[] { ts })); case FileType.AMF: return(AMFFileData.SaveSolids(stream, new[] { ts })); case FileType.ThreeMF: #if net40 throw new NotSupportedException("The loading or saving of .3mf files are not allowed in the .NET4.0 version of TVGL."); #else return(ThreeMFFileData.Save(stream, new[] { ts })); #endif case FileType.Model3MF: return(ThreeMFFileData.SaveModel(stream, new[] { ts })); case FileType.OFF: return(OFFFileData.SaveSolid(stream, ts)); case FileType.PLY_ASCII: return(PLYFileData.SaveSolidASCII(stream, ts)); case FileType.PLY_Binary: return(PLYFileData.SaveSolidBinary(stream, ts)); default: return(TVGLFileData.SaveSolid(stream, ts)); } } if (solid is VoxelizedSolid) { return(TVGLFileData.SaveSolid(stream, (VoxelizedSolid)solid)); } return(false); }
/// <summary> /// Tries the read binary. /// </summary> /// <param name="stream">The stream.</param> /// <param name="offData">The off data.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="System.NotImplementedException"></exception> /// <exception cref="System.IO.EndOfStreamException">Incomplete file</exception> internal static bool TryReadBinary(Stream stream, out OFFFileData offData) { offData = null; return false; throw new NotImplementedException(); }
internal static bool TryReadAscii(Stream stream, out OFFFileData offData) { var reader = new StreamReader(stream); offData = new OFFFileData(); var line = ReadLine(reader); if (!line.Contains("off") && !line.Contains("OFF")) return false; offData.ContainsNormals = line.Contains("N"); offData.ContainsColors = line.Contains("C"); offData.ContainsTextureCoordinates = line.Contains("ST"); offData.ContainsHomogeneousCoordinates = line.Contains("4"); double[] point; if (TryParseDoubleArray(ReadLine(reader), out point)) { offData.NumVertices = (int) Math.Round(point[0], 0); offData.NumFaces = (int) Math.Round(point[1], 0); offData.NumEdges = (int) Math.Round(point[2], 0); } else return false; for (var i = 0; i < offData.NumVertices; i++) { line = ReadLine(reader); if (TryParseDoubleArray(line, out point)) { if (offData.ContainsHomogeneousCoordinates && !StarMath.IsNegligible(point[3])) offData.Vertices.Add(new[] { point[0]/point[3], point[1]/point[3], point[2]/point[3] }); else offData.Vertices.Add(point); } else return false; } for (var i = 0; i < offData.NumFaces; i++) { line = ReadLine(reader); double[] numbers; if (!TryParseDoubleArray(line, out numbers)) return false; var numVerts = (int) Math.Round(numbers[0], 0); var vertIndices = new List<int>(); for (var j = 0; j < numVerts; j++) vertIndices.Add((int) Math.Round(numbers[1 + j], 0)); offData.FaceToVertexIndices.Add(vertIndices); if (numbers.GetLength(0) == 1 + numVerts + 3) { var r = (float) numbers[1 + numVerts]; var g = (float) numbers[2 + numVerts]; var b = (float) numbers[3 + numVerts]; var currentColor = new Color(1f, r, g, b); offData.HasColorSpecified = true; if (!offData._lastColor.Equals(currentColor)) offData._lastColor = currentColor; } offData.Colors.Add(offData._lastColor); } offData.Name = getNameFromStream(stream); return true; }
public static Solid Open(Stream s, string filename = "") { try { var extension = GetFileTypeFromExtension(Path.GetExtension(filename)); switch (extension) { case FileType.STL_ASCII: case FileType.STL_Binary: return(STLFileData.OpenSolids(s, filename)[0]); // Standard Tessellation or StereoLithography case FileType.ThreeMF: return(ThreeMFFileData.OpenSolids(s, filename)[0]); case FileType.Model3MF: return(ThreeMFFileData.OpenModelFile(s, filename)[0]); case FileType.AMF: return(AMFFileData.OpenSolids(s, filename)[0]); case FileType.OFF: return(OFFFileData.OpenSolid(s, filename)); case FileType.PLY_ASCII: case FileType.PLY_Binary: return(PLYFileData.OpenSolid(s, filename)); case FileType.SHELL: return(ShellFileData.OpenSolids(s, filename)[0]); break; default: var serializer = new JsonSerializer(); var sr = new StreamReader(s); using (var reader = new JsonTextReader(sr)) { JObject jObject = JObject.Load(reader); var typeString = ((string)jObject["TVGLSolidType"]); if (string.IsNullOrWhiteSpace(typeString)) { return(null); } var type = Type.GetType(typeString); if (type == null) { var assembly = Assembly.LoadFrom((string)jObject["InAssembly"]); type = assembly.GetType(typeString); } if (type == null) { return(null); } return((Solid)JsonConvert.DeserializeObject(jObject.ToString(), type)); } } } catch (Exception exc) { throw new Exception("Cannot open file. Message: " + exc.Message); } }
/// <summary> /// Tries the read ASCII. /// </summary> /// <param name="stream">The stream.</param> /// <param name="offData">The off data.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> private static bool TryReadAscii(Stream stream, out OFFFileData offData) { var reader = new StreamReader(stream); offData = new OFFFileData(); var line = ReadLine(reader); if (!line.Contains("off") && !line.Contains("OFF")) { return(false); } offData.ContainsNormals = line.Contains("N"); offData.ContainsColors = line.Contains("C"); offData.ContainsTextureCoordinates = line.Contains("ST"); offData.ContainsHomogeneousCoordinates = line.Contains("4"); double[] point; line = ReadLine(reader); while (line.StartsWith("#")) { line.Remove(0, 1); if (!string.IsNullOrWhiteSpace(line)) { offData.Comments.Add(line.Substring(1)); } line = ReadLine(reader); } if (TryParseDoubleArray(line, out point)) { offData.NumVertices = (int)Math.Round(point[0], 0); offData.NumFaces = (int)Math.Round(point[1], 0); offData.NumEdges = (int)Math.Round(point[2], 0); } else { return(false); } for (var i = 0; i < offData.NumVertices; i++) { line = ReadLine(reader); while (line.StartsWith("#")) { line.Remove(0, 1); if (!string.IsNullOrWhiteSpace(line)) { offData.Comments.Add(line.Substring(1)); } line = ReadLine(reader); } if (TryParseDoubleArray(line, out point)) { if (offData.ContainsHomogeneousCoordinates && !point[3].IsNegligible()) { offData.Vertices.Add(new[] { point[0] / point[3], point[1] / point[3], point[2] / point[3] }); } else { offData.Vertices.Add(point); } } else { return(false); } } for (var i = 0; i < offData.NumFaces; i++) { line = ReadLine(reader); while (line.StartsWith("#")) { line.Remove(0, 1); if (!string.IsNullOrWhiteSpace(line)) { offData.Comments.Add(line.Substring(1)); } line = ReadLine(reader); } double[] numbers; if (!TryParseDoubleArray(line, out numbers)) { return(false); } var numVerts = (int)Math.Round(numbers[0], 0); var vertIndices = new int[numVerts]; for (var j = 0; j < numVerts; j++) { vertIndices[j] = (int)Math.Round(numbers[1 + j], 0); } offData.FaceToVertexIndices.Add(vertIndices); if (numbers.GetLength(0) == 1 + numVerts + 3) { var r = (float)numbers[1 + numVerts]; var g = (float)numbers[2 + numVerts]; var b = (float)numbers[3 + numVerts]; var currentColor = new Color(1f, r, g, b); offData.HasColorSpecified = true; if (offData._lastColor == null || !offData._lastColor.Equals(currentColor)) { offData._lastColor = currentColor; } } offData.Colors.Add(offData._lastColor); } return(true); }
/// <summary> /// Saves the specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="solids">The solids.</param> /// <param name="fileType">Type of the file.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public static bool Save(Stream stream, IList <Solid> solids, FileType fileType = FileType.TVGL) { if (solids.Count == 0) { return(false); } if (solids.All(s => s is TessellatedSolid)) { var tessellatedSolids = solids.Cast <TessellatedSolid>().ToArray(); switch (fileType) { case FileType.STL_ASCII: return(STLFileData.SaveASCII(stream, tessellatedSolids)); case FileType.STL_Binary: return(STLFileData.SaveBinary(stream, tessellatedSolids)); case FileType.AMF: return(AMFFileData.SaveSolids(stream, tessellatedSolids)); case FileType.ThreeMF: #if net40 throw new NotSupportedException("The loading or saving of .3mf files are not allowed in the .NET4.0 version of TVGL."); #else return(ThreeMFFileData.Save(stream, tessellatedSolids)); #endif case FileType.Model3MF: return(ThreeMFFileData.SaveModel(stream, tessellatedSolids)); case FileType.OFF: if (solids.Count > 1) { throw new NotSupportedException( "The OFF format does not support saving multiple solids to a single file."); } else { return(OFFFileData.SaveSolid(stream, tessellatedSolids[0])); } case FileType.PLY_ASCII: if (solids.Count > 1) { throw new NotSupportedException( "The PLY format does not support saving multiple solids to a single file."); } else { return(PLYFileData.SaveSolidASCII(stream, tessellatedSolids[0])); } case FileType.PLY_Binary: if (solids.Count > 1) { throw new NotSupportedException( "The PLY format does not support saving multiple solids to a single file."); } else { return(PLYFileData.SaveSolidBinary(stream, tessellatedSolids[0])); } default: if (solids.Count > 1) { return(TVGLFileData.SaveSolids(stream, tessellatedSolids)); } else { return(TVGLFileData.SaveSolid(stream, tessellatedSolids[0])); } } } if (solids.All(s => s is VoxelizedSolid)) { var tessellatedSolids = solids.Cast <VoxelizedSolid>().ToArray(); if (solids.Count > 1) { return(TVGLFileData.SaveSolids(stream, solids.Cast <VoxelizedSolid>().ToArray())); } else { return(TVGLFileData.SaveSolid(stream, (VoxelizedSolid)solids[0])); } } return(false); }