public SlicerTests()
        {
            bool _failed;

            do
            {
                _failed = false;
                try
                {
                    using (var stlStream = new FileStream(_testPyramidPath, FileMode.Open))
                    {
                        var stl = STLDocument.Read(stlStream, true);
                        if (stl == null)
                        {
                            throw new FileNotFoundException($"File not found: {_testPyramidPath}");
                        }

                        _slicer = new SLASlicer(stl);
                    }
                }
                catch (IOException)
                {
                    _failed = true;
                }
            } while (_failed);
        }
        public void CopyAsBinary()
        {
            STLDocument stlStringFrom;
            STLDocument stlStringTo;
            STLDocument stlBinaryFrom;
            STLDocument stlBinaryTo;

            using (var inStream = GetData("ASCII.stl"))
                using (var outStream = new MemoryStream())
                {
                    stlStringFrom = STLDocument.Read(inStream);
                    stlStringTo   = STLDocument.CopyAsBinary(inStream, outStream);
                }

            Assert.NotNull(stlStringFrom);
            Assert.NotNull(stlStringTo);
            Assert.Equal(stlStringFrom, stlStringTo);

            using (var inStream = GetData("Binary.stl"))
                using (var outStream = new MemoryStream())
                {
                    stlBinaryFrom = STLDocument.Read(inStream);
                    stlBinaryTo   = STLDocument.CopyAsBinary(inStream, outStream);
                }

            Assert.NotNull(stlBinaryFrom);
            Assert.NotNull(stlBinaryTo);
            Assert.Equal(stlBinaryFrom, stlBinaryTo);
        }
示例#3
0
        public void WriteBinary()
        {
            STLDocument stl1 = new STLDocument("WriteBinary", new List <Facet>()
            {
                new Facet(new Normal(0, 0, 1), new List <Vertex>()
                {
                    new Vertex(0, 0, 0),
                    new Vertex(-10, -10, 0),
                    new Vertex(-10, 0, 0)
                }, 0)
            });
            STLDocument stl2 = null;

            byte[] stl1Data = null;
            byte[] stl2Data = null;

            using (MemoryStream stream = new MemoryStream())
            {
                stl1.WriteBinary(stream);
                stl1Data = stream.ToArray();
            }

            using (MemoryStream stream = new MemoryStream(stl1Data))
            {
                stl2     = STLDocument.Read(stream);
                stl2Data = stream.ToArray();
            }

            Assert.IsTrue(stl1.Equals(stl2));
            Assert.IsTrue(stl1Data.SequenceEqual(stl2Data));
        }
示例#4
0
        public void CopyAsBinary()
        {
            STLDocument stlStringFrom = null;
            STLDocument stlStringTo   = null;
            STLDocument stlBinaryFrom = null;
            STLDocument stlBinaryTo   = null;

            using (Stream inStream = GetData("ASCII.stl"), outStream = new MemoryStream())
            {
                stlStringFrom = STLDocument.Read(inStream);
                stlStringTo   = STLDocument.CopyAsBinary(inStream, outStream);
            }

            Assert.IsNotNull(stlStringFrom);
            Assert.IsNotNull(stlStringTo);
            Assert.IsTrue(stlStringFrom.Equals(stlStringTo));

            using (Stream inStream = GetData("Binary.stl"), outStream = new MemoryStream())
            {
                stlBinaryFrom = STLDocument.Read(inStream);
                stlBinaryTo   = STLDocument.CopyAsBinary(inStream, outStream);
            }

            Assert.IsNotNull(stlBinaryFrom);
            Assert.IsNotNull(stlBinaryTo);
            Assert.IsTrue(stlBinaryFrom.Equals(stlBinaryTo));
        }
        public void WriteBinary__ToStream__Then__Read__FromStream__ProducesSameDocument()
        {
            STLDocument stl1 = new STLDocument("WriteBinary", new List <Facet>()
            {
                new Facet(new Normal(0, 0, 1), new List <Vertex>()
                {
                    new Vertex(0, 0, 0),
                    new Vertex(-10, -10, 0),
                    new Vertex(-10, 0, 0)
                }, 0)
            });
            STLDocument stl2;

            byte[] stl1Data;
            byte[] stl2Data;

            using (var stream = new MemoryStream())
            {
                stl1.WriteBinary(stream);
                stl1Data = stream.ToArray();
            }

            using (var stream = new MemoryStream(stl1Data))
            {
                stl2     = STLDocument.Read(stream);
                stl2Data = stream.ToArray();
            }

            CompareSTLs(stl1, stl2, true);
            Assert.True(stl1Data.SequenceEqual(stl2Data));
        }
