Exemple #1
0
        public void ReadBinaryTriangleWithNoNormalTest()
        {
            var binaryList = new List <byte>();

            binaryList.AddRange(new byte[80]);                    // header
            binaryList.AddRange(BitConverter.GetBytes(1));        // 1 triangle
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // normal.X
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // normal.Y
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // normal.Z
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex1.X
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex1.Y
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex1.Z
            binaryList.AddRange(BitConverter.GetBytes(1.0f));     // vertex2.X
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex2.Y
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex2.Z
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex3.X
            binaryList.AddRange(BitConverter.GetBytes(1.0f));     // vertex3.Y
            binaryList.AddRange(BitConverter.GetBytes(0.0f));     // vertex3.Z
            binaryList.AddRange(BitConverter.GetBytes((short)0)); // attribute byte count
            using (var ms = new MemoryStream())
            {
                ms.Write(binaryList.ToArray(), 0, binaryList.Count);
                ms.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                var file = StlFile.Load(ms);

                // should be auto-corrected to the positive z axis
                Assert.Equal(new StlNormal(0, 0, 1), file.Triangles.Single().Normal);
            }
        }
Exemple #2
0
 public List <Triangle> Read(string path)
 {
     using (FileStream fs = new FileStream(path, FileMode.Open))
     {
         var stlFile = StlFile.Load(fs);
         return(stlFile.Triangles.Select(t => Convert(t)).ToList());
     }
 }
Exemple #3
0
        public List <StlTriangle> LoadFile(string path)
        {
            StlFile stlFile = null;

            using (FileStream fs = new FileStream(path, FileMode.Open)) {
                stlFile = StlFile.Load(fs);
            }

            return(stlFile.Triangles.ToList());
        }
Exemple #4
0
        private StlFile FromString(string content)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write(content.Trim());
            writer.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            return(StlFile.Load(stream));
        }
Exemple #5
0
        private StlFile FromBytes(byte[] bytes)
        {
            using (var ms = new MemoryStream())
            {
                ms.Write(bytes, 0, bytes.Length);
                ms.Flush();
                ms.Seek(0, SeekOrigin.Begin);

                var file = StlFile.Load(ms);
                return(file);
            }
        }
Exemple #6
0
        public static Mesh LoadSTL(string path)
        {
            StlFile stlFile;
            Mesh    m;

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                stlFile = StlFile.Load(fs);
                Triangle[] tris = new Triangle[stlFile.Triangles.Count];
                for (int i = 0; i < tris.Length; i++)
                {
                    tris[i] = FromSTLTriangle(stlFile.Triangles[i]);
                }
                m.tris = tris;
            }
            return(m);
        }
