示例#1
0
        /// <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);
            }
        }
示例#2
0
        /// <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);
            }
        }
示例#3
0
        /// <summary>
        /// Tries the read binary.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="filename">The filename.</param>
        /// <param name="stlData">The STL data.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="System.IO.EndOfStreamException">Incomplete file</exception>
        /// <exception cref="EndOfStreamException">Incomplete file</exception>
        internal static bool TryReadBinary(Stream stream, string filename, out List <STLFileData> stlData)
        {
            var length = stream.Length;

            stlData = new List <STLFileData>();
            var stlSolid1 = new STLFileData();

            if (length < 84)
            {
                throw new EndOfStreamException("Incomplete file");
            }
            var decoder = new UTF8Encoding();

            var reader   = new BinaryReader(stream);
            var comments = decoder.GetString(reader.ReadBytes(80), 0, 80).Trim(' ');

            stlSolid1.Comments.Add(comments);
            stlSolid1.Name     = comments.Split(' ')[0];
            stlSolid1.Units    = InferUnitsFromComments(stlSolid1.Comments);
            stlSolid1.FileName = filename;
            if (string.IsNullOrWhiteSpace(stlSolid1.Name))
            {
                stlSolid1.Name = Path.GetFileNameWithoutExtension(filename);
            }
            do
            {
                var numFaces = ReadNumberAsInt(reader, typeof(uint), FormatEndiannessType.binary_little_endian);
                if (length - 84 != numFaces * 50)
                {
                    return(false);
                }
                for (var i = 0; i < numFaces; i++)
                {
                    stlSolid1.ReadFacet(reader);
                }
                stlData.Add(stlSolid1);
                var last = stlSolid1;
                stlSolid1 = new STLFileData
                {
                    Name     = last.Name,
                    Units    = last.Units,
                    FileName = last.FileName
                };
                stlSolid1.Comments.Add(comments);
            } while (reader.BaseStream.Position < length);
            return(true);
        }
示例#4
0
        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);
            }
        }
示例#5
0
        /// <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.");
            }
        }
示例#6
0
        /// <summary>
        /// Reads the model in ASCII format from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="filename">The filename.</param>
        /// <param name="stlData">The STL data.</param>
        /// <returns>True if the model was loaded successfully.</returns>
        internal static bool TryReadAscii(Stream stream, string filename, out List <STLFileData> stlData)
        {
            var defaultName = Path.GetFileNameWithoutExtension(filename) + "_";
            var solidNum    = 0;
            var reader      = new StreamReader(stream);

            stlData = new List <STLFileData>();
            var stlSolid = new STLFileData {
                FileName = filename, Units = UnitType.unspecified
            };
            var comments = new List <string>();

            while (!reader.EndOfStream)
            {
                var line = ReadLine(reader);
                comments.Add(line);
                string id, values;
                ParseLine(line, out id, out values);
                switch (id)
                {
                case "#":
                    stlSolid.Comments.Add(values);
                    break;

                case "solid":
                    if (string.IsNullOrWhiteSpace(values))
                    {
                        stlSolid.Name = defaultName + ++solidNum;
                    }
                    stlSolid.Name = values.Trim(' ');
                    break;

                case "facet":
                    stlSolid.ReadFacet(reader, values);
                    break;

                case "endsolid":
                    stlSolid.Units = InferUnitsFromComments(comments);
                    stlData.Add(stlSolid);
                    stlSolid = new STLFileData();
                    break;
                }
            }
            return(true);
        }
示例#7
0
        /// <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);
            }
        }
示例#8
0
        /// <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);
        }
示例#9
0
        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.");
            }
        }
示例#10
0
        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);
            }
        }
示例#11
0
        /// <summary>
        /// Tries the read binary.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="stlData">The STL data.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="System.IO.EndOfStreamException">Incomplete file</exception>
        internal static bool TryReadBinary(Stream stream, out List<STLFileData> stlData)
        {
            var length = stream.Length;
            stlData = null;
            var stlSolid1 = new STLFileData();
            if (length < 84)
            {
                throw new EndOfStreamException("Incomplete file");
            }
            var decoder = new System.Text.UTF8Encoding();

            var reader = new BinaryReader(stream);
            stlSolid1.Name = decoder.GetString(reader.ReadBytes(80), 0, 80).Trim(' ');
            stlSolid1.Name = stlSolid1.Name.Replace("solid", "").Trim(' ');
            if (string.IsNullOrWhiteSpace(stlSolid1.Name))
                stlSolid1.Name = getNameFromStream(stream);
            var numberTriangles = ReadUInt32(reader);

            if (length - 84 != numberTriangles * 50)
            {
                return false;
            }

            //this.Meshes.Add(new MeshBuilder(true, true));
            //this.Materials.Add(this.DefaultMaterial);

            for (var i = 0; i < numberTriangles; i++)
            {
                stlSolid1.ReadTriangle(reader);
            }
            stlData = new List<STLFileData>(new[] { stlSolid1 });
            return true;
        }
示例#12
0
 /// <summary>
 /// Reads the model in ASCII format from the specified stream.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="stlData">The STL data.</param>
 /// <returns>True if the model was loaded successfully.</returns>
 internal static bool TryReadAscii(Stream stream, out List<STLFileData> stlData)
 {
     var defaultName = getNameFromStream(stream)+"_";
     var solidNum = 0;
     var reader = new StreamReader(stream);
     stlData = new List<STLFileData>();
     var stlSolid = new STLFileData();
     while (!reader.EndOfStream)
     {
         var line = ReadLine(reader);
         string id, values;
         ParseLine(line, out id, out values);
         switch (id)
         {
             case "solid":
                 if (string.IsNullOrWhiteSpace(values))
                     stlSolid.Name = defaultName + (++solidNum);
                 stlSolid.Name = values.Trim(' ');
                 break;
             case "facet":
                 stlSolid.ReadFacet(reader, values);
                 break;
             case "endsolid":
                 stlData.Add(stlSolid);
                 stlSolid=new STLFileData();
                 break;
         }
     }
     return true;
 }
示例#13
0
        /// <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);
        }