示例#6
0
        public void SaveToFile()
        {
            STLDocument stl           = null;
            STLDocument stlText       = null;
            STLDocument stlBinary     = null;
            string      stlTextPath   = Path.GetTempFileName();
            string      stlBinaryPath = Path.GetTempFileName();

            using (Stream stream = GetData("ASCII.stl"))
                stl = STLDocument.Read(stream);

            stl.SaveAsText(stlTextPath);
            stlText = STLDocument.Open(stlTextPath);
            stl.SaveAsBinary(stlBinaryPath);
            stlBinary = STLDocument.Open(stlBinaryPath);

            ValidateSTL(stlText);
            ValidateSTL(stlBinary);

            try { File.Delete(stlTextPath); }
            catch { }

            try { File.Delete(stlBinaryPath); }
            catch { }
        }
示例#7
0
        public void WriteString()
        {
            STLDocument stl1 = new STLDocument("WriteString", new List <Facet>()
            {
                new Facet(new Normal(0.23f, 0, 1), new List <Vertex>()
                {
                    new Vertex(0, 0, 0),
                    new Vertex(-10.123f, -10, 0),
                    new Vertex(-10.123f, 0, 0)
                }, 0)
            });
            STLDocument stl2 = null;

            byte[] stl1Data   = null;
            string stl1String = null;

            byte[] stl2Data   = null;
            string stl2String = null;

            using (MemoryStream stream = new MemoryStream()) {
                stl1.WriteText(stream);
                stl1Data   = stream.ToArray();
                stl1String = Encoding.ASCII.GetString(stl1Data);
            }

            using (MemoryStream stream = new MemoryStream(stl1Data)) {
                stl2       = STLDocument.Read(stream);
                stl2Data   = stream.ToArray();
                stl2String = Encoding.ASCII.GetString(stl2Data);
            }

            Assert.IsTrue(stl1.Equals(stl2));
            Assert.AreEqual(stl1String, stl2String);
        }
        public void Read__FromBinaryStream()
        {
            STLDocument stlBinary;

            using (var stream = GetData("Binary.stl"))
            {
                stlBinary = STLDocument.Read(stream);
            }

            ValidateSTL(stlBinary);
        }
        public void Read__FromTextStream()
        {
            STLDocument stlString;

            using (var stream = GetData("ASCII.stl"))
            {
                stlString = STLDocument.Read(stream);
            }

            ValidateSTL(stlString);
        }
示例#10
0
 public SLASlicer(string path)
 {
     using (var stream = new FileStream(path, FileMode.Open))
     {
         _stl = STLDocument.Read(stream, true);
         if (_stl == null)
         {
             throw new FileNotFoundException($"File not found: {path}");
         }
     }
 }
示例#11
0
        public void FromText()
        {
            STLDocument stl = null;

            using (Stream stream = GetData("ASCII.stl")) {
                using (StreamReader reader = new StreamReader(stream, Encoding.ASCII, true, 1024, true)) {
                    stl = STLDocument.Read(reader);
                }
            }

            ValidateSTL(stl);
        }
示例#12
0
        public void FromString()
        {
            string      stlText = null;
            STLDocument stl     = null;

            using (Stream stream = GetData("ASCII.stl"))
                using (StreamReader reader = new StreamReader(stream))
                    stlText = reader.ReadToEnd();

            stl = STLDocument.Read(stlText);

            ValidateSTL(stl);
        }
