コード例 #1
0
        /// <summary>Reads the STL document contained within the <paramref name="stream"/> into a new <see cref="STLDocument"/>.</summary>
        /// <remarks>This method expects a text-based STL document to be contained within the <paramref name="reader"/>.</remarks>
        /// <param name="reader">The reader which contains the text-based STL data.</param>
        /// <returns>An <see cref="STLDocument"/> representing the data contained in the stream or null if the stream is empty.</returns>
        public static STLDocument Read(StreamReader reader)
        {
            const string regexSolid = @"solid\s+(?<Name>[^\r\n]+)?";

            if (reader == null)
            {
                return(null);
            }

            //Read the header.
            string      header       = reader.ReadLine();
            Match       headerMatch  = Regex.Match(header, regexSolid);
            STLDocument stl          = null;
            Facet       currentFacet = null;

            //Check the header.
            if (!headerMatch.Success)
            {
                throw new FormatException("Invalid STL header, expected \"solid [name]\" but found \"{0}\".".FormatString(header));
            }

            //Create the STL and extract the name (optional).
            stl = new STLDocument()
            {
                Name = headerMatch.Groups["Name"].Value
            };

            //Read each facet until the end of the stream.
            while ((currentFacet = Facet.Read(reader)) != null)
            {
                stl.Facets.Add(currentFacet);
            }

            return(stl);
        }
コード例 #2
0
        /// <summary>Reads the STL document contained within the <paramref name="reader"/> into a new <see cref="STLDocument"/>.</summary>
        /// <remarks>This method expects a text-based STL document to be contained within the <paramref name="reader"/>.</remarks>
        /// <param name="reader">The reader which contains the text-based STL data.</param>
        /// <returns>An <see cref="STLDocument"/> representing the data contained in the stream or null if the stream is empty.</returns>
        public static STLDocument Read(StreamReader reader)
        {
            if (reader == null)
            {
                throw new NullReferenceException(nameof(reader));
            }

            const string regexSolid = @"solid\s+(?<Name>[^\r\n]+)?";
            string?      header     = reader.ReadLine();
            Match        headerMatch;
            STLDocument  stl;

            // Check the header.
            if (header == null || !(headerMatch = Regex.Match(header, regexSolid)).Success)
            {
                throw new FormatException($"Invalid STL header, expected \"solid [name]\" but found: {header}");
            }

            // Create the STL and extract the name (optional).
            stl = new STLDocument()
            {
                Name = headerMatch.Groups["Name"].Value
            };

            // Read each facet until the end of the stream.
            while (!reader.EndOfStream)
            {
                // Peek the next char to make sure it's a facet (e.g. not "endsolid").
                if (((char)reader.Peek()) == 'e')
                {
                    break;
                }

                stl.Facets.Add(Facet.Read(reader));
            }

            return(stl);
        }
コード例 #3
0
        /// <summary>Reads the STL document contained within the <paramref name="stream"/> into a new <see cref="STLDocument"/>.</summary>
        /// <remarks>This method will expects a binary-based <see cref="STLDocument"/> to be contained within the <paramref name="reader"/>.</remarks>
        /// <param name="reader">The reader which contains the binary-based STL data.</param>
        /// <returns>An <see cref="STLDocument"/> representing the data contained in the stream or null if the stream is empty.</returns>
        public static STLDocument Read(BinaryReader reader)
        {
            if (reader == null)
            {
                return(null);
            }

            byte[]      buffer       = new byte[80];
            STLDocument stl          = new STLDocument();
            Facet       currentFacet = null;

            //Read (and ignore) the header and number of triangles.
            buffer = reader.ReadBytes(80);
            reader.ReadBytes(4);

            //Read each facet until the end of the stream. Stop when the end of the stream is reached.
            while ((reader.BaseStream.Position != reader.BaseStream.Length) && (currentFacet = Facet.Read(reader)) != null)
            {
                stl.Facets.Add(currentFacet);
            }

            return(stl);
        }