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; } }
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}"); } }
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); }
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 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); } }
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]); } }
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 { } }
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); } }
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); }
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; } }
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; } }
protected override void SolveInstance(IGH_DataAccess DA) { Model model = new Model(); string dir = null; bool trigger = false; if (!DA.GetData(0, ref model)) { return; } if (!DA.GetData(1, ref dir)) { return; } if (!DA.GetData(2, ref trigger)) { return; } if (!trigger) { DA.SetData(0, outputMessage); return; } if (model == null) { return; } if (!Directory.Exists(dir)) { outputMessage = "[" + DateTime.Now.ToString() + "]: Output directory does not exists."; DA.SetData(0, outputMessage); return; } var dirFiles = Directory.GetFiles(dir); foreach (var file in dirFiles.Where(f => f.Contains("RS_Joint_Mesh_"))) { File.Delete(file); } // Get unit scaling var unit = Rhino.RhinoDoc.ActiveDoc.ModelUnitSystem; var scale = 1.0f; switch (unit) { case Rhino.UnitSystem.Millimeters: scale = 1.0f; break; case Rhino.UnitSystem.Centimeters: scale = 10; break; case Rhino.UnitSystem.Meters: scale = 1000; break; case Rhino.UnitSystem.Inches: scale = 25.4f; break; case Rhino.UnitSystem.Feet: scale = 304.8f; break; } int fileCounter = 0; foreach (var kvp in model.JointMeshes) { var stlFile = new StlFile(); stlFile.SolidName = "RS_Joint_Mesh_" + kvp.Key.ToString(); foreach (var m in kvp.Value) { var vertices = m.Vertices.Select(v => new StlVertex(v.X * scale, v.Y * scale, v.Z * scale)).ToList(); var faces = m.Faces; var normals = m.FaceNormals.Select(n => new StlNormal(n.X, n.Y, n.Z)).ToList(); for (int f = 0; f < faces.Count(); f++) { stlFile.Triangles.Add(new StlTriangle( normals[f], vertices[faces[f].A], vertices[faces[f].B], vertices[faces[f].C])); if (faces[f].IsQuad) { stlFile.Triangles.Add(new StlTriangle( normals[f], vertices[faces[f].A], vertices[faces[f].C], vertices[faces[f].D])); } } } if (stlFile.Triangles.Count > 0) { using (FileStream fs = File.Create(Path.Combine(dir, stlFile.SolidName + ".stl"))) { stlFile.Save(fs); fileCounter++; } } } outputMessage = "[" + DateTime.Now.ToString() + "]: Successfully wrote " + fileCounter.ToString() + " STL files."; DA.SetData(0, outputMessage); }