示例#13
0
        public void StreamLeftOpen()
        {
            using (Stream stream = GetData("ASCII.stl")) {
                STLDocument.Read(stream);

                try {
                    stream.ReadByte();
                }
                catch (ObjectDisposedException) {
                    Assert.Fail("Stream is closed.");
                }
            }
        }
示例#14
0
        public void FromBinary()
        {
            STLDocument stl = null;

            using (Stream stream = GetData("Binary.stl")) {
                using (BinaryReader reader = new BinaryReader(stream)) {
                    {
                        stl = STLDocument.Read(reader);
                    }
                }
            }

            ValidateSTL(stl);
        }
示例#15
0
        public void Read__FromString()
        {
            string      stlText;
            STLDocument stl;

            using (var stream = GetData("ASCII.stl"))
                using (var reader = new StreamReader(stream))
                {
                    stlText = reader.ReadToEnd();
                }

            stl = STLDocument.Read(stlText);

            ValidateSTL(stl);
        }
示例#16
0
        public void Equality()
        {
            STLDocument[] stls = new STLDocument[2];

            for (int i = 0; i < stls.Length; i++)
            {
                using (Stream stream = GetData("ASCII.stl")) {
                    using (StreamReader reader = new StreamReader(stream)) {
                        stls[i] = STLDocument.Read(reader);
                    }
                }
            }

            Assert.IsTrue(stls[0].Equals(stls[1]));
        }
示例#17
0
        public void Read__FromBinaryReader()
        {
            STLDocument stl;

            using (var stream = GetData("Binary.stl"))
            {
                using (var reader = new BinaryReader(stream))
                {
                    {
                        stl = STLDocument.Read(reader);
                    }
                }
            }

            ValidateSTL(stl);
        }
示例#18
0
        public void Equalz()
        {
            var stls = new STLDocument[2];

            for (int i = 0; i < stls.Length; i++)
            {
                using (var stream = GetData("ASCII.stl"))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        stls[i] = STLDocument.Read(reader);
                    }
                }
            }

            Assert.Equal(stls[0], stls[1]);
        }
示例#19
0
        public void FromStringAndBinary()
        {
            STLDocument stlString = null;
            STLDocument stlBinary = null;

            using (Stream stream = GetData("ASCII.stl")) {
                stlString = STLDocument.Read(stream);
            }

            ValidateSTL(stlString);

            using (Stream stream = GetData("Binary.stl")) {
                stlBinary = STLDocument.Read(stream);
            }

            ValidateSTL(stlBinary);
        }
示例#20
0
        public void Read__FromStream__LeavesStreamOpen()
        {
            STLDocument stl;

            using (var stream = GetData("ASCII.stl"))
            {
                stl = STLDocument.Read(stream);

                try
                {
                    stream.ReadByte();
                }
                catch (ObjectDisposedException)
                {
                    throw new Exception("Stream is closed.");
                }
            }
        }
示例#21
0
        public void SaveAsBinary()
        {
            STLDocument stl;
            STLDocument stlBinary;
            string      stlBinaryPath = Path.GetTempFileName();

            using (var stream = GetData("ASCII.stl"))
            {
                stl = STLDocument.Read(stream);
            }

            stl.SaveAsBinary(stlBinaryPath);
            stlBinary = STLDocument.Open(stlBinaryPath);

            ValidateSTL(stlBinary);

            try { File.Delete(stlBinaryPath); }
            catch { }
        }
示例#22
0
        public void AppendFacets()
        {
            STLDocument stl1       = null;
            STLDocument stl2       = null;
            int         facetCount = 0;

            using (Stream stream = GetData("ASCII.stl")) {
                stl1 = STLDocument.Read(stream);
                stl2 = STLDocument.Read(stream);
            }

            ValidateSTL(stl1);
            ValidateSTL(stl2);

            facetCount = (stl1.Facets.Count + stl2.Facets.Count);
            stl1.AppendFacets(stl2);

            ValidateSTL(stl1, facetCount);
        }
