/// <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); } }
internal static TessellatedSolid[] OpenModelFile(Stream s, string filename) { var now = DateTime.Now; ThreeMFFileData threeMFData = null; try { var settings = new XmlReaderSettings { IgnoreComments = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true }; using (var reader = XmlReader.Create(s, settings)) { if (reader.IsStartElement("model")) { var defaultNamespace = reader["xmlns"]; var serializer = new XmlSerializer(typeof(ThreeMFFileData), defaultNamespace); threeMFData = (ThreeMFFileData)serializer.Deserialize(reader); } threeMFData.FileName = filename; var results = new List <TessellatedSolid>(); threeMFData.Name = GetNameFromFileName(filename); var nameIndex = threeMFData.metadata.FindIndex( md => md != null && (md.type.Equals("name", StringComparison.CurrentCultureIgnoreCase) || md.type.Equals("title", StringComparison.CurrentCultureIgnoreCase))); if (nameIndex != -1) { threeMFData.Name = threeMFData.metadata[nameIndex].Value; threeMFData.metadata.RemoveAt(nameIndex); } foreach (var item in threeMFData.build.Items) { results.AddRange(threeMFData.TessellatedSolidsFromIDAndTransform(item.objectid, item.transformMatrix, threeMFData.Name + "_")); } Message.output("Successfully read in 3Dmodel file (" + (DateTime.Now - now) + ").", 3); return(results.ToArray()); } } catch (Exception exception) { Message.output("Unable to read in 3Dmodel file.", 1); Message.output("Exception: " + exception.Message, 3); return(null); } }
public static void Open(Stream s, string filename, out TessellatedSolid[] tessellatedSolids) { try { var extension = GetFileTypeFromExtension(Path.GetExtension(filename)); switch (extension) { case FileType.STL_ASCII: case FileType.STL_Binary: tessellatedSolids = STLFileData.OpenSolids(s, filename); // Standard Tessellation or StereoLithography break; case FileType.ThreeMF: tessellatedSolids = ThreeMFFileData.OpenSolids(s, filename); break; case FileType.Model3MF: tessellatedSolids = ThreeMFFileData.OpenModelFile(s, filename); break; case FileType.AMF: tessellatedSolids = AMFFileData.OpenSolids(s, filename); break; case FileType.SHELL: tessellatedSolids = ShellFileData.OpenSolids(s, filename); break; case FileType.OFF: case FileType.PLY_ASCII: case FileType.PLY_Binary: throw new Exception("Attempting to open multiple solids with a " + extension.ToString() + " file."); default: var serializer = new JsonSerializer(); var sr = new StreamReader(s); using (var reader = new JsonTextReader(sr)) // note this is a hack...<T> is overly specific tessellatedSolids = serializer.Deserialize <TessellatedSolid[]>(reader); break; } } catch (Exception exc) { throw new Exception("Cannot open file. Message: " + exc.Message); } }
/// <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="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.Count == 1) { return(Save(stream, solids[0], fileType)); } switch (fileType) { case FileType.STL_ASCII: return(STLFileData.SaveASCII(stream, solids.Cast <TessellatedSolid>().ToArray())); case FileType.STL_Binary: return(STLFileData.SaveBinary(stream, solids.Cast <TessellatedSolid>().ToArray())); case FileType.AMF: return(AMFFileData.SaveSolids(stream, solids.Cast <TessellatedSolid>().ToArray())); case FileType.ThreeMF: return(ThreeMFFileData.Save(stream, solids.Cast <TessellatedSolid>().ToArray())); case FileType.Model3MF: return(ThreeMFFileData.SaveModel(stream, solids.Cast <TessellatedSolid>().ToArray())); case FileType.OFF: throw new NotSupportedException( "The OFF format does not support saving multiple solids to a single file."); case FileType.PLY_ASCII: throw new NotSupportedException( "The PLY format does not support saving multiple solids to a single file."); case FileType.PLY_Binary: throw new NotSupportedException( "The PLY format does not support saving multiple solids to a single file."); default: solids.Select(solid => Save(stream, solid, fileType)); return(true); } }
/// <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); }
public static void Open(Stream s, string filename, out TessellatedSolid[] tessellatedSolids) { var extension = GetExtensionFromFileName(filename); switch (extension) { case "stl": tessellatedSolids = STLFileData.OpenSolids(s, filename); // 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 tessellatedSolids = ThreeMFFileData.OpenSolids(s, filename); break; #endif case "model": tessellatedSolids = ThreeMFFileData.OpenModelFile(s, filename); break; case "amf": tessellatedSolids = AMFFileData.OpenSolids(s, filename); break; case "shell": tessellatedSolids = ShellFileData.OpenSolids(s, filename); break; case "xml": tessellatedSolids = TVGLFileData.OpenSolids(s, filename).Cast <TessellatedSolid>().ToArray(); 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="solids">The solids.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> internal static bool SaveModel(Stream stream, IList <TessellatedSolid> solids) { var objects = new List <Object>(); var baseMats = new BaseMaterials { id = 1 }; for (var i = 0; i < solids.Count; i++) { var solid = solids[i]; var thisObject = new Object { name = solid.Name, id = i + 2 }; // this is "+ 2" since the id's start with 1 instead of 0 plus BaseMaterials is typically 1, so start at 2. var triangles = new List <Triangle>(); foreach (var face in solid.Faces) { var colString = (face.Color ?? solid.SolidColor ?? new Color(Constants.DefaultColor)).ToString(); var colorIndex = baseMats.bases.FindIndex(col => col.colorString.Equals(colString)); if (colorIndex == -1) { colorIndex = baseMats.bases.Count; baseMats.bases.Add(new Base { colorString = colString }); } triangles.Add(new Triangle { v1 = face.Vertices[0].IndexInList, v2 = face.Vertices[1].IndexInList, v3 = face.Vertices[2].IndexInList, pid = 1, p1 = colorIndex }); } thisObject.mesh = new Mesh { vertices = solid.Vertices.Select(v => new threemfclasses.Vertex { x = v.X, y = v.Y, z = v.Z }).ToList(), triangles = triangles }; objects.Add(thisObject); } var metaData = new List <Metadata>(); var allRawComments = solids.SelectMany(s => s.Comments); var comments = new List <string>(); foreach (var comment in allRawComments.Where(string.IsNullOrWhiteSpace)) { var arrowIndex = comment.IndexOf("==>"); if (arrowIndex == -1) { comments.Add(comment); } else { var endOfType = arrowIndex - 1; var beginOfValue = arrowIndex + 3; //todo: check this -1 and +3 metaData.Add(new Metadata { type = comment.Substring(0, endOfType), Value = comment.Substring(beginOfValue) }); } } var threeMFData = new ThreeMFFileData { Units = solids[0].Units, Name = solids[0].Name.Split('_')[0], Language = solids[0].Language, metadata = metaData, build = new Build { Items = objects.Select(o => new Item { objectid = o.id }).ToList() }, resources = new Resources { basematerials = new[] { baseMats }.ToList(), //colors = colors, materials = materials, objects = objects } }; threeMFData.Comments.AddRange(comments); try { using (var writer = XmlWriter.Create(stream)) { writer.WriteComment(tvglDateMarkText); if (!string.IsNullOrWhiteSpace(solids[0].FileName)) { writer.WriteComment("Originally loaded from " + solids[0].FileName); } var serializer = new XmlSerializer(typeof(ThreeMFFileData), defXMLNameSpaceModel); serializer.Serialize(writer, threeMFData); } Message.output("Successfully wrote 3MF file to stream.", 3); return(true); } catch (Exception exception) { Message.output("Unable to write in model file.", 1); Message.output("Exception: " + exception.Message, 3); return(false); } }
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> /// 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); }