// Start is called before the first frame update void Start() { string fileName = @"C:\Users\Hayden\Downloads\Test STL.stl"; byte[] fileData = File.ReadAllBytes(fileName); STLFile testFile; Stopwatch sw = new Stopwatch(); try { sw.Start(); testFile = new STLFile(fileData); sw.Stop(); } catch (Exception ex) { Debug.LogWarning($"Error loading STL file: {ex}"); return; } Debug.Log($"Loaded solid \"{testFile.Name}\" with {testFile.Facets.Length} facets in {sw.ElapsedMilliseconds}ms"); var mesh = Triangle.CreateMeshFromSTL(testFile); //mesh.RecalculateNormals(); // get mesh filter and assign mesh MeshFilter mf = GetComponent <MeshFilter>(); mf.mesh = mesh; }
public MainWindow() { InitializeComponent(); _viewer3DVM = new Viewer3DViewModel(); Viewer3D.DataContext = _viewer3DVM; _textEditorVM = new TextEditorViewModel(TextEditor, _viewer3DVM); TextEditor.DataContext = _textEditorVM; Statusbar.DataContext = new StatusBarViewModel(_textEditorVM); string filePath = @"STL-Files\xyz-calibrationCube.stl"; var stlFile = new STLFile(filePath); var meshes = stlFile.LoadMeshes(); // cutting meshes var cutMeshes = meshes[0].Cut(new Vector3d(1, 1, 10), new Vector3d(0, 0, 1)); _viewer3DVM.Add(new Wireframe(cutMeshes.BaseMesh, Color.Yellow, Color.DarkSlateGray)); _viewer3DVM.Add(new Wireframe(cutMeshes.CutOffMesh, Color.GreenYellow)); //stlFile.SaveMeshes(new List<Mesh> { cutMeshes.BaseMesh }); }
public static Mesh CreateMeshFromSTL(STLFile stlFile) { Vector3[] vertices = new Vector3[stlFile.Facets.Length * 3]; Vector3[] normals = new Vector3[stlFile.Facets.Length * 3]; int[] triangles = new int[stlFile.Facets.Length * 3]; for (int i = 0; i < stlFile.Facets.Length; i++) { // copy vertices, triangles, and normals for each facet for (int j = 0; j < 3; j++) { int meshIndex = (i * 3) + j; vertices[meshIndex] = stlFile.Facets[i].Vertices[j]; // unity normals are per-vertex and not triangle. copy normal 3x for each vertex normals[meshIndex] = stlFile.Facets[i].Normal; triangles[meshIndex] = meshIndex; } } return(new Mesh() { vertices = vertices, normals = normals, triangles = triangles }); }
public void LoadBinary() { var stl = new STLFile("Models/cube_binary.stl"); Assert.AreEqual(STLType.Binary, stl.STLType); Assert.AreEqual(12, stl.Facets.Count); Assert.AreEqual("solid TESTCUBE", stl.Header); }
public void LoadASCII() { var stl = new STLFile("Models/cube_ascii.stl"); Assert.AreEqual(STLType.ASCII, stl.STLType); Assert.AreEqual(12, stl.Facets.Count); Assert.AreEqual("TESTCUBE", stl.Header); }
private static void makeSolid(string inputFile, string outputFile, string cubeFile, double size, double zMargin) { Console.WriteLine("Reading mesh..."); var meshCreator = new SolidMeshCreator(STLFile.Read(inputFile).ToArray(), size, zMargin); Console.WriteLine("Writing..."); STLFile.Write(outputFile, meshCreator.Triangles); STLFile.Write(cubeFile, meshCreator.GetCube().ToArray()); Console.WriteLine("Complete."); }
public STLExtractor(string stlFileLocation, STLInfo.STLUnits units = STLInfo.STLUnits.Centimeters, bool InhibitFullAnalysis = false) { if (File.Exists(stlFileLocation)) { ValidState = FileValidity.Valid; IsValid = true; try { STL = new STLFile(File.Open(stlFileLocation, FileMode.Open), units, InhibitFullAnalysis); } catch (ArgumentNullException) { IsValid = false; ValidState = FileValidity.FileNameNull; } catch (UnauthorizedAccessException) { IsValid = false; ValidState = FileValidity.UnauthorizedAccess; } catch (ArgumentException) { IsValid = false; ValidState = FileValidity.InvalidFile; } catch (PathTooLongException) { IsValid = false; ValidState = FileValidity.PathTooLong; } catch (NotSupportedException) { IsValid = false; ValidState = FileValidity.UnsupportedFileOperation; } } else { ValidState = FileValidity.WrongPath; IsValid = false; } }
static void Main(string[] args) { var stl = new STLFile(@"C:\temp\testelephant.stl"); //stl.Save(@"C:\temp\test.stl"); // var s = @" //using System; //using System.Collections.Generic; //using OpenCAD.Kernel.Maths; //using OpenCAD.Kernel.Modeling; //using OpenCAD.Kernel.Modeling.Octree; //using OpenCAD.Kernel.Primatives; //using OpenCAD.Kernel.Scripting; // //namespace Test //{ // public class TestPart:IPartScript // { // public IEnumerable<IPartScriptOption> Options() // { // throw new NotImplementedException(); // } // // public IModel Generate() // { // return new OctreeModel(new OctreeNode(Vect3.Zero, 16, 5), String.Empty); // } // } //} //"; // var comp = Compilation.Create("test", new CompilationOptions(OutputKind.DynamicallyLinkedLibrary), new[] { SyntaxTree.ParseText(s) }, new[] // { // MetadataReference.CreateAssemblyReference("mscorlib"), // MetadataReference.CreateAssemblyReference("System"), // MetadataReference.CreateAssemblyReference("System.Core"), // MetadataReference.CreateAssemblyReference("Microsoft.CSharp"), // new MetadataFileReference(typeof(IModel).Assembly.Location) // }); // using (var output = new MemoryStream()) // { // var emitResult = comp.Emit(output); // foreach (var diagnostic in emitResult.Diagnostics) // { // Console.WriteLine(diagnostic.ToString()); // } // byte[] compiledAssembly = output.ToArray(); // var assembly = Assembly.Load(compiledAssembly); // var types = assembly.GetTypes(); // var type= types.FirstOrDefault(); // if (type != null) // { // dynamic model = assembly.CreateInstance(type.FullName); // var partScript = (IPartScript) model; // if (partScript != null) // { // var m = partScript.Generate(); // Console.WriteLine(m); // } // } // } // var t = new CodeExecuter(); // t.Execute(s); //var o = new OctreeModel(new OctreeNode(Vect3.Zero, 16, 8),"testing"); //var render = new TestModelRenderManager(); //render.Fetch(o) //Console.WriteLine("Testing"); //var features = new FeatureFactory(); //var origin = new DatumOrigin(); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumPlane(new IFeatureReference[] { new FeatureReference(origin) })); //features.Add(new DatumOrigin()); //features.Add(origin); //Console.WriteLine("Running Regen"); //features.RegenerateAsync(); //Console.WriteLine("Run Regen"); Console.ReadLine(); }