Ejemplo n.º 1
0
 void ThreadFunc()
 {
     Status = StandardMeshWriter.WriteFile(Filename, Meshes, options);
     if (CompletionF != null)
     {
         CompletionF(Status);
     }
 }
Ejemplo n.º 2
0
        public static void WriteDebugMesh(IMesh mesh, string sPath)
        {
            WriteOptions options = WriteOptions.Defaults;

            options.bWriteGroups = true;
            StandardMeshWriter.WriteFile(sPath, new List <WriteMesh>()
            {
                new WriteMesh(mesh)
            }, options);
        }
Ejemplo n.º 3
0
    private IEnumerator ExportOneFile(Generate generate, string path, string name, int num)
    {
        var mesh     = generate.mesh;
        var filename = path + "/" + name + num + ".stl";

        StandardMeshWriter.WriteFile(filename,
                                     new List <WriteMesh>()
        {
            new WriteMesh(mesh)
        }, WriteOptions.Defaults);
        yield return(null);
    }
Ejemplo n.º 4
0
        public static void WriteObj(FileInfo file, IGeometryData geo)
        {
            var meshes = new List <WriteMesh>();
            var mesh   = new SimpleMesh();

            var map     = new Dictionary <Vector3, int>();
            var indeces = new List <int>();

            for (var index = 0; index < geo.Positions.Length; index++)
            {
                var v = geo.Positions[index];
                if (!map.ContainsKey(v))
                {
                    map.Add(v, map.Count);
                }
            }
            for (var index = 0; index < geo.Indices.Length; index++)
            {
                var i = geo.Indices[index];
                var v = geo.Positions[i];
                indeces.Add(map[v]);
            }


            var points = map.Keys.ToArray();
            var pcount = points.Length * 3;
            var pp     = new double[pcount];
            var pindex = 0;

            for (var index = 0; index < points.Length; index++)
            {
                var v = points[index];
                pp[pindex++] = v.X;
                pp[pindex++] = v.Y;
                pp[pindex++] = v.Z;
            }
            mesh.Initialize(new VectorArray3d(pp), new VectorArray3i(indeces.ToArray()));

            meshes.Add(new WriteMesh(mesh, $"d3dlab export"));

            StandardMeshWriter.WriteFile(file.FullName, meshes, WriteOptions.Defaults);
        }
Ejemplo n.º 5
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            this.Message = "." + type.ToString();

            DMesh3_goo goo  = null;
            string     path = "";
            string     file = "";

            DA.GetData(0, ref goo);
            DA.GetData(1, ref path);
            DA.GetData(2, ref file);
            DMesh3 mesh = new DMesh3(goo.Value);


            IOWriteResult result = StandardMeshWriter.WriteFile(Path.Combine(path, file) + "." + type.ToString(), new List <WriteMesh>()
            {
                new WriteMesh(mesh)
            }, WriteOptions.Defaults);

            DA.SetData(0, Path.Combine(path, file) + "." + type.ToString());
        }
Ejemplo n.º 6
0
        public static void test_simple_obj()
        {
            string cwd = System.IO.Directory.GetCurrentDirectory();

            System.Console.WriteLine("MeshIOTests : test_simple_obj() starting");

            //SimpleMeshBuilder builder = new SimpleMeshBuilder();
            DMesh3Builder      builder = new DMesh3Builder();
            StandardMeshReader reader  = new StandardMeshReader();

            reader.MeshBuilder = builder;
            var readResult = reader.Read(Program.TEST_FILES_PATH + "socket_with_groups.obj", new ReadOptions());

            System.Console.WriteLine("read complete");

            if (readResult.code != IOCode.Ok)
            {
                System.Console.WriteLine("read failed : " + readResult.message);
                throw new Exception("failed");
            }

            List <WriteMesh> meshes = new List <WriteMesh>();

            foreach (IMesh m in builder.Meshes)
            {
                meshes.Add(new WriteMesh(m));
            }
            var writeResult = StandardMeshWriter.WriteFile(Program.TEST_OUTPUT_PATH + "temp_write.obj",
                                                           meshes, new WriteOptions());

            System.Console.WriteLine("write complete");

            if (writeResult.code != IOCode.Ok)
            {
                System.Console.WriteLine("write failed : " + writeResult.message);
                throw new Exception("f**k");
            }
        }
