Пример #1
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);
            }
        }
Пример #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)
        {
            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);
        }
Пример #3
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);
            }
        }
Пример #4
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);
        }