예제 #1
0
        public static void ExportMesh(string path, Mesh mesh)
        {
            var facets = DeconstructMesh(mesh);
            var bytes  = BinaryStl.ToBytes(facets);

            File.WriteAllBytes(path, bytes);
        }
예제 #2
0
        public static async Task <(Mesh mesh, string fileHash)> ImportMeshAsync(
            string fileName,
            byte[] fileBytes,
            bool centerVertices = true,
            bool computeHash    = true)
        {
            if (fileBytes.Length < 112)
            {
                throw new InvalidDataException($"File `{fileName}` is too small to be any kind of valid STL.");
            }

            var isBinary = BinaryStl.IsBinary(fileBytes);

            Task <string> ComputeHash()
            {
                return(computeHash
                    ? Task.Run(() => StlImporter.ComputeHash(fileBytes))
                       .Timed("Computing hash of {0}", fileName)
                    : Task.FromResult((string)null));
            }

            Facet[]       facets;
            Task <string> computeHashTask;

            if (isBinary)
            {
                computeHashTask = ComputeHash();
                facets          = await Task.Run(() => BinaryStl.FromBytes(fileBytes))
                                  .Timed("Reading binary stl {0}", fileName);
            }
            else
            {
                facets = await Task.Run(() => TextualStl.Import(fileBytes, out _).ToArray())
                         .Timed("Reading textual stl {0}", fileName);

                var convertToBinary = Task.Run(() => fileBytes = BinaryStl.ToBytes(facets))
                                      .Timed("Converting {0} to binary stl", fileName);

                computeHashTask = Task.Run(async() =>
                {
                    await convertToBinary;
                    return(await ComputeHash());
                });
            }

            var mesh = await CreateMeshFromFacetsAsync(facets, centerVertices, fileName);

            return(mesh, await computeHashTask);
        }