Ejemplo n.º 7
0
        public static void WriteDebugMeshAndMarkers(IMesh mesh, List <Vector3D> Markers, string sPath)
        {
            WriteOptions options = WriteOptions.Defaults;

            options.bWriteGroups = true;
            List <WriteMesh> meshes = new List <WriteMesh>()
            {
                new WriteMesh(mesh)
            };
            double size = BoundsUtil.Bounds(mesh).Diagonal.Length * 0.01f;

            foreach (Vector3D v in Markers)
            {
                TrivialBox3Generator boxgen = new TrivialBox3Generator();
                boxgen.Box = new Box3d(v, size * Vector3D.One);
                boxgen.Generate();
                DMesh3 m = new DMesh3();
                boxgen.MakeMesh(m);
                meshes.Add(new WriteMesh(m));
            }

            StandardMeshWriter.WriteFile(sPath, meshes, options);
        }
Ejemplo n.º 8
0
        void SaveMesh(PlateConfig config, [CallerMemberName] string fileName = null)
        {
            var output = Path.Combine(SolutionFolder(), "output");

            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }

            var plate = new PlateGenerator(config);

            var meshGenerator = plate.Generate();
            var mesh          = meshGenerator.MakeSimpleMesh();

            fileName = fileName != null ? $"{fileName}.stl" : $"output.stl";

            IOWriteResult result = StandardMeshWriter.WriteFile(
                Path.Combine(output, fileName),
                new List <WriteMesh>()
            {
                new WriteMesh(mesh)
            },
                WriteOptions.Defaults);
        }
Ejemplo n.º 9
0
        public virtual ExportStatus Export(FScene scene, string filename)
        {
            int[]            vertexMap = new int[2048]; // temp
            List <WriteMesh> vMeshes   = new List <WriteMesh>();

            if (WriteFaceGroups)
            {
                throw new Exception("SceneMeshExporter.Export: writing face groups has not yet been implemented!");
            }

            // extract all the mesh data we want to export
            foreach (SceneObject so in scene.SceneObjects)
            {
                if (so.IsTemporary || so.IsSurface == false || SceneUtil.IsVisible(so) == false)
                {
                    continue;
                }
                if (SOFilterF != null && SOFilterF(so) == false)
                {
                    continue;
                }

                // if this SO has an internal mesh we can just copy, use it
                if (so is DMeshSO)
                {
                    DMeshSO meshSO = so as DMeshSO;

                    // todo: flags

                    // make a copy of mesh
                    DMesh3 m = new DMesh3(meshSO.Mesh, true);

                    // transform to scene coords and swap left/right
                    foreach (int vid in m.VertexIndices())
                    {
                        Vector3f v = (Vector3f)m.GetVertex(vid);
                        v = SceneTransforms.ObjectToScene(meshSO, v);
                        v = UnityUtil.SwapLeftRight(v);
                        m.SetVertex(vid, v);
                    }
                    m.ReverseOrientation();

                    vMeshes.Add(new WriteMesh(m, so.Name));
                }


                // Look for lower-level fGameObject items to export. By default
                // this is anything with a MeshFilter, override CollectGOChildren
                // or use GOFilterF to add restrictions
                List <fGameObject> vExports = CollectGOChildren(so);
                if (vExports.Count > 0)
                {
                    SimpleMesh m = new SimpleMesh();
                    m.Initialize(WriteNormals, WriteVertexColors, WriteUVs, WriteFaceGroups);
                    int groupCounter = 1;

                    foreach (fGameObject childgo in vExports)
                    {
                        if (GOFilterF != null && GOFilterF(so, childgo) == false)
                        {
                            continue;
                        }

                        if (AppendGOMesh(childgo, m, vertexMap, scene, groupCounter))
                        {
                            groupCounter++;
                        }
                    }

                    vMeshes.Add(new WriteMesh(m, so.Name));
                }
            }


            // ok, we are independent of Scene now and can write in bg thread
            if (WriteInBackgroundThreads)
            {
                ExportStatus status = new ExportStatus()
                {
                    Exporter = this, IsComputing = true
                };
                WriteOptions useOptions = Options;
                useOptions.ProgressFunc = (cur, max) => {
                    status.Progress    = cur;
                    status.MaxProgress = max;
                };
                BackgroundWriteThread t = new BackgroundWriteThread()
                {
                    Meshes      = vMeshes, options = useOptions, Filename = filename,
                    CompletionF = (result) => {
                        LastWriteStatus         = result.code;
                        LastErrorMessage        = result.message;
                        status.LastErrorMessage = result.message;
                        status.Ok          = (result.code == IOCode.Ok);
                        status.IsComputing = false;
                        if (BackgroundWriteCompleteF != null)
                        {
                            BackgroundWriteCompleteF(this, status);
                        }
                    }
                };
                t.Start();
                return(status);
            }
            else
            {
                IOWriteResult result = StandardMeshWriter.WriteFile(filename, vMeshes, Options);
                LastWriteStatus  = result.code;
                LastErrorMessage = result.message;
                return(new ExportStatus()
                {
                    Exporter = this, IsComputing = false,
                    Ok = (result.code == IOCode.Ok),
                    LastErrorMessage = result.message
                });
            }
        }
