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 { } }
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(); } } }
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(); }
public void FromFile() { STLDocument stl = null; using (Stream stream = GetData("ASCII.stl")) { string tempFilePath = Path.GetTempFileName(); using (TemporaryFileStream tempStream = new TemporaryFileStream(tempFilePath)) { stream.CopyTo(tempStream); tempStream.Flush(); tempStream.Close(); stl = STLDocument.Open(tempFilePath); } } ValidateSTL(stl); }
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 { } }
static void TestSlicer() { //-0.03141076,0.9995066 var doc = STLDocument.Open(AppDomain.CurrentDomain.BaseDirectory + @"/stl/squirtle__repaired_.stl"); doc.MakeCenter(); var results = Slicer.DoSlice(doc.Facets.ToArray(), new ArrayDefine[] { new ArrayDefine(new Plane(0, 1, 0, 0), ArrayType.Circle, 200, 360) //new ArrayDefine(new Plane(-0.03141076f, 0.9995066f, 0, 8), ArrayType.Circle, 8) }); var prefix = 0; IImageMould im = new LED6432P(); foreach (var key in results.Keys) { var filename = AppDomain.CurrentDomain.BaseDirectory + prefix + "_frame.h"; System.IO.File.Delete(filename); var code = ""; var frameTable = new List <string>(); var images = SliceImage.ToImage(results[key], doc.Size, 64, 32, 0, 0); for (int i = 0; i < images.Count; i++) { var bmp = images[i]; bmp.Save(AppDomain.CurrentDomain.BaseDirectory + "/" + prefix + "_" + i + ".bmp", ImageFormat.Bmp); code += im.GetFrameCode(prefix, i, bmp); frameTable.Add("_" + prefix + "_frame_" + i); } code += im.GetFramesCode(prefix, frameTable); System.IO.File.AppendAllText(filename, code); prefix++; } Console.WriteLine("TestSlicer finish"); }
public void FromFile() { STLDocument stl = null; using (Stream inStream = GetData("ASCII.stl")) { string tempFilePath = Path.GetTempFileName(); using (var outStream = File.Create(tempFilePath)) { inStream.CopyTo(outStream); } stl = STLDocument.Open(tempFilePath); try { File.Delete(tempFilePath); } catch { /* Ignore. */ } } ValidateSTL(stl); }
/// <summary> /// 读取文件 /// </summary> /// <returns></returns> static STLDocument ReadFile() { Console.WriteLine("请输入要切片的3D图绝对路径:"); var path = Console.ReadLine(); while (true) { if (!Path.IsPathRooted(path)) { Console.WriteLine("请输入正确的地址:"); path = Console.ReadLine(); } else { break; } } Console.WriteLine("正在读取文件请稍后..."); var doc = STLDocument.Open(path); doc.MakeCenter(); return(doc); }
public static MeshData TestImport() { OpenFileDialog dialog = new OpenFileDialog() { Filter = "3d file|*.obj;*.stl" }; if (dialog.ShowDialog() == true) { var fileName = dialog.FileName; IMeshBuilder builder; if (Path.GetExtension(fileName).Contains("obj")) { builder = ObjDocument.Open(fileName); } else { builder = STLDocument.Open(fileName); } var meshData = builder.ConvertToMesh(); return(meshData); } return(null); }
/// <summary> /// _3DModel 변환 /// </summary> /// <param name="filename"></param> /// <param name="extension"></param> /// <returns></returns> public Object3DModel Get3DModel(string filename, string extension) { Object3DModel stlModel = new Object3DModel(); Metadata metadata = new Metadata(); metadata.formatVersion = "3"; metadata.generatedBy = "Mersh"; stlModel.metadata = metadata; stlModel.scale = "1.0"; stlModel.materials = new string[] { }; stlModel.morphTargets = new string[] { }; stlModel.morphColors = new string[] { }; stlModel.normals = new string[] { }; stlModel.colors = new string[] { }; stlModel.uvs = new string[, ] { }; int indexF = 0; int indexV = 0; if (extension == ".stl") { STLDocument facets = STLDocument.Open(filename); int index = 0; stlModel.faces = new int[facets.Count() * 4]; stlModel.vertices = new float[facets.Count() * 9]; foreach (var facet in facets) { stlModel.faces[index] = 0; index++; foreach (var vertice in facet.Vertices) { VertexM m = new VertexM(); stlModel.faces[index] = indexF; stlModel.vertices[indexV] = vertice.X; indexV++; stlModel.vertices[indexV] = vertice.Y; indexV++; stlModel.vertices[indexV] = vertice.Z; indexV++; indexF++; index++; } } } if (extension == ".obj") { VertexList = new List <QuantumConcepts.Formats.StereoLithography.Vertex>(); OBJDocument objDoc = new OBJDocument().LoadObj(filename); stlModel.faces = new int[objDoc.FaceList.Count() * 4]; stlModel.vertices = new float[objDoc.VertexList.Count() * 9]; for (int i = 0; i < objDoc.FaceList.Count; i++) { stlModel.faces[indexF] = 0; indexF++; stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[0] - 1; indexF++; stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[1] - 1; indexF++; stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[2] - 1; indexF++; } for (int i = 0; i < objDoc.VertexList.Count; i++) { VertexM m = new VertexM(); stlModel.vertices[indexV] = (float)objDoc.VertexList[i].X; indexV++; stlModel.vertices[indexV] = (float)objDoc.VertexList[i].Y; indexV++; stlModel.vertices[indexV] = (float)objDoc.VertexList[i].Z; indexV++; } } return(stlModel); }
/// <summary> /// 3D파일 사이즈 구하기 /// </summary> /// <param name="path"></param> /// <param name="ext"></param> /// <returns></returns> public ModelingSize GetSizeFor3DFile(string path, string ext) { ModelingSize getSize = new ModelingSize(); double volume = 0; double x1 = 0; double y1 = 0; double z1 = 0; double x2 = 0; double y2 = 0; double z2 = 0; double x3 = 0; double y3 = 0; double z3 = 0; switch (ext) { case ".stl": STLDocument facets = STLDocument.Open(path); //stl Size = new Extent { XMax = facets.Facets.Max(f => f.Vertices.Max(v => v.X)), XMin = facets.Facets.Min(f => f.Vertices.Min(v => v.X)), YMax = facets.Facets.Max(f => f.Vertices.Max(v => v.Y)), YMin = facets.Facets.Min(f => f.Vertices.Min(v => v.Y)), ZMax = facets.Facets.Max(f => f.Vertices.Max(v => v.Z)), ZMin = facets.Facets.Min(f => f.Vertices.Min(v => v.Z)) }; for (int i = 0; i < facets.Facets.Count; i++) { x1 = facets.Facets[i].Vertices[0].X; y1 = facets.Facets[i].Vertices[0].Y; z1 = facets.Facets[i].Vertices[0].Z; x2 = facets.Facets[i].Vertices[1].X; y2 = facets.Facets[i].Vertices[1].Y; z2 = facets.Facets[i].Vertices[1].Z; x3 = facets.Facets[i].Vertices[2].X; y3 = facets.Facets[i].Vertices[2].Y; z3 = facets.Facets[i].Vertices[2].Z; volume += (-x3 * y2 * z1 + x2 * y3 * z1 + x3 * y1 * z2 - x1 * y3 * z2 - x2 * y1 * z3 + x1 * y2 * z3) / 6; } break; case ".obj": VertexList = new List <QuantumConcepts.Formats.StereoLithography.Vertex>(); OBJDocument objDoc = new OBJDocument().LoadObj(path); int[] idx = new int[3]; Size = new Extent { XMax = objDoc.VertexList.Where(w => (int)w.X != 0).Max(v => v.X), XMin = objDoc.VertexList.Where(w => (int)w.X != 0).Min(v => v.X), YMax = objDoc.VertexList.Where(w => (int)w.Y != 0).Max(v => v.Y), YMin = objDoc.VertexList.Where(w => (int)w.Y != 0).Min(v => v.Y), ZMax = objDoc.VertexList.Where(w => (int)w.Z != 0).Max(v => v.Z), ZMin = objDoc.VertexList.Where(w => (int)w.Z != 0).Min(v => v.Z) }; int vertexCnt = objDoc.VertexList.Count(); for (int i = 0; i < objDoc.FaceList.Count; i++) { idx[0] = objDoc.FaceList[i].VertexIndexList[0] - 1; idx[1] = objDoc.FaceList[i].VertexIndexList[1] - 1; idx[2] = objDoc.FaceList[i].VertexIndexList[2] - 1; if (idx[0] > 0 && idx[1] > 0 && idx[2] > 0) { if (idx[0] > vertexCnt && idx[1] > vertexCnt && idx[2] > vertexCnt) { //log 로 남겨보기 } else { x1 = objDoc.VertexList[idx[0]].X; y1 = objDoc.VertexList[idx[0]].Y; z1 = objDoc.VertexList[idx[0]].Z; x2 = objDoc.VertexList[idx[1]].X; y2 = objDoc.VertexList[idx[1]].Y; z2 = objDoc.VertexList[idx[1]].Z; x3 = objDoc.VertexList[idx[2]].X; y3 = objDoc.VertexList[idx[2]].Y; z3 = objDoc.VertexList[idx[2]].Z; volume += (-x3 * y2 * z1 + x2 * y3 * z1 + x3 * y1 * z2 - x1 * y3 * z2 - x2 * y1 * z3 + x1 * y2 * z3) / 6; } } else { //log 로 남겨보기 } } break; } volume = volume < 0 ? volume * -1 : volume; getSize.X = Math.Round(Size.XSize, 1); getSize.Y = Math.Round(Size.YSize, 1); getSize.Z = Math.Round(Size.ZSize, 1); getSize.ObjectVolume = Math.Round(volume, 1) / 1000; return(getSize); }