private void CompareSTLsLeftToRight(STLDocument left, STLDocument right, bool isBinary = false)
        {
            if (!isBinary)
            {
                right.Name.Should().Be(left.Name);
            }

            right.Facets.Count.Should().Be(left.Facets.Count);

            for (var f = 0; f < left.Facets.Count; f++)
            {
                var leftFacet  = left.Facets[f];
                var rightFacet = right.Facets[f];

                if (isBinary)
                {
                    rightFacet.AttributeByteCount.Should().Be(leftFacet.AttributeByteCount);
                }

                rightFacet.Normal.X.Should().Be(leftFacet.Normal.X);
                rightFacet.Normal.Y.Should().Be(leftFacet.Normal.Y);
                rightFacet.Normal.Z.Should().Be(leftFacet.Normal.Z);

                for (var v = 0; v < leftFacet.Vertices.Count; v++)
                {
                    var leftVertice  = leftFacet.Vertices[v];
                    var rightVertice = rightFacet.Vertices[v];

                    rightVertice.X.Should().Be(leftVertice.X, $"vertices at index {v} should be equal");
                    rightVertice.Y.Should().Be(leftVertice.Y, $"vertices at index {v} should be equal");
                    rightVertice.Z.Should().Be(leftVertice.Z, $"vertices at index {v} should be equal");
                }
            }
        }
Exemple #2
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 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);
        }
        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));
        }
Exemple #5
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 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);
        }
Exemple #7
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 { }
        }
Exemple #8
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));
        }
        /// <summary>
        /// Extracts BRep geometry out of a given STLDocument, adds required instances to the IfcModel and returns an fcTriangulatedFaceSet instance to the model
        /// </summary>
        /// <param name="model"></param>
        /// <param name="stlDocument"></param>
        /// <returns></returns>
        public static IfcProductDefinitionShape StlToIfc_TFS(ref IfcStore model, STLDocument stlDocument, double?scaling)
        {
            double scalingFactor = 0.001;

            if (scaling != null)
            {
                scalingFactor = (double)scaling;
            }

            var pointList = model.Instances.New <IfcCartesianPointList3D>();
            // https://github.com/xBimTeam/XbimEssentials/issues/70

            var tfs = model.Instances.New <IfcTriangulatedFaceSet>(fs =>
            {
                fs.Closed      = true;
                fs.Coordinates = pointList;
            });

            foreach (var facet in stlDocument.Facets)
            {
                var facetPoints = facet.Vertices.Select(a => new { a.X, a.Y, a.Z }).ToList();

                // ToDo: check, if the point is already stored and get the indices
                var currentEndCoords = pointList.CoordList.Count;
                var currentEndIndex  = tfs.CoordIndex.Count;

                var indices = new int[3];
                for (int i = 0; i < facetPoints.Count; i++)
                {
                    pointList.CoordList.GetAt(currentEndCoords + i).AddRange(new IfcLengthMeasure[]
                    {
                        facetPoints[i].X * scalingFactor,
                        facetPoints[i].Y * scalingFactor,
                        facetPoints[i].Z * scalingFactor
                    });

                    indices[i] = currentEndCoords + 1 + i;
                }
                tfs.CoordIndex.GetAt(currentEndIndex).AddRange(indices.Select(j => new IfcPositiveInteger(j)));
            }

            // https://github.com/xBimTeam/XbimEssentials/issues/182

            var ifcShapeRepresentation = model.Instances.New <IfcShapeRepresentation>();
            var context = model.Instances.OfType <IfcGeometricRepresentationContext>().FirstOrDefault();

            ifcShapeRepresentation.ContextOfItems           = context;
            ifcShapeRepresentation.RepresentationIdentifier = "Tesselation";
            ifcShapeRepresentation.RepresentationType       = "Tesselation";
            ifcShapeRepresentation.Items.Add(tfs);


            //Erstellt IfcProductDefinitionShape
            var ifcProductDefinitonShape = model.Instances.New <IfcProductDefinitionShape>();

            ifcProductDefinitonShape.Representations.Add(ifcShapeRepresentation);

            return(ifcProductDefinitonShape);
        }