Exemple #7
0
        public void ReadAsciiStlNoLengthOrPositionStreamTest()
        {
            using (var ms = new MemoryStream())
                using (var writer = new StreamWriter(ms))
                {
                    writer.WriteLine(@"
solid foo
  facet normal 1 2 3
    outer loop
      vertex 4 5 6
      vertex 7 8 9
      vertex 10 11 12
    endloop
  endfacet
  facet normal 13 14 15
    outer loop
      vertex 16 17 18
      vertex 19 20 21
      vertex 22 23 24
    endloop
  endfacet
endsolid foo
".Trim());
                    writer.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    using (var stream = new StreamWithNoLengthOrPosition(ms))
                    {
                        var file = StlFile.Load(stream);
                        Assert.Equal("foo", file.SolidName);
                        Assert.Equal(2, file.Triangles.Count);
                        Assert.Equal(new StlNormal(1, 2, 3), file.Triangles[0].Normal);
                        Assert.Equal(new StlVertex(4, 5, 6), file.Triangles[0].Vertex1);
                        Assert.Equal(new StlVertex(7, 8, 9), file.Triangles[0].Vertex2);
                        Assert.Equal(new StlVertex(10, 11, 12), file.Triangles[0].Vertex3);
                        Assert.Equal(new StlNormal(13, 14, 15), file.Triangles[1].Normal);
                        Assert.Equal(new StlVertex(16, 17, 18), file.Triangles[1].Vertex1);
                        Assert.Equal(new StlVertex(19, 20, 21), file.Triangles[1].Vertex2);
                        Assert.Equal(new StlVertex(22, 23, 24), file.Triangles[1].Vertex3);
                    }
                }
        }
Exemple #8
0
        public Task <ReadDrawingResult> ReadDrawing(string fileName, Stream fileStream, Func <string, Task <byte[]> > contentResolver)
        {
            var file  = StlFile.Load(fileStream);
            var lines = new Line[file.Triangles.Count * 3];
            var index = 0;

            foreach (var triangle in file.Triangles)
            {
                lines[index++] = new Line(ToPoint(triangle.Vertex1), ToPoint(triangle.Vertex2));
                lines[index++] = new Line(ToPoint(triangle.Vertex2), ToPoint(triangle.Vertex3));
                lines[index++] = new Line(ToPoint(triangle.Vertex3), ToPoint(triangle.Vertex1));
            }

            var layer   = new Layer(file.SolidName ?? "stl", lines);
            var drawing = new Drawing(
                new DrawingSettings(fileName, UnitFormat.Architectural, -1, -1),
                new ReadOnlyTree <string, Layer>().Insert(layer.Name, layer));

            drawing.Tag = file;

            return(Task.FromResult(ReadDrawingResult.Succeeded(drawing, null)));
        }
Exemple #9
0
        public bool ReadDrawing(string fileName, Stream fileStream, out Drawing drawing, out ViewPort viewPort)
        {
            var file  = StlFile.Load(fileStream);
            var lines = new Line[file.Triangles.Count * 3];
            var index = 0;

            foreach (var triangle in file.Triangles)
            {
                lines[index++] = new Line(ToPoint(triangle.Vertex1), ToPoint(triangle.Vertex2));
                lines[index++] = new Line(ToPoint(triangle.Vertex2), ToPoint(triangle.Vertex3));
                lines[index++] = new Line(ToPoint(triangle.Vertex3), ToPoint(triangle.Vertex1));
            }

            var layer = new Layer(file.SolidName ?? "stl", lines);

            drawing = new Drawing(
                new DrawingSettings(fileName, UnitFormat.Architectural, -1),
                new ReadOnlyTree <string, Layer>().Insert(layer.Name, layer));
            drawing.Tag = file;

            viewPort = null; // auto-set it later

            return(true);
        }
Exemple #10
0
        public Spine(string key, List <Vertex> points, List <Geometry> geometries, int direction)
        {
            Key        = key;
            Points     = points;
            Geometries = geometries;
            if (Points.Count == 4)
            {
                //Считывание модели из файла
                Model = new StlMesh(StlFile.Load(new FileStream("FullModel.stl", FileMode.Open, FileAccess.Read))
                                    .Triangles);

                int k = 0;

                Up    = new List <Pointes>();
                Down  = new List <Pointes>();
                UpC   = new List <Pointes>();
                DownC = new List <Pointes>();
                using (StreamReader sr = new StreamReader("up.txt"))
                {
                    while (sr.Peek() >= 0)
                    {
                        string   str;
                        string[] strArray;
                        str = sr.ReadLine();

                        strArray = str.Split(' ');
                        Pointes currentPoint = new Pointes();
                        currentPoint.X = float.Parse(strArray[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Y = float.Parse(strArray[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Z = float.Parse(strArray[2], NumberStyles.Float, CultureInfo.InvariantCulture);
                        Up.Add(currentPoint);
                    }
                }
                using (StreamReader sr = new StreamReader("down.txt"))
                {
                    while (sr.Peek() >= 0)
                    {
                        string   str;
                        string[] strArray;
                        str = sr.ReadLine();

                        strArray = str.Split(' ');
                        Pointes currentPoint = new Pointes();
                        currentPoint.X = float.Parse(strArray[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Y = float.Parse(strArray[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Z = float.Parse(strArray[2], NumberStyles.Float, CultureInfo.InvariantCulture);
                        Down.Add(currentPoint);
                    }
                }
                using (StreamReader sr = new StreamReader("up_corner.txt"))
                {
                    while (sr.Peek() >= 0)
                    {
                        string   str;
                        string[] strArray;
                        str = sr.ReadLine();

                        strArray = str.Split(' ');
                        Pointes currentPoint = new Pointes();
                        currentPoint.X = float.Parse(strArray[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Y = float.Parse(strArray[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Z = float.Parse(strArray[2], NumberStyles.Float, CultureInfo.InvariantCulture);
                        UpC.Add(currentPoint);
                    }
                }
                using (StreamReader sr = new StreamReader("down_corner.txt"))
                {
                    while (sr.Peek() >= 0)
                    {
                        string   str;
                        string[] strArray;
                        str = sr.ReadLine();

                        strArray = str.Split(' ');
                        Pointes currentPoint = new Pointes();
                        currentPoint.X = float.Parse(strArray[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Y = float.Parse(strArray[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        currentPoint.Z = float.Parse(strArray[2], NumberStyles.Float, CultureInfo.InvariantCulture);
                        DownC.Add(currentPoint);
                    }
                }
                U = new List <int>();
                foreach (var a in Up)
                {
                    foreach (var b in Model.Vertices)
                    {
                        if (a.X == b.X && a.Y == b.Y && a.Z == b.Z)
                        {
                            U.Add(b.IdInMesh);
                        }
                    }
                }
                D = new List <int>();
                foreach (var a in Down)
                {
                    foreach (var b in Model.Vertices)
                    {
                        if (a.X == b.X && a.Y == b.Y && a.Z == b.Z)
                        {
                            D.Add(b.IdInMesh);
                        }
                    }
                }
                UC = new List <int>();
                foreach (var a in UpC)
                {
                    foreach (var b in Model.Vertices)
                    {
                        if (a.X == b.X && a.Y == b.Y && a.Z == b.Z)
                        {
                            UC.Add(b.IdInMesh);
                        }
                    }
                }
                DC = new List <int>();
                foreach (var a in DownC)
                {
                    foreach (var b in Model.Vertices)
                    {
                        if (a.X == b.X && a.Y == b.Y && a.Z == b.Z)
                        {
                            DC.Add(b.IdInMesh);
                        }
                    }
                }

                //TODO Здесь добавить маркировку точек

                if (direction == 0)
                {
                    //Отражение
                    Model.Vertices = Transformation.ConvertPointsArrayToList(
                        Transformation.ReflectionObject(Transformation.ConvertPointsListToArray(Model.Vertices), true,
                                                        false, false));
                }

                //Масштабирование
                var dy = Vertex.D(Points[2], Points[3]) / Vertex.D(Model.Vertices[_k2], Model.Vertices[_k3]);
                var dx = Vertex.D(Points[2], Points[1]) / Vertex.D(Model.Vertices[_k2], Model.Vertices[_k1]);
                Model.Vertices =
                    Transformation.ConvertPointsArrayToList(
                        Transformation.ScaleObject(Transformation.ConvertPointsListToArray(Model.Vertices), dx, dy,
                                                   dx));

                //Поворот
                double k2 = (Points[0].Y - Points[3].Y) / (Points[0].X - Points[3].X);
                double k1 = (Model.Vertices[_k0].Y - Model.Vertices[_k3].Y) /
                            (Model.Vertices[_k0].X - Model.Vertices[_k3].X);
                double alfa = Math.Atan((k2 - k1) / (1 + k2 * k1));
                Model.Vertices = Transformation.ConvertPointsArrayToList(Transformation.Rotate(alfa,
                                                                                               Transformation.ConvertPointsListToArray(Model.Vertices), 3, 0, 0, 0));

                //Перемещение
                Model.Vertices = Transformation.ConvertPointsArrayToList(Transformation.MoveObject(
                                                                             Transformation.ConvertPointsListToArray(Model.Vertices), Points[3].X - Model.Vertices[_k3].X,
                                                                             Points[3].Y - Model.Vertices[_k3].Y, 0));
            }
        }
Exemple #11
0
        public void ReadBinaryStlNoLengthOrPositionStreamTest()
        {
            var binarySize =
                80 +                    // header
                sizeof(int) +           // number of triangles
                2 *                     // 2 verticies
                (
                    sizeof(float) * 3 + // normal vector
                    sizeof(float) * 3 + // vertex 1
                    sizeof(float) * 3 + // vertex 2
                    sizeof(float) * 3 + // vertex 3
                    sizeof(short)       // attribute byte count
                );
            var binaryList = new List <byte>();

            binaryList.AddRange(new byte[80]);             // header
            binaryList.AddRange(BitConverter.GetBytes(2)); // 2 triangles

            // first triangle
            binaryList.AddRange(BitConverter.GetBytes(1.0f));     // normal.X
            binaryList.AddRange(BitConverter.GetBytes(2.0f));     // normal.Y
            binaryList.AddRange(BitConverter.GetBytes(3.0f));     // normal.Z
            binaryList.AddRange(BitConverter.GetBytes(4.0f));     // vertex1.X
            binaryList.AddRange(BitConverter.GetBytes(5.0f));     // vertex1.Y
            binaryList.AddRange(BitConverter.GetBytes(6.0f));     // vertex1.Z
            binaryList.AddRange(BitConverter.GetBytes(7.0f));     // vertex2.X
            binaryList.AddRange(BitConverter.GetBytes(8.0f));     // vertex2.Y
            binaryList.AddRange(BitConverter.GetBytes(9.0f));     // vertex2.Z
            binaryList.AddRange(BitConverter.GetBytes(10.0f));    // vertex3.X
            binaryList.AddRange(BitConverter.GetBytes(11.0f));    // vertex3.Y
            binaryList.AddRange(BitConverter.GetBytes(12.0f));    // vertex3.Z
            binaryList.AddRange(BitConverter.GetBytes((short)0)); // attribute byte count

            // second triangle
            binaryList.AddRange(BitConverter.GetBytes(13.0f));    // normal.X
            binaryList.AddRange(BitConverter.GetBytes(14.0f));    // normal.Y
            binaryList.AddRange(BitConverter.GetBytes(15.0f));    // normal.Z
            binaryList.AddRange(BitConverter.GetBytes(16.0f));    // vertex1.X
            binaryList.AddRange(BitConverter.GetBytes(17.0f));    // vertex1.Y
            binaryList.AddRange(BitConverter.GetBytes(18.0f));    // vertex1.Z
            binaryList.AddRange(BitConverter.GetBytes(19.0f));    // vertex2.X
            binaryList.AddRange(BitConverter.GetBytes(20.0f));    // vertex2.Y
            binaryList.AddRange(BitConverter.GetBytes(21.0f));    // vertex2.Z
            binaryList.AddRange(BitConverter.GetBytes(22.0f));    // vertex3.X
            binaryList.AddRange(BitConverter.GetBytes(23.0f));    // vertex3.Y
            binaryList.AddRange(BitConverter.GetBytes(24.0f));    // vertex3.Z
            binaryList.AddRange(BitConverter.GetBytes((short)0)); // attribute byte count

            Assert.Equal(binarySize, binaryList.Count);

            using (var ms = new MemoryStream())
            {
                ms.Write(binaryList.ToArray(), 0, binaryList.Count);
                ms.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                using (var stream = new StreamWithNoLengthOrPosition(ms))
                {
                    var file = StlFile.Load(stream);
                    Assert.Equal(2, file.Triangles.Count);

                    Assert.Equal(new StlNormal(1.0f, 2.0f, 3.0f), file.Triangles[0].Normal);
                    Assert.Equal(new StlVertex(4.0f, 5.0f, 6.0f), file.Triangles[0].Vertex1);
                    Assert.Equal(new StlVertex(7.0f, 8.0f, 9.0f), file.Triangles[0].Vertex2);
                    Assert.Equal(new StlVertex(10.0f, 11.0f, 12.0f), file.Triangles[0].Vertex3);

                    Assert.Equal(new StlNormal(13.0f, 14.0f, 15.0f), file.Triangles[1].Normal);
                    Assert.Equal(new StlVertex(16.0f, 17.0f, 18.0f), file.Triangles[1].Vertex1);
                    Assert.Equal(new StlVertex(19.0f, 20.0f, 21.0f), file.Triangles[1].Vertex2);
                    Assert.Equal(new StlVertex(22.0f, 23.0f, 24.0f), file.Triangles[1].Vertex3);
                }
            }
        }