Пример #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
        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);
            }
        }
Пример #4
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.");
            }
        }
Пример #5
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.");
            }
        }
Пример #6
0
        internal static TessellatedSolid[] OpenSolids(Stream s, string filename)
        {
            var now = DateTime.Now;

            try
            {
                var reader     = new StreamReader(s);
                var shellData  = new List <ShellFileData>();
                var shellSolid = new ShellFileData();
                ParseLine(ReadLine(reader), out _, out var unitstring);
                TryParseUnits(unitstring, out var unit);
                while (!reader.EndOfStream)
                {
                    var line = ReadLine(reader);
                    switch (line)
                    {
                    case "shell":
                        inObject = true;
                        line     = ReadLine(reader);
                        shellSolid.ReadMaterial(line);
                        break;

                    case "vertices":
                        startofVertices = true;
                        line            = ReadLine(reader);
                        break;

                    case "endvertices":
                        endofVertices = true;
                        break;

                    case "endshell":
                        inObject = false;
                        shellData.Add(shellSolid);
                        shellSolid = new ShellFileData();
                        inObject   = startofFacets = startofVertices = endofFacets = endofVertices = false;
                        break;

                    case "facets":
                        startofFacets = true;
                        line          = ReadLine(reader);
                        break;

                    case "endfacets":
                        endofFacets = true;
                        break;
                    }
                    if (inObject && startofVertices && !endofVertices)
                    //read and collect vertices coordinates while we're in the vertices loop of the current object/shell
                    {
                        shellSolid.ReadVertices(line);
                    }
                    if (inObject && startofFacets && !endofFacets)
                    //read and collect trianges numbers while we're in the vertices loop of the current object/shell
                    {
                        shellSolid.ReadFaces(line);
                    }
                }
                var results = new TessellatedSolid[shellData.Count];
                for (int i = 0; i < shellData.Count; i++)
                {
                    var shell = shellData[i];
                    if (shell.Vertices.Any() && shell.FaceToVertexIndices.Any())
                    {
                        results[i] = new TessellatedSolid(shell.Vertices, shell.FaceToVertexIndices, true,
                                                          shell.Colors, unit, shell.Name + "_" + shell.Material.materialName,
                                                          filename, shell.Comments, shell.Language);
                    }
                }

                Message.output(
                    "Successfully read in SHELL file called " + filename + " in " + (DateTime.Now - now).TotalSeconds +
                    " seconds.", 4);
                return(results);
            }
            catch
            {
                Message.output("Unable to read in SHELL file.", 1);
                return(null);
            }
        }
Пример #7
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);
            }
        }