Ejemplo n.º 1
0
        /// <summary>
        /// Read the STL file
        /// </summary>
        /// <returns>mesh defined in the file</returns>
        public Mesh Read()
        {
            this._stream.Position = 0;

            string header = this._stream.ReadString(80);

            this.OnNotification?.Invoke(new NotificationArgs(header.Replace("\0", "")));

            Mesh mesh = new Mesh();
            LayerElementNormal normals = new LayerElementNormal();

            mesh.Layers.Add(normals);

            int nTriangles = this._stream.ReadInt <LittleEndianConverter>();

            if (this.checkStreamLenth(nTriangles))
            {
                for (int i = 0; i < nTriangles; i++)
                {
                    XYZ normal = new XYZ(this._stream.ReadSingle(), this._stream.ReadSingle(), this._stream.ReadSingle());

                    normals.Add(normal);

                    XYZ v1 = new XYZ(this._stream.ReadSingle(), this._stream.ReadSingle(), this._stream.ReadSingle());
                    XYZ v2 = new XYZ(this._stream.ReadSingle(), this._stream.ReadSingle(), this._stream.ReadSingle());
                    XYZ v3 = new XYZ(this._stream.ReadSingle(), this._stream.ReadSingle(), this._stream.ReadSingle());

                    mesh.AddTriangles(v1, v2, v3);

                    ushort attByteCount = this._stream.ReadUShort();
                }
            }
            else
            {
                this._stream.Position = 0;

                string line = this._stream.ReadUntil('\n');
                string name = Regex.Match(line, @"solid \s\n", options: RegexOptions.IgnoreCase).Value;
                mesh.Name = name;

                line = this._stream.ReadUntil('\n');

                while (!line.Contains($"endsolid {name}"))
                {
                    XYZ normal = this.readPoint(line, "facet normal");
                    normals.Add(normal);

                    this.checkLine(this._stream.ReadUntil('\n'), "outer loop");

                    XYZ v1 = this.readPoint(this._stream.ReadUntil('\n'), "vertex");
                    XYZ v2 = this.readPoint(this._stream.ReadUntil('\n'), "vertex");
                    XYZ v3 = this.readPoint(this._stream.ReadUntil('\n'), "vertex");

                    mesh.AddTriangles(v1, v2, v3);

                    this.checkLine(this._stream.ReadUntil('\n'), "endloop");
                    this.checkLine(this._stream.ReadUntil('\n'), "endfacet");

                    line = this._stream.ReadUntil('\n');
                }
            }

            return(mesh);
        }