示例#23
0
        public static void Load(string filepath, TriangleMesh triMesh, float scale)
        {
            STLDocument stl = null;

            using (Stream filestream = File.OpenRead(filepath))
            {
                stl = STLDocument.Read(filestream);
            }

            foreach (var facet in stl.Facets)
            {
                List <Vector3> vertices = new List <Vector3>();
                foreach (var vertex in facet.Vertices)
                {
                    Vector3 v = new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z);
                    vertices.Add(v * scale);
                }
                triMesh.AddTriangle(new Triangle(vertices[0], vertices[1], vertices[2]));
            }
            triMesh.Clean();
        }
示例#24
0
        public void InvertFacets()
        {
            STLDocument stl1 = null;
            STLDocument stl2 = null;

            using (Stream stream = GetData("ASCII.stl")) {
                stl1 = STLDocument.Read(stream);
                stl2 = STLDocument.Read(stream);
            }

            stl2.Facets.Invert();

            for (int f = 0; f < stl1.Facets.Count; f++)
            {
                for (int v = 0; v < stl1.Facets[f].Vertices.Count; v++)
                {
                    Assert.AreEqual(stl1.Facets[f].Normal.X, (stl2.Facets[f].Normal.X * -1));
                    Assert.AreEqual(stl1.Facets[f].Normal.Y, (stl2.Facets[f].Normal.Y * -1));
                    Assert.AreEqual(stl1.Facets[f].Normal.Z, (stl2.Facets[f].Normal.Z * -1));
                }
            }
        }
示例#25
0
        public void ImportSTL(Document doc)
        {
            String assemblyPath = this.GetType().Assembly.Location;
            String stlPath      = Path.Combine(assemblyPath, "STL files");
            String filename     = SelectSTLFile(stlPath);

            if (filename == null)
            {
                return;
            }

            String documentName = Path.GetFileName(filename);

            // Read from file

            if (StlImportProperties.GetProperties().Binary)
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filename, FileMode.Open)))
                {
                    STLDocument document = STLDocument.Read(reader);

                    ImportSTLDocument(document, doc, documentName);

                    reader.Close();
                }
            }
            else
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    STLDocument document = STLDocument.Read(reader);

                    ImportSTLDocument(document, doc, documentName);

                    reader.Close();
                }
            }
        }
示例#26
0
        public void ShiftFacets()
        {
            STLDocument stl1  = null;
            STLDocument stl2  = null;
            Vertex      shift = new Vertex(100, -100, 50);

            using (Stream stream = GetData("ASCII.stl")) {
                stl1 = STLDocument.Read(stream);
                stl2 = STLDocument.Read(stream);
            }

            stl2.Facets.Shift(shift);

            for (int f = 0; f < stl1.Facets.Count; f++)
            {
                for (int v = 0; v < stl1.Facets[f].Vertices.Count; v++)
                {
                    Assert.AreEqual(stl1.Facets[f].Vertices[v].X, stl2.Facets[f].Vertices[v].X - shift.X);
                    Assert.AreEqual(stl1.Facets[f].Vertices[v].Y, stl2.Facets[f].Vertices[v].Y - shift.Y);
                    Assert.AreEqual(stl1.Facets[f].Vertices[v].Z, stl2.Facets[f].Vertices[v].Z - shift.Z);
                }
            }
        }
示例#27
0
        public void WriteText__ToStream__Then__Read__FromStream__ProducesSameDocument()
        {
            STLDocument stl1 = new STLDocument("WriteString", new List <Facet>()
            {
                new Facet(new Normal(0.23f, 0, 1), new List <Vertex>()
                {
                    new Vertex(0, 0, 0),
                    new Vertex(-10.123f, -10, 0),
                    new Vertex(-10.123f, 0, 0)
                }, 0)
            });
            STLDocument stl2;

            byte[] stl1Data;
            string stl1String;

            byte[] stl2Data;
            string stl2String;

            using (var stream = new MemoryStream())
            {
                stl1.WriteText(stream);
                stl1Data   = stream.ToArray();
                stl1String = Encoding.ASCII.GetString(stl1Data);
            }

            using (var stream = new MemoryStream(stl1Data))
            {
                stl2       = STLDocument.Read(stream);
                stl2Data   = stream.ToArray();
                stl2String = Encoding.ASCII.GetString(stl2Data);
            }

            CompareSTLs(stl1, stl2);
            Assert.Equal(stl1String, stl2String);
        }
