Exemple #1
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()));
        }
        // </SnippetFileCheck>

        // <SnippetRepairModel>
        private async void OnFixClick(object sender, RoutedEventArgs e)
        {
            // read the loaded file's data as a data stream
            IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);

            // assign a Printing3DModel to this data stream
            Printing3DModel model = await package.LoadModelFromPackageAsync(fileStream);

            // use Printing3DModel's repair function
            OutputTextBlock.Text = "repairing model";
            var data = model.RepairAsync();

            // </SnippetRepairModel>

            // <SnippetSaveModel>
            // save model to this class' Printing3D3MFPackage
            OutputTextBlock.Text = "saving model to 3MF package";
            await package.SaveModelToPackageAsync(model);
        }
Exemple #3
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);
        }
Exemple #4
0
        private async void LoadPackageFromFile()
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);

            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".3mf");

            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            rootPage.NotifyUser("Loading...", NotifyType.StatusMessage);

            var package = new Printing3D3MFPackage();

            using (var fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                var model = await package.LoadModelFromPackageAsync(fileStream);

                rootPage.NotifyUser("Repairing...", NotifyType.StatusMessage);
                await model.RepairAsync();

                rootPage.NotifyUser("Saving...", NotifyType.StatusMessage);
                await package.SaveModelToPackageAsync(model);
                await FixTextureContentTypeAsync(package);
            }

            currentPackage = package;
            rootPage.NotifyUser("Package created from file.", NotifyType.StatusMessage);
            EnablePackageOperationButtons();
        }
        private async void LoadPackageFromFile()
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".3mf");

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file == null)
            {
                return;
            }

            rootPage.NotifyUser("Loading...", NotifyType.StatusMessage);

            var package = new Printing3D3MFPackage();
            using (var fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                var model = await package.LoadModelFromPackageAsync(fileStream);
                rootPage.NotifyUser("Repairing...", NotifyType.StatusMessage);
                await model.RepairAsync();
                rootPage.NotifyUser("Saving...", NotifyType.StatusMessage);
                await package.SaveModelToPackageAsync(model);
                await FixTextureContentTypeAsync(package);
            }

            currentPackage = package;
            rootPage.NotifyUser("Package created from file.", NotifyType.StatusMessage);
            EnablePackageOperationButtons();
        }