Esempio n. 1
0
        public static void SaveSTL(Mesh mesh, string path)
        {
            StlFile stlFile = new StlFile();

            stlFile.SolidName = "my-solid";
            for (int i = 0; i < mesh.tris.Length; i++)
            {
                Triangle item = mesh.tris[i];
                stlFile.Triangles.Add(ToSTLTriangle(item));
            }
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    stlFile.Save(fs, false);
                }
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine($"Exception during saving STL occured: \n{e.Message}");
            }
        }
Esempio n. 2
0
        public void STLExport(MeshPrimitive mesh, string fileName, bool ascii = true, IEnumerable <Attribution> attributions = null)
        {
            try
            {
                CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
                // ...

                StlFile stlFile = new StlFile();
                stlFile.SolidName = GetAttributionExtraText(attributions);

                foreach (var triangle in mesh.EvaluateTriangles())
                {
                    stlFile.Triangles.Add(CreateStlTriangle(
                                              triangle.A.GetGeometry().GetPosition()
                                              , triangle.B.GetGeometry().GetPosition()
                                              , triangle.C.GetGeometry().GetPosition()));
                }
                // ...
                string folder = System.IO.Path.GetDirectoryName(fileName);
                if (!System.IO.Directory.Exists(folder))
                {
                    System.IO.Directory.CreateDirectory(folder);
                }
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    stlFile.Save(fs, ascii);
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "Error while exporting STL model");
                throw;
            }
        }
Esempio n. 3
0
        public void stlFileParser_openBinary_fileOK()
        {
            string  filename = "SURF-TEST-1.STL";
            StlFile file     = StlFileParser.Open(filename);

            Assert.AreNotEqual(0, file.Count);
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
        private bool doRead()
        {
            if (!File.Exists(PathRead))
            {
                MessageBox.Show("File does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            if (StlFile.IsBinaryFile(PathRead))
            {
                reader = new BinaryStlReader <Triangle, Vertex, Normal>(new DataCreator());
                writer = null;
            }
            else
            {
                reader = new AsciiStlReader <Triangle, Vertex, Normal>(new DataCreator());
                writer = null;
            }

            ByteReadHelper readHelper = readStenography();
            string         msg        = "";

            if (readHelper.ReadEverything())
            {
                try {
                    textBox3.Text = MyEncoding.GetString(readHelper.Data.ToArray());
                    return(true);
                } catch (Exception e) {
                    msg = "\n" + e.Message;
                }
            }
            MessageBox.Show("Could not decode data!" + msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return(false);
        }
Esempio n. 6
0
        public void OutputSTL(string filepath)
        {
            StlFile stlFile = new StlFile();

            stlFile.SolidName = "testsolid";

            foreach (var erf in erfs)
            {
                foreach (var facet in erf.Facets)
                {
                    stlFile.Triangles.Add
                    (
                        new StlTriangle
                        (
                            new StlNormal((float)facet.Normal.X, (float)facet.Normal.Y, (float)facet.Normal.Z),
                            new StlVertex((float)facet.Vertex1.X, (float)facet.Vertex1.Y, (float)facet.Vertex1.Z),
                            new StlVertex((float)facet.Vertex2.X, (float)facet.Vertex2.Y, (float)facet.Vertex2.Z),
                            new StlVertex((float)facet.Vertex3.X, (float)facet.Vertex3.Y, (float)facet.Vertex3.Z)
                        )
                    );
                }
            }

            stlFile.Save(new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None), false);
        }
Esempio n. 7
0
        private bool doWrite()
        {
            if (!File.Exists(PathRead))
            {
                MessageBox.Show("Read file does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            if (File.Exists(PathWrite))
            {
                DialogResult result = MessageBox.Show("Write file already exists, overwrite?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                    return(false);
                }
            }
            if (StlFile.IsBinaryFile(PathRead))
            {
                reader = new BinaryStlReader <Triangle, Vertex, Normal>(new DataCreator());
                writer = new BinaryStlWriter <Triangle, Vertex, Normal>(new DataExtractor());
            }
            else
            {
                reader = new AsciiStlReader <Triangle, Vertex, Normal>(new DataCreator());
                writer = new AsciiStlWriter <Triangle, Vertex, Normal>(new DataExtractor());
            }

            return(performStenography(MyEncoding.GetBytes(textBox3.Text)));
        }
Esempio n. 8
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());
     }
 }
Esempio n. 9
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));
        }
