Пример #1
0
        private async void SavePackage()
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);

            FileSavePicker savePicker = new FileSavePicker();

            savePicker.DefaultFileExtension   = ".3mf";
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.FileTypeChoices.Add("3MF File", new[] { ".3mf" });
            var storageFile = await savePicker.PickSaveFileAsync();

            if (storageFile == null)
            {
                return;
            }

            using (var stream = await currentPackage.SaveAsync())
            {
                stream.Seek(0);
                using (var dataReader = new DataReader(stream))
                {
                    await dataReader.LoadAsync((uint)stream.Size);

                    var buffer = dataReader.ReadBuffer((uint)stream.Size);
                    await FileIO.WriteBufferAsync(storageFile, buffer);
                }
            }

            rootPage.NotifyUser("Saved", NotifyType.StatusMessage);
        }
        //<SnippetSaveTo3mf>
        private async void SaveTo3mf(Printing3D3MFPackage localPackage)
        {
            // prompt the user to choose a location to save the file to
            FileSavePicker savePicker = new FileSavePicker();

            savePicker.DefaultFileExtension   = ".3mf";
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.FileTypeChoices.Add("3MF File", new[] { ".3mf" });
            var storageFile = await savePicker.PickSaveFileAsync();

            if (storageFile == null)
            {
                return;
            }

            // save the 3MF Package to an IRandomAccessStream
            using (var stream = await localPackage.SaveAsync()) {
                // go to the beginning of the stream
                stream.Seek(0);

                // read from the file stream and write to a buffer
                using (var dataReader = new DataReader(stream)) {
                    await dataReader.LoadAsync((uint)stream.Size);

                    var buffer = dataReader.ReadBuffer((uint)stream.Size);

                    // write from the buffer to the storagefile specified
                    await FileIO.WriteBufferAsync(storageFile, buffer);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Fixes 3D printing file stream.
        /// </summary>
        /// <param name="inputStream">The input file to be fixed as a Stream object.</param>
        /// <returns>The fixed file as a Stream.</returns>
        public async Task <Stream> FixAsync(Stream inputStream)
        {
            InputStream = inputStream;

            // 1. LoadModelFromPackageAsync accepts IRandomAccessStream and uses stream cloning internally
            // 2. WindowsRuntimeStreamExtensions.AsRandomStream converts Stream to IRandomAccessStream, but the resulting stream doesn't support cloning
            // 3. InMemoryRandomAccessStream does support cloning. So we needed a way to go from Stream to InMemoryRandomAccessStream
            // 4. Solution: Copy Stream to MemoryStream. Write to InMemoryRandomAccessStream via WriteAsync, which accepts IBuffer
            //    To get IBuffer, we first need to get the memoryStream Bytes with ToArray() and then get IBuffer using the
            //    System.Runtime.InteropServices.WindowsRuntime AsBuffer90 extension method.
            //    We then pass the InMemoryRandomAccessStream object to  LoadModelFromPackageAsync.

            using var memoryStream = new MemoryStream();
            await InputStream.CopyToAsync(memoryStream);

            using InMemoryRandomAccessStream memoryRandomAccessStream = new InMemoryRandomAccessStream();
            await memoryRandomAccessStream.WriteAsync(memoryStream.ToArray().AsBuffer());

            var package = new Printing3D3MFPackage();
            var model   = await package.LoadModelFromPackageAsync(memoryRandomAccessStream);

            await model.RepairAsync();

            await package.SaveModelToPackageAsync(model);

            return(WindowsRuntimeStreamExtensions.AsStream(await package.SaveAsync()));
        }
Пример #4
0
        /// <summary>
        /// Fixes 3D printing inputFile and returns path to fixed file.
        /// </summary>
        /// <param name="inputFile">The absolute path to the file to be fixed.</param>
        /// <param name="outputFile">The absolute path to the output fixed file. Defaults to appending '_fixed' to the InputFile file name.</param>
        /// <returns>The absolute path to the fixed file.</returns>
        public async Task <string> FixAsync(string inputFile, string outputFile = "")
        {
            InputFile  = inputFile;
            OutputFile = outputFile;

            var package = new Printing3D3MFPackage();

            using var stream = await FileRandomAccessStream.OpenAsync(InputFile, FileAccessMode.ReadWrite);

            var model = await package.LoadModelFromPackageAsync(stream);

            await model.RepairAsync();

            await package.SaveModelToPackageAsync(model);

            using var outstream = WindowsRuntimeStreamExtensions.AsStream(await package.SaveAsync());
            using var outfile   = File.Create(OutputFile);

            outstream.Seek(0, SeekOrigin.Begin);
            outstream.CopyTo(outfile);

            return(OutputFile);
        }