Ejemplo n.º 10
0
        public ExportStatus Export(FScene s, string filename)
        {
            List <WriteMesh> vMeshes = new List <WriteMesh>();

            if (WriteFaceGroups)
            {
                throw new Exception("SceneMeshExporter.Export: writing face groups has not yet been implemented!");
            }

            foreach (SceneObject so in s.SceneObjects)
            {
                if (so.IsTemporary)
                {
                    continue;
                }

                SimpleMesh m = new SimpleMesh();
                m.Initialize(WriteNormals, WriteVertexColors, WriteUVs, WriteFaceGroups);
                int groupCounter = 1;

                GameObject rootgo = so.RootGameObject;

                int[] vertexMap = new int[2048];
                foreach (GameObject childgo in rootgo.Children())
                {
                    MeshFilter filter = childgo.GetComponent <MeshFilter>();
                    if (filter == null || filter.mesh == null)
                    {
                        continue;
                    }
                    if (GOFilterF != null && GOFilterF(so, childgo) == false)
                    {
                        continue;
                    }

                    Mesh      curMesh  = filter.sharedMesh;
                    Vector3[] vertices = curMesh.vertices;
                    Vector3[] normals  = (WriteNormals) ? curMesh.normals : null;
                    Color[]   colors   = (WriteVertexColors) ? curMesh.colors : null;
                    Vector2[] uvs      = (WriteUVs) ? curMesh.uv : null;

                    if (vertexMap.Length < curMesh.vertexCount)
                    {
                        vertexMap = new int[curMesh.vertexCount * 2];
                    }

                    for (int i = 0; i < curMesh.vertexCount; ++i)
                    {
                        NewVertexInfo vi = new NewVertexInfo();
                        vi.bHaveN = WriteNormals; vi.bHaveC = WriteVertexColors; vi.bHaveUV = WriteUVs;

                        Vector3 v = vertices[i];
                        // local to world
                        v = filter.gameObject.transform.TransformPoint(v);
                        // world back to scene
                        vi.v = UnityUtil.SwapLeftRight(s.RootGameObject.transform.InverseTransformPoint(v));

                        if (WriteNormals)
                        {
                            Vector3 n = normals[i];
                            n    = filter.gameObject.transform.TransformDirection(n);
                            vi.n = UnityUtil.SwapLeftRight(s.RootGameObject.transform.InverseTransformDirection(n));
                        }
                        if (WriteVertexColors)
                        {
                            vi.c = colors[i];
                        }
                        if (WriteUVs)
                        {
                            vi.uv = uvs[i];
                        }

                        vertexMap[i] = m.AppendVertex(vi);
                    }

                    int[] triangles  = curMesh.triangles;
                    int   nTriangles = triangles.Length / 3;
                    for (int i = 0; i < nTriangles; ++i)
                    {
                        int a = vertexMap[triangles[3 * i]];
                        int b = vertexMap[triangles[3 * i + 1]];
                        int c = vertexMap[triangles[3 * i + 2]];
                        m.AppendTriangle(a, c, b, groupCounter);  // TRI ORIENTATION IS REVERSED HERE!!
                    }
                    groupCounter++;
                }

                vMeshes.Add(new WriteMesh(m, so.Name));
            }


            if (WriteInBackgroundThreads)
            {
                ExportStatus status = new ExportStatus()
                {
                    Exporter = this, IsComputing = true
                };
                WriteOptions useOptions = Options;
                useOptions.ProgressFunc = (cur, max) => {
                    status.Progress    = cur;
                    status.MaxProgress = max;
                };
                BackgroundWriteThread t = new BackgroundWriteThread()
                {
                    Meshes      = vMeshes, options = useOptions, Filename = filename,
                    CompletionF = (result) => {
                        LastWriteStatus         = result.code;
                        LastErrorMessage        = result.message;
                        status.LastErrorMessage = result.message;
                        status.Ok          = (result.code == IOCode.Ok);
                        status.IsComputing = false;
                    }
                };
                t.Start();
                return(status);
            }
            else
            {
                IOWriteResult result = StandardMeshWriter.WriteFile(filename, vMeshes, Options);
                LastWriteStatus  = result.code;
                LastErrorMessage = result.message;
                return(new ExportStatus()
                {
                    Exporter = this, IsComputing = false,
                    Ok = (result.code == IOCode.Ok),
                    LastErrorMessage = result.message
                });
            }
        }