示例#28
0
        private void DisplayFile(string filename)
        {
            STLDocument doc = STLDocument.Read(new FileStream(filename, FileMode.Open));

            MeshGeometry3D mg = new MeshGeometry3D();

            foreach (var facet in doc.Facets)
            {
                for (int i = 0; i < facet.Vertices.Count; i++)
                {
                    mg.Positions.Add(new Point3D(facet.Vertices[i].X, facet.Vertices[i].Y, facet.Vertices[i].Z));
                    mg.Normals.Add(new Vector3D(facet.Normal.X, facet.Normal.Y, facet.Normal.Z));
                }
            }

            ModelVisual3D m3d = new ModelVisual3D();

            m3d.Content = new GeometryModel3D(mg, new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(175, 175, 175))))
            {
                BackMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 0, 0)))
            };

            HelixViewer.Children.Add(m3d);
        }
        private void LoadSTL()
        {
            bool _failed;

            do
            {
                _failed = false;
                try
                {
                    using (var _stlStream = new FileStream(_testPyramidPath, FileMode.Open))
                    {
                        _stl = STLDocument.Read(_stlStream, true);
                        if (_stl == null)
                        {
                            throw new FileNotFoundException($"File not found: {_testPyramidPath}");
                        }
                    }
                }
                catch (IOException)
                {
                    _failed = true;
                }
            } while (_failed);
        }
示例#30
0
        public PrintModelFile(string file, FileType fileType)
        {
            // TODO: Possibly implement support for more formats

            STLDocument stl;

            try
            {
                Stream stream = File.OpenRead(file);
                stl = STLDocument.Read(stream, true);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (stl.Facets.Count == 0)
            {
                throw new Exception("STL file parsing failed.");
            }

            float maxX = float.MinValue, minX = float.MaxValue, maxY = float.MinValue, minY = float.MaxValue, maxZ = float.MinValue, minZ = float.MaxValue;
            bool  first = true;

            foreach (Facet facet in stl.Facets)
            {
                foreach (Vertex v in facet.Vertices)
                {
                    if (v.X > maxX)
                    {
                        maxX = v.X;
                    }
                    if (v.X < minX)
                    {
                        minX = v.X;
                    }
                    if (v.Y > maxY)
                    {
                        maxY = v.Y;
                    }
                    if (v.Y < minY)
                    {
                        minY = v.Y;
                    }
                    if (v.Z > maxZ)
                    {
                        maxZ = v.Z;
                    }
                    if (v.Z < minZ)
                    {
                        minZ = v.Z;
                    }
                    if (first)
                    {
                        first = false;
                    }
                }
            }

            this.X = Math.Round(maxX - minX, 2);
            this.Y = Math.Round(maxY - minY, 2);
            this.Z = Math.Round(maxZ - minZ, 2);

            // Calculate volume
            int    f = stl.Facets.Count;
            double p = 0;

            foreach (Facet facet in stl.Facets)
            {
                double vAx = facet.Vertices[0].X;
                double vAy = facet.Vertices[0].Y;
                double vAz = facet.Vertices[0].Z;
                double vBx = facet.Vertices[1].X;
                double vBy = facet.Vertices[1].Y;
                double vBz = facet.Vertices[1].Z;
                double vCx = facet.Vertices[2].X;
                double vCy = facet.Vertices[2].Y;
                double vCz = facet.Vertices[2].Z;

                p += (-1 * vCx * vBy * vAz) + (vBx * vCy * vAz) + (vCx * vAy * vBz) - (vAx * vCy * vBz) - (vBx * vAy * vCz) + (vAx * vBy * vCz);
            }

            this.Volume = Math.Abs(p / 6);
        }