Esempio n. 10
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());
        }
Esempio n. 11
0
        public void WriteFIle(IList <StlTriangle> facets, string path)
        {
            StlFile stlFile = new StlFile();

            stlFile.SolidName = Path.GetFileNameWithoutExtension(path);
            stlFile.Triangles.AddRange(facets);
            using (FileStream fs = new FileStream(path, FileMode.Create)) {
                stlFile.Save(fs, false);
            }
        }
        public void OctreeBuilder_buildFromSTLFile_octreeOK()
        {
            string  fileName            = "Tbinary.STL";
            StlFile stlFile             = StlFileParser.Open(fileName);
            double  minPointSpacing     = .005;
            Octree <AbmachPoint> octree = OctreeBuilder <AbmachPoint> .Build(stlFile, minPointSpacing);

            List <AbmachPoint> points = octree.GetAllPoints();

            Assert.AreNotEqual(0, points.Count);
        }
Esempio n. 13
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);
            }
        }
Esempio n. 14
0
        public void Write(List <Triangle> triangles, string path)
        {
            var stlFile = new StlFile();

            stlFile.SolidName = "solidname";

            triangles.ForEach(t => stlFile.Triangles.Add(Convert(t)));

            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                stlFile.Save(fs);
            }
        }
Esempio n. 15
0
        public void stlFileParser_openAscii_createPointgrid()
        {
            string  filename  = "RECT-TEST-1.STL";
            StlFile file      = StlFileParser.Open(filename);
            var     pointList = new List <DwgEntity>();

            foreach (Triangle tri in file)
            {
                pointList.AddRange(tri.AsPointGrid(.1));
            }
            DxfFileBuilder.Save(pointList, "dxffromStl.dxf");
            Assert.AreEqual(1397, pointList.Count, 100);
        }
Esempio n. 16
0
        public void StlFileParser_parseAscii_fileOK()
        {
            string  filename = "RECT-TEST-1.STL";
            StlFile file     = StlFileParser.Open(filename);

            Assert.AreEqual(2, file.Count);

            Assert.AreEqual(-2.965822, file[0].Vertices[0].X, .001);
            Assert.AreEqual(1.471885, file[0].Vertices[0].Y, .001);
            Assert.AreEqual(2.039691, file[1].Vertices[2].X, .001);
            Assert.AreEqual(-1.323043, file[1].Vertices[2].Y, .001);
            Assert.AreEqual(1.0, file[0].Normal.Z, .001);
        }
Esempio n. 17
0
        public void stlFileParser_saveBinary_fileOK()
        {
            string  filename  = "RECT-TEST-1.STL";
            string  filename2 = "test-save-binary.stl";
            StlFile file      = StlFileParser.Open(filename);

            StlFileParser.SaveBinary(file, filename2);
            StlFile file2 = StlFileParser.Open(filename2);

            Assert.AreEqual(file.Count, file2.Count);
            Assert.AreEqual(file[0].Vertices[0].X, file2[0].Vertices[0].X, .001);
            Assert.AreEqual(file[1].Vertices[2].Y, file2[1].Vertices[2].Y, .001);
            Assert.AreEqual(file[0].Normal.Z, file2[0].Normal.Z, .001);
        }