Ejemplo n.º 11
0
        public static void WriteObj(FileInfo file, IEnumerable <IFileGeometry3D> geometries)
        {
            var meshes = new List <WriteMesh>();
            var mesh   = new SimpleMesh();

            var map     = new Dictionary <Vector3, int>();
            var indeces = new List <int>();

            foreach (var geo in geometries)
            {
                //var pcount = geo.Positions.Count * 3;
                //var pp = new double[pcount];
                //var pindex = 0;
                //for(var index =0; index < geo.Positions.Count; index++) {
                //    var v = geo.Positions[index];
                //    pp[pindex++] = v.X;
                //    pp[pindex++] = v.Y;
                //    pp[pindex++] = v.Z;
                //}
                //mesh.Initialize(new VectorArray3d(pp), new VectorArray3i(geo.Indices.ToArray()));
                for (var index = 0; index < geo.Positions.Count; index++)
                {
                    var v = geo.Positions[index];
                    if (!map.ContainsKey(v))
                    {
                        map.Add(v, map.Count);
                    }
                }
                for (var index = 0; index < geo.Indices.Count; index++)
                {
                    var i = geo.Indices[index];
                    var v = geo.Positions[i];
                    indeces.Add(map[v]);
                }
            }

            var points = map.Keys.ToArray();
            var pcount = points.Length * 3;
            var pp     = new double[pcount];
            var pindex = 0;

            for (var index = 0; index < points.Length; index++)
            {
                var v = points[index];
                pp[pindex++] = v.X;
                pp[pindex++] = v.Y;
                pp[pindex++] = v.Z;
            }
            mesh.Initialize(new VectorArray3d(pp), new VectorArray3i(indeces.ToArray()));

            meshes.Add(new WriteMesh(mesh, $"d3dlab export"));

            StandardMeshWriter.WriteFile(file.FullName, meshes, WriteOptions.Defaults);

            //var obj = new OBJWriter();
            //System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            //using (var stream = File.Open(file.FullName, FileMode.Create)) {
            //    using (var writer = new StreamWriter(stream, Encoding.UTF8)) {
            //        obj.Write(writer, meshes, WriteOptions.Defaults);
            //    }
            //}
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            bool INTERACTIVE = true;


            //string inFile = "c:\\scratch\\test_bunny.obj";
            //string inFile = "c:\\scratch\\bunny_100k.obj";
            //string inFile = "c:\\scratch\\test_bunny_ascii.stl";
            string inFile  = "c:\\scratch\\test_bunny_binary.stl";
            string outFile = "c:\\scratch\\test_bunny_out.obj";

            ReadOptions read_options = new ReadOptions();

            read_options.ReadMaterials = false;
            StandardMeshReader reader      = new StandardMeshReader();
            DMesh3Builder      builder     = new DMesh3Builder();
            IOReadResult       read_result = StandardMeshReader.ReadFile(inFile, read_options, builder);

            if (read_result.code != IOCode.Ok)
            {
                System.Console.WriteLine("Error reading " + inFile + " : " + read_result.message);
                if (INTERACTIVE)
                {
                    System.Console.WriteLine("press enter key to exit");
                    System.Console.ReadLine();
                }
                return;
            }


            WriteOptions write_options = new WriteOptions();

            write_options.bCombineMeshes = true;
            write_options.bWriteBinary   = true;

            List <WriteMesh> outMeshes = new List <WriteMesh>();

            foreach (DMesh3 m in builder.Meshes)
            {
                outMeshes.Add(new WriteMesh(m));
            }

            IOWriteResult write_result = StandardMeshWriter.WriteFile(outFile, outMeshes, write_options);

            if (write_result.code != IOCode.Ok)
            {
                System.Console.WriteLine("Error writing " + outFile + " : " + write_result.message);
                if (INTERACTIVE)
                {
                    System.Console.WriteLine("press enter key to exit");
                    System.Console.ReadLine();
                }
                return;
            }


            if (INTERACTIVE)
            {
                System.Console.WriteLine("Done conversion, press enter key to exit");
                System.Console.ReadLine();
            }
        }