Exemple #10
0
        static void Main(string[] args)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            STLDocument    file;
            FileStream     temp = null;

            dlg.Title       = "STL 바이너리 파일 열기";
            dlg.Multiselect = false;
            dlg.Filter      = "STL 파일(*.stl)|*.stl|모든 파일(*.*)|*.*";

            if (DialogResult.OK != dlg.ShowDialog())
            {
                MessageBox.Show("파일 선택을 해줘!");
                return;
            }


            string path = dlg.FileName;

            if (!File.Exists(path))
            {
                return;
            }


            try
            {
                if (File.Exists(dlg.SafeFileName.Replace(".stl", ".txt")))
                {
                    if (DialogResult.Yes != MessageBox.Show("이미 txt파일이 존재합니다. 덮어쓰시겠습니까?", "중복 오류", MessageBoxButtons.YesNo))
                    {
                        MessageBox.Show("프로그램을 종료합니다.");
                        return;
                    }
                }
                Console.WriteLine("STL 파일을 엽니다");
                if (!STLDocument.IsBinary(temp = new FileStream(dlg.FileName, FileMode.Open)))
                {
                    MessageBox.Show("이미 텍스트 파일입니다. 프로그램을 종료합니다");
                    return;
                }
                temp.Dispose();

                file = STLDocument.Open(path);
                Console.WriteLine("STL 파일을 ASCII파일로 변환중");
                temp = new FileStream(dlg.SafeFileName.Replace(".stl", ".txt"), FileMode.Create);
                file.WriteText(temp);
                MessageBox.Show(dlg.SafeFileName + " 변환 완료");
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }
        }
Exemple #11
0
        private void ValidateSTL(STLDocument stl, int expectedFacetCount = 12)
        {
            Assert.IsNotNull(stl);
            Assert.AreEqual(expectedFacetCount, stl.Facets.Count);

            foreach (Facet facet in stl.Facets)
            {
                Assert.AreEqual(3, facet.Vertices.Count);
            }
        }
Exemple #12
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}");
         }
     }
 }
        public void Read__FromTextStream()
        {
            STLDocument stlString;

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

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

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

            ValidateSTL(stlBinary);
        }
Exemple #15
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);
        }
Exemple #16
0
        public static float CalculateLowY(STLDocument stl)
        {
            float lowestY = stl.Facets[0].Vertices[0].Y;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.Y < lowestY)
                {
                    lowestY = v.Y;
                }
            }));
            return(lowestY);
        }
Exemple #17
0
        public static float CalculateHighY(STLDocument stl)
        {
            float highestY = stl.Facets[0].Vertices[0].Y;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.Y > highestY)
                {
                    highestY = v.Y;
                }
            }));
            return(highestY);
        }
Exemple #18
0
        public static float CalculateLowX(STLDocument stl)
        {
            float lowestX = stl.Facets[0].Vertices[0].X;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.X < lowestX)
                {
                    lowestX = v.X;
                }
            }));
            return(lowestX);
        }
Exemple #19
0
        public static float CalculateLowZ(STLDocument stl)
        {
            float lowestZ = stl.Facets[0].Vertices[0].Z;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.Z < lowestZ)
                {
                    lowestZ = v.Z;
                }
            }));
            return(lowestZ);
        }
Exemple #20
0
        public void StreamLeftOpen()
        {
            using (Stream stream = GetData("ASCII.stl")) {
                STLDocument.Read(stream);

                try {
                    stream.ReadByte();
                }
                catch (ObjectDisposedException) {
                    Assert.Fail("Stream is closed.");
                }
            }
        }
Exemple #21
0
        public static float CalculateHighX(STLDocument stl)
        {
            float highestX = stl.Facets[0].Vertices[0].X;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.X > highestX)
                {
                    highestX = v.X;
                }
            }));
            return(highestX);
        }
