/// <summary>
        /// solid
        ///  facet normal -1.000000 -0.000000 -0.000000
        /// outer loop
        /// vertex -12.000000 -12.000000 0.000000
        /// vertex -12.000000 -12.000000 24.000000
        /// vertex -12.000000 12.000000 0.000000
        /// endloop
        /// endfacet

        /// </summary>
        /// <param name="file"></param>
        private void LoadText(string file)
        {
            string text = System.IO.File.ReadAllText(file);
            int    lastP = 0, p, pend, normal, outer, vertex, vertex2;

            while ((p = text.IndexOf("facet", lastP)) > 0)
            {
                pend   = text.IndexOf("endfacet", p + 5);
                normal = text.IndexOf("normal", p) + 6;
                outer  = text.IndexOf("outer loop", normal);
                STLTriangle tri = new STLTriangle();
                tri.normal = extractVector(text.Substring(normal, outer - normal));
                tri.normal = Vector3.Normalize(tri.normal);
                outer     += 10;
                vertex     = text.IndexOf("vertex", outer) + 6;
                vertex2    = text.IndexOf("vertex", vertex);
                tri.p1     = extractVector(text.Substring(vertex, vertex2 - vertex));
                vertex2   += 7;
                vertex     = text.IndexOf("vertex", vertex2);
                tri.p2     = extractVector(text.Substring(vertex2, vertex - vertex2));
                vertex    += 7;
                vertex2    = text.IndexOf("endloop", vertex);
                tri.p3     = extractVector(text.Substring(vertex, vertex2 - vertex));
                lastP      = pend + 8;
                if (AssertVector3NotNaN(tri.normal) && AssertVector3NotNaN(tri.p1) && AssertVector3NotNaN(tri.p2) && AssertVector3NotNaN(tri.p3))
                {
                    if (AssertMinDistance(tri.p1, tri.p2) && AssertMinDistance(tri.p1, tri.p3) && AssertMinDistance(tri.p2, tri.p3))
                    {
                        list.AddLast(tri);
                    }
                }
            }
        }
        public void Load(string file)
        {
            list     = new LinkedList <STLTriangle>();
            filename = file;
            try
            {
                DateTime lastModified2 = File.GetLastWriteTime(filename);
                lastModified = lastModified2.Ticks;
                FileStream f      = File.OpenRead(file);
                byte[]     header = new byte[80];
                ReadArray(f, header);

                /*   if (header[0] == 's' && header[1] == 'o' && header[2] == 'l' && header[3] == 'i' && header[4] == 'd')
                 * {
                 *     f.Close();
                 *     LoadText(file);
                 * }
                 * else
                 * {*/
                BinaryReader r    = new BinaryReader(f);
                int          nTri = r.ReadInt32();
                if (f.Length != 84 + nTri * 50)
                {
                    f.Close();
                    LoadText(file);
                }
                else
                {
                    for (int i = 0; i < nTri; i++)
                    {
                        STLTriangle tri = new STLTriangle();
                        tri.normal = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
                        tri.p1     = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
                        tri.p2     = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
                        tri.p3     = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
                        tri.normal = Vector3.Normalize(tri.normal);
                        list.AddLast(tri);
                        r.ReadUInt16();
                    }
                    r.Close();
                    f.Close();
                }
                //}
                FileInfo info = new FileInfo(file);
                name = info.Name;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error reading STL file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #3
0
        ///// solid
        /////  facet normal -1.000000 -0.000000 -0.000000
        ///// outer loop
        ///// vertex -12.000000 -12.000000 0.000000
        ///// vertex -12.000000 -12.000000 24.000000
        ///// vertex -12.000000 12.000000 0.000000
        ///// endloop
        ///// endfacet

        /// <summary>
        /// Reads a plain text .stl file and saves it into the programs data structure of a list of STLTriangles.
        /// </summary>
        /// <param name="file"></param>
        private void LoadText(string file)
        {
            string text = System.IO.File.ReadAllText(file);
            int    lastP = 0, p, pend, normal, outer, vertex, vertex2;

            // While there are more .stl triangles to read from the file.
            while ((p = text.IndexOf("facet", lastP)) > 0)
            {
                pend   = text.IndexOf("endfacet", p + 5);
                normal = text.IndexOf("normal", p) + 6;
                outer  = text.IndexOf("outer loop", normal);

                // Make a new .stl triangle
                STLTriangle tri = new STLTriangle();
                tri.normal = extractVector(text.Substring(normal, outer - normal));
                tri.normal = Vector3.Normalize(tri.normal);
                outer     += 10;
                vertex     = text.IndexOf("vertex", outer) + 6;
                vertex2    = text.IndexOf("vertex", vertex);
                tri.p1     = extractVector(text.Substring(vertex, vertex2 - vertex));
                vertex2   += 7;
                vertex     = text.IndexOf("vertex", vertex2);
                tri.p2     = extractVector(text.Substring(vertex2, vertex - vertex2));
                vertex    += 7;
                vertex2    = text.IndexOf("endloop", vertex);
                tri.p3     = extractVector(text.Substring(vertex, vertex2 - vertex));
                lastP      = pend + 8;

                // Check to make sure the extracted .stl triangle is a valid size.
                if (AssertVector3NotNaN(tri.normal) && AssertVector3NotNaN(tri.p1) && AssertVector3NotNaN(tri.p2) && AssertVector3NotNaN(tri.p3))
                {
                    if (AssertMinDistance(tri.p1, tri.p2) && AssertMinDistance(tri.p1, tri.p3) && AssertMinDistance(tri.p2, tri.p3))
                    {
                        // If all ok with the .stl triangle then add it to the list.
                        list.AddLast(tri);
                    }
                }
            }
        }