Esempio n. 18
0
 /// <summary>
 /// build stl files from ring data
 /// </summary>
 /// <param name="correctedRingList"></param>
 /// <param name="flatFileName"></param>
 /// <param name="rolledFileName"></param>
 static void SaveRolledSTLFile(Barrel barrel, DataOutputOptions options, CylGridData correctedRingList, double unrollRadius, string fileName, IProgress <int> progress)
 {
     try
     {
         var v3rolledPtList = correctedRingList.AsCartGridData();
         var _flatStlFile   = new StlFile();
         var trimesh        = new TriMesh();
         trimesh.BuildFromGrid(v3rolledPtList);
         StlFile.SaveBinary(trimesh, fileName);
     }
     catch (Exception)
     {
         throw;
     }
 }
Esempio n. 19
0
        public async Task LoadAsyncはアスキーSTLのポリゴンを読み込むことができる()
        {
            var singleFacet = await StlFile.LoadAsync(@"..\..\TestData\StlAscii\singleFacet-ascii.stl");

            Expect(singleFacet.Geometry.TriangleIndices, Is.EquivalentTo(new[] { Tuple.Create(0, 1, 2) }));

            Expect(singleFacet.Geometry.Positions.ElementAt(0).X, Is.EqualTo(0.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(0).Y, Is.EqualTo(0.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(0).Z, Is.EqualTo(10.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(1).X, Is.EqualTo(10.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(1).Y, Is.EqualTo(0.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(1).Z, Is.EqualTo(10.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(2).X, Is.EqualTo(0.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(2).Y, Is.EqualTo(10.0));
            Expect(singleFacet.Geometry.Positions.ElementAt(2).Z, Is.EqualTo(10.0));
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
        public MainViewModel()
            : base(Messenger.Default)
        {
            モデル = new 空間();

            立方体を追加する = new RelayCommand(() => モデル.モデルを追加する(new 立方体()));

            Stlファイルを読み込む = new RelayCommand(
                () => {
                MessengerInstance.Send(
                    new FileOpenMessage(
                        async path =>
                {
                    モデル.モデルを追加する(await StlFile.LoadAsync(path));
                }));
            });
        }
Esempio n. 22
0
        public void BinaryWriterTest()
        {
            var file = new StlFile();

            file.SolidName = "foo";
            file.Triangles.Add(new StlTriangle(new StlNormal(1, 2, 3), new StlVertex(4, 5, 6), new StlVertex(7, 8, 9), new StlVertex(10, 11, 12)));
            var stream = new MemoryStream();

            file.Save(stream, asAscii: false);
            stream.Seek(0, SeekOrigin.Begin);
            var buffer = new byte[256];
            var read   = stream.Read(buffer, 0, buffer.Length);

            Assert.Equal(134, read);
            var expected = new byte[]
            {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 80 byte header
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00,                                     // 1 facet
                0x00, 0x00, 0x80, 0x3F,                                     // normal.X = 1
                0x00, 0x00, 0x00, 0x40,                                     // normal.Y = 1
                0x00, 0x00, 0x40, 0x40,                                     // normal.Z = 1
                0x00, 0x00, 0x80, 0x40,                                     // vertex1.X = 1
                0x00, 0x00, 0xA0, 0x40,                                     // vertex1.Y = 1
                0x00, 0x00, 0xC0, 0x40,                                     // vertex1.Z = 1
                0x00, 0x00, 0xE0, 0x40,                                     // vertex2.X = 1
                0x00, 0x00, 0x00, 0x41,                                     // vertex2.Y = 1
                0x00, 0x00, 0x10, 0x41,                                     // vertex2.Z = 1
                0x00, 0x00, 0x20, 0x41,                                     // vertex3.X = 1
                0x00, 0x00, 0x30, 0x41,                                     // vertex3.Y = 1
                0x00, 0x00, 0x40, 0x41,                                     // vertex3.Z = 1
                0x00, 0x00                                                  // attribute byte count
            };

            for (int i = 0; i < read; i++)
            {
                Assert.Equal(expected[i], buffer[i]);
            }
        }
Esempio n. 23
0
        public void FileSystemAPITest()
        {
            var filePath = Path.GetTempFileName();
            var stl      = new StlFile();

            stl.Save(filePath);
            var stlText = File.ReadAllText(filePath);

            Assert.Contains("solid", stlText);

            try
            {
                File.Delete(filePath);
            }
            catch
            {
            }
        }
Esempio n. 24
0
        static void SaveFlatSTLFile(Barrel barrel, DataOutputOptions options, CylGridData correctedRingList, double unrollRadius, string fileName, IProgress <int> progress)
        {
            try
            {
                Debug.WriteLine("writing STL file " + fileName);
                var _flatStlFile = new StlFile();


                var v3flatptList = DataUtil.UnrollCylinder(correctedRingList, options.SurfaceFileScaleFactor, unrollRadius);
                var trimesh      = new TriMesh();
                trimesh.BuildFromGrid(v3flatptList);
                StlFile.SaveBinary(trimesh, fileName);
            }
            catch (Exception)
            {
                throw;
            }
        }
        private static void WriteModelToFile(List <Face> faces, string modelName, string path)
        {
            Tracer.Info("Запись 3D модели в STL файл...");

            StlFile stlF = new StlFile();

            stlF.SolidName = modelName;
            foreach (var face in faces)
            {
                stlF.Triangles.Add(new StlTriangle(new StlNormal(face.Normal[0], face.Normal[1], face.Normal[2]),
                                                   new StlVertex(face.Vertices[0].Center.X, face.Vertices[0].Center.Y, face.Vertices[0].Center.Z),
                                                   new StlVertex(face.Vertices[1].Center.X, face.Vertices[1].Center.Y, face.Vertices[1].Center.Z),
                                                   new StlVertex(face.Vertices[2].Center.X, face.Vertices[2].Center.Y, face.Vertices[2].Center.Z)));
            }
            using (FileStream fs = new FileStream(string.Concat(path, @"\", modelName, ".stl"), FileMode.Create))
            {
                stlF.Save(fs);
            }
        }
Esempio n. 26
0
 public void AsciiWriterTest()
 {
     var file = new StlFile();
     file.SolidName = "foo";
     file.Triangles.Add(new StlTriangle(new StlNormal(1, 2, 3), new StlVertex(4, 5, 6), new StlVertex(7, 8, 9), new StlVertex(10, 11, 12)));
     var stream = new MemoryStream();
     file.Save(stream);
     stream.Seek(0, SeekOrigin.Begin);
     var content = new StreamReader(stream).ReadToEnd();
     Assert.Equal(@"solid foo
       facet normal 1.000000e+000 2.000000e+000 3.000000e+000
     outer loop
       vertex 4.000000e+000 5.000000e+000 6.000000e+000
       vertex 7.000000e+000 8.000000e+000 9.000000e+000
       vertex 1.000000e+001 1.100000e+001 1.200000e+001
     endloop
       endfacet
     endsolid foo
     ", content);
 }
Esempio n. 27
0
 public void BinaryWriterTest()
 {
     var file = new StlFile();
     file.SolidName = "foo";
     file.Triangles.Add(new StlTriangle(new StlNormal(1, 2, 3), new StlVertex(4, 5, 6), new StlVertex(7, 8, 9), new StlVertex(10, 11, 12)));
     var stream = new MemoryStream();
     file.Save(stream, asAscii: false);
     stream.Seek(0, SeekOrigin.Begin);
     var buffer = new byte[256];
     var read = stream.Read(buffer, 0, buffer.Length);
     Assert.Equal(134, read);
     var expected = new byte[]
     {
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 80 byte header
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x01, 0x00, 0x00, 0x00,                                     // 1 facet
         0x00, 0x00, 0x80, 0x3F,                                     // normal.X = 1
         0x00, 0x00, 0x00, 0x40,                                     // normal.Y = 1
         0x00, 0x00, 0x40, 0x40,                                     // normal.Z = 1
         0x00, 0x00, 0x80, 0x40,                                     // vertex1.X = 1
         0x00, 0x00, 0xA0, 0x40,                                     // vertex1.Y = 1
         0x00, 0x00, 0xC0, 0x40,                                     // vertex1.Z = 1
         0x00, 0x00, 0xE0, 0x40,                                     // vertex2.X = 1
         0x00, 0x00, 0x00, 0x41,                                     // vertex2.Y = 1
         0x00, 0x00, 0x10, 0x41,                                     // vertex2.Z = 1
         0x00, 0x00, 0x20, 0x41,                                     // vertex3.X = 1
         0x00, 0x00, 0x30, 0x41,                                     // vertex3.Y = 1
         0x00, 0x00, 0x40, 0x41,                                     // vertex3.Z = 1
         0x00, 0x00                                                  // attribute byte count
     };
     for (int i = 0; i < read; i++)
     {
         Assert.Equal(expected[i], buffer[i]);
     }
 }
Esempio n. 28
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);
                    }
                }
        }
Esempio n. 29
0
        public void STLExport(MeshPrimitive mesh, string fileName, bool ascii = true, IEnumerable <Attribution> attributions = null)
        {
            try
            {
                CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
                // ...

                StlFile stlFile = new StlFile();
                stlFile.SolidName = GetAttributionExtraText(attributions);

                //The number of the vertices
                var positions = mesh.Positions.ToList();
                var indices   = mesh.Indices.ToList();

                stlFile.Triangles.Capacity = indices.Count / 3;
                int numTriangle = 0;
                for (int i = 0; i < indices.Count; i += 3)
                {
                    stlFile.Triangles.Add(CreateStlTriangle(positions[indices[i]]
                                                            , positions[indices[i + 1]]
                                                            , positions[indices[i + 2]]));
                    numTriangle++;
                }

                // ...
                string folder = System.IO.Path.GetDirectoryName(fileName);
                if (!System.IO.Directory.Exists(folder))
                {
                    System.IO.Directory.CreateDirectory(folder);
                }
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    stlFile.Save(fs, ascii);
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "Error while exporting STL model");
                throw;
            }
        }
Esempio n. 30
0
        public void WriteDecimalSeparatorCultureTest()
        {
            var existingCulture = CultureInfo.CurrentCulture;

            try
            {
                CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
                var stl = new StlFile();
                stl.Triangles.Add(new StlTriangle(new StlNormal(1, 2, 3), new StlVertex(4, 5, 6), new StlVertex(7, 8, 9), new StlVertex(10, 11, 12)));
                var stream = new MemoryStream();
                stl.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                var content = new StreamReader(stream).ReadToEnd();
                Assert.Contains("facet normal 1.000000e+000 2.000000e+000 3.000000e+000", content);
                Assert.Contains("vertex 4.000000e+000 5.000000e+000 6.000000e+000", content);
            }
            finally
            {
                CultureInfo.CurrentCulture = existingCulture;
            }
        }
Esempio n. 31
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)));
        }
Esempio n. 32
0
        public void AsciiWriterTest()
        {
            var file = new StlFile();

            file.SolidName = "foo";
            file.Triangles.Add(new StlTriangle(new StlNormal(1, 2, 3), new StlVertex(4, 5, 6), new StlVertex(7, 8, 9), new StlVertex(10, 11, 12)));
            var stream = new MemoryStream();

            file.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            var content = new StreamReader(stream).ReadToEnd();

            Assert.Equal(@"solid foo
  facet normal 1.000000e+000 2.000000e+000 3.000000e+000
    outer loop
      vertex 4.000000e+000 5.000000e+000 6.000000e+000
      vertex 7.000000e+000 8.000000e+000 9.000000e+000
      vertex 1.000000e+001 1.100000e+001 1.200000e+001
    endloop
  endfacet
endsolid foo
", content);
        }