From 342a9b15733f71d700e040fe36974481e5d8fb82 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 9 Aug 2017 11:07:03 -0700 Subject: [PATCH] Revise shared export implementation to support Amf --- CustomWidgets/ExportPrintItemPage.cs | 102 ++++++++++++++------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/CustomWidgets/ExportPrintItemPage.cs b/CustomWidgets/ExportPrintItemPage.cs index a9ef57b8d..3b91eef86 100644 --- a/CustomWidgets/ExportPrintItemPage.cs +++ b/CustomWidgets/ExportPrintItemPage.cs @@ -55,30 +55,24 @@ namespace MatterHackers.MatterControl var commonMargin = new BorderDouble(4, 2); + // put in stl export + RadioButton exportAsStlButton = new RadioButton("Export as".Localize() + " STL", textColor: ActiveTheme.Instance.PrimaryTextColor); + exportAsStlButton.Name = "Export as STL button"; + exportAsStlButton.Margin = commonMargin; + exportAsStlButton.HAnchor = HAnchor.Left; + exportAsStlButton.Cursor = Cursors.Hand; + exportAsStlButton.DebugShowBounds = true; + + // put in amf export + RadioButton exportAsAmfButton = new RadioButton("Export as".Localize() + " AMF", textColor: ActiveTheme.Instance.PrimaryTextColor); + exportAsAmfButton.Name = "Export as AMF button"; + exportAsAmfButton.Margin = commonMargin; + exportAsAmfButton.HAnchor = HAnchor.Left; + exportAsAmfButton.Cursor = Cursors.Hand; + if (modelCanBeExported) { - // put in stl export - RadioButton exportAsStlButton = new RadioButton("Export as".Localize() + " STL", textColor: ActiveTheme.Instance.PrimaryTextColor); - exportAsStlButton.Name = "Export as STL button"; - exportAsStlButton.Margin = commonMargin; - exportAsStlButton.HAnchor = HAnchor.Left; - exportAsStlButton.Cursor = Cursors.Hand; - exportAsStlButton.CheckedStateChanged += (s, e) => - { - // Set in stl mode - }; contentRow.AddChild(exportAsStlButton); - - // put in amf export - RadioButton exportAsAmfButton = new RadioButton("Export as".Localize() + " AMF", textColor: ActiveTheme.Instance.PrimaryTextColor); - exportAsAmfButton.Name = "Export as AMF button"; - exportAsAmfButton.Margin = commonMargin; - exportAsAmfButton.HAnchor = HAnchor.Left; - exportAsAmfButton.Cursor = Cursors.Hand; - exportAsAmfButton.Click += (s, e) => - { - // Set in stl mode - }; contentRow.AddChild(exportAsAmfButton); } @@ -231,37 +225,59 @@ namespace MatterHackers.MatterControl var exportButton = textImageButtonFactory.Generate("Export".Localize()); exportButton.Click += (s, e) => { + string fileTypeFilter = ""; + string targetExtension = ""; - //FileDialog.SaveFileDialog( - // new SaveFileDialogParams("Save as AMF|*.amf", initialDirectory: documentsPath) - // { - // Title = "MatterControl: Export File", - // ActionButtonLabel = "Export", - // FileName = printItemWrapper.Name - // }, - // (saveParams) => - // { - // Task.Run(() => SaveAmf(saveParams.FileName)); - // }); + if (exportAsStlButton.Checked) + { + fileTypeFilter = "Save as STL|*.stl"; + targetExtension = ".stl"; + } + else if (exportAsAmfButton.Checked) + { + fileTypeFilter = "Save as AMF|*.amf"; + targetExtension = ".amf"; + } this.Parent.CloseOnIdle(); UiThread.RunOnIdle(() => { string title = "MatterControl: " + "Export File".Localize(); FileDialog.SaveFileDialog( - new SaveFileDialogParams("Save as STL|*.stl") + new SaveFileDialogParams(fileTypeFilter) { Title = title, - ActionButtonLabel = "Export", + ActionButtonLabel = "Export".Localize(), FileName = printItemWrapper.Name }, (saveParams) => { - if (saveParams.FileName != null) + string savePath = saveParams.FileName; + + if (!string.IsNullOrEmpty(savePath)) { Task.Run(() => { - if (SaveStl(new FileSystemFileItem(printItemWrapper.FileLocation), saveParams.FileName)) + string extension = Path.GetExtension(savePath); + if (extension != targetExtension) + { + savePath += targetExtension; + } + + var sourceContent = new FileSystemFileItem(printItemWrapper.FileLocation); + + bool result = false; + + if (exportAsStlButton.Checked) + { + result = SaveStl(sourceContent, savePath); + } + else if (exportAsAmfButton.Checked) + { + result = SaveAmf(sourceContent, savePath); + } + + if (result) { ShowFileIfRequested(saveParams.FileName); } @@ -372,13 +388,6 @@ namespace MatterHackers.MatterControl { if (!string.IsNullOrEmpty(filePathToSave)) { - string extension = Path.GetExtension(filePathToSave); - if (extension == "") - { - File.Delete(filePathToSave); - filePathToSave += ".amf"; - } - if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true); @@ -405,13 +414,6 @@ namespace MatterHackers.MatterControl { if (!string.IsNullOrEmpty(filePathToSave)) { - string extension = Path.GetExtension(filePathToSave); - if (extension == "") - { - File.Delete(filePathToSave); - filePathToSave += ".stl"; - } - if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true);