Exemple #22
0
        public static float CalculateHighZ(STLDocument stl)
        {
            float highestZ = stl.Facets[0].Vertices[0].Z;

            stl.Facets.ForEach(f => f.Vertices.ForEach(v =>
            {
                if (v.Z > highestZ)
                {
                    highestZ = v.Z;
                }
            }));
            return(highestZ);
        }
Exemple #23
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);
        }
Exemple #24
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);
        }
Exemple #25
0
        public STLRenderer(GraphicsDevice graphics, STLDocument document)
        {
            Graphics = graphics;
            Document = document;

            BuildBuffer();

            _effect = new BasicEffect(graphics);
            _effect.VertexColorEnabled = true;

            _effect.EnableDefaultLighting();
            _effect.Alpha = 1f;
            _effect.PreferPerPixelLighting = true;
        }
Exemple #26
0
    public Mesh(string stl_file)
    {
        STLDocument stldoc = STLDocument.Open(stl_file);

        Facets = new Facet[stldoc.Facets.Count];
        for (int i = 0; i < stldoc.Facets.Count; i++)
        {
            Facets[i] = new Facet(stldoc.Facets[i].Vertices[0].X, stldoc.Facets[i].Vertices[0].Y, stldoc.Facets[i].Vertices[0].Z
                                  , stldoc.Facets[i].Vertices[1].X, stldoc.Facets[i].Vertices[1].Y, stldoc.Facets[i].Vertices[1].Z
                                  , stldoc.Facets[i].Vertices[2].X, stldoc.Facets[i].Vertices[2].Y, stldoc.Facets[i].Vertices[2].Z);
        }

        find_minmax();
    }
Exemple #27
0
        /// <summary>
        /// 生成切片
        /// </summary>
        /// <param name="doc">文件</param>
        /// <returns>切片结果集</returns>
        static Dictionary <ArrayDefine, List <SlicedPlane> > Slice(STLDocument doc)
        {
            Console.WriteLine("请选择切片模式:");
            Console.WriteLine("1.快速切片\t2.自定义切片");
            string type       = Console.ReadLine();
            var    listdefine = new List <ArrayDefine>();

            while (true)
            {
                var plane = new Plane(0, 1, 0, 0);
                if (type == "2")
                {
                    Console.WriteLine("请输入旋转面法线的X分量");
                    int x = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入旋转面法线的Y分量");
                    int y = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入旋转面法线的Z分量");
                    int z = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入旋转面从原点沿其法线的距离");
                    int d = Convert.ToInt32(Console.ReadLine());
                    plane = new Plane(x, y, z, d);
                }

                Console.WriteLine("请输入切片数量");
                var count = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入切片弧度(360为最大)");
                var angle       = Convert.ToInt32(Console.ReadLine());
                var arrayDefine = new ArrayDefine(plane, ArrayType.Circle, count, angle);
                listdefine.Add(arrayDefine);
                if (type == "2")
                {
                    Console.WriteLine("请问是否继续录入下个切片定义?(y/n)");
                    var confirm = Console.ReadLine();
                    if (confirm.ToLower() == "n")
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("正在切片,请稍等...");
            var results = Slicer.Core.Slicer.DoSlice(doc.Facets.ToArray(), listdefine.ToArray());

            Console.WriteLine("切片完成");
            return(results);
        }
Exemple #28
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]));
        }
        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);
        }
        public void Read__FromBinaryReader()
        {
            STLDocument stl;

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

            ValidateSTL(stl);
        }
Exemple #31
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]));
        }
Exemple #32
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));
        }
Exemple #33
0
        public void WriteString()
        {
            STLDocument stl1 = new STLDocument("WriteString", 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;
            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);
        }
Exemple #34
0
        private void ValidateSTL(STLDocument stl, int expectedFacetCount = 12)
        {
            Assert.IsNotNull(stl);
            Assert.AreEqual(expectedFacetCount, stl.Facets.Count);

            foreach (Facet facet in stl.Facets)
                Assert.AreEqual(3, facet.Vertices.Count);
        }