2017-08-18 18:13:15 -07:00
|
|
|
|
/*
|
2018-04-13 18:34:58 -07:00
|
|
|
|
Copyright (c) 2018, Lars Brubaker, John Lewin
|
2017-08-18 18:13:15 -07:00
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-18 18:15:10 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2017-08-11 23:13:19 -07:00
|
|
|
|
using System.Linq;
|
2017-07-18 18:15:10 -07:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using MatterHackers.Agg;
|
2017-08-20 02:34:39 -07:00
|
|
|
|
using MatterHackers.Agg.Platform;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.Agg.UI;
|
|
|
|
|
|
using MatterHackers.Localizations;
|
2014-06-19 15:55:20 -07:00
|
|
|
|
using MatterHackers.MatterControl.CustomWidgets;
|
2017-08-09 07:30:07 -07:00
|
|
|
|
using MatterHackers.MatterControl.Library;
|
2017-08-11 23:53:27 -07:00
|
|
|
|
using MatterHackers.MatterControl.Library.Export;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
2017-11-08 15:56:37 -08:00
|
|
|
|
public class ExportPrintItemPage : DialogPage
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
|
|
|
|
|
private CheckBox showInFolderAfterSave;
|
|
|
|
|
|
|
2017-08-10 21:55:16 -07:00
|
|
|
|
private Dictionary<RadioButton, IExportPlugin> exportPluginButtons;
|
2017-08-11 23:13:19 -07:00
|
|
|
|
private IEnumerable<ILibraryItem> libraryItems;
|
2017-08-09 14:40:48 -07:00
|
|
|
|
|
2018-09-07 10:25:29 -07:00
|
|
|
|
bool centerOnBed;
|
|
|
|
|
|
|
2018-11-12 09:32:08 -08:00
|
|
|
|
public ExportPrintItemPage(IEnumerable<ILibraryItem> libraryItems, bool centerOnBed, PrinterConfig printer)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-09-07 10:25:29 -07:00
|
|
|
|
this.centerOnBed = centerOnBed;
|
2017-08-23 15:51:29 -07:00
|
|
|
|
this.WindowTitle = "Export File".Localize();
|
2017-12-02 22:07:45 -08:00
|
|
|
|
this.HeaderText = "Export selection to".Localize() + ":";
|
2017-08-23 15:51:29 -07:00
|
|
|
|
|
2017-08-11 23:13:19 -07:00
|
|
|
|
this.libraryItems = libraryItems;
|
2015-11-13 18:06:44 -08:00
|
|
|
|
this.Name = "Export Item Window";
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-08-08 20:31:34 -07:00
|
|
|
|
var commonMargin = new BorderDouble(4, 2);
|
|
|
|
|
|
|
2017-08-19 09:50:24 -07:00
|
|
|
|
bool isFirstItem = true;
|
|
|
|
|
|
|
2017-08-09 13:24:49 -07:00
|
|
|
|
// GCode export
|
2018-04-13 18:34:58 -07:00
|
|
|
|
exportPluginButtons = new Dictionary<RadioButton, IExportPlugin>();
|
2017-08-09 13:24:49 -07:00
|
|
|
|
|
2018-04-13 18:34:58 -07:00
|
|
|
|
foreach (IExportPlugin plugin in PluginFinder.CreateInstancesOf<IExportPlugin>().OrderBy(p => p.ButtonText))
|
|
|
|
|
|
{
|
|
|
|
|
|
plugin.Initialize(printer);
|
2018-02-13 18:11:01 -08:00
|
|
|
|
|
2018-04-13 18:34:58 -07:00
|
|
|
|
// Skip plugins which are invalid for the current printer
|
|
|
|
|
|
if (!plugin.Enabled)
|
2016-03-17 08:57:58 -07:00
|
|
|
|
{
|
2018-12-05 11:33:49 -08:00
|
|
|
|
if (!string.IsNullOrEmpty(plugin.DisabledReason))
|
2018-11-29 17:35:08 -08:00
|
|
|
|
{
|
|
|
|
|
|
// add a message to let us know why not enabled
|
|
|
|
|
|
var disabledPluginButton = new RadioButton(new RadioImageWidget(plugin.ButtonText, theme.TextColor, plugin.Icon))
|
|
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Left,
|
|
|
|
|
|
Margin = commonMargin,
|
|
|
|
|
|
Cursor = Cursors.Hand,
|
|
|
|
|
|
Name = plugin.ButtonText + " Button",
|
|
|
|
|
|
Enabled = false
|
|
|
|
|
|
};
|
|
|
|
|
|
contentRow.AddChild(disabledPluginButton);
|
2018-12-05 11:33:49 -08:00
|
|
|
|
contentRow.AddChild(new TextWidget("Disabled: {0}".Localize().FormatWith(plugin.DisabledReason), textColor: theme.PrimaryAccentColor)
|
2018-11-29 17:35:08 -08:00
|
|
|
|
{
|
|
|
|
|
|
Margin = new BorderDouble(left: 80),
|
|
|
|
|
|
HAnchor = HAnchor.Left
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2018-04-13 18:34:58 -07:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-02-13 18:11:01 -08:00
|
|
|
|
|
2018-04-13 18:34:58 -07:00
|
|
|
|
// Create export button for each plugin
|
2018-11-03 09:13:07 -07:00
|
|
|
|
var pluginButton = new RadioButton(new RadioImageWidget(plugin.ButtonText, theme.TextColor, plugin.Icon))
|
2018-04-13 18:34:58 -07:00
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Left,
|
|
|
|
|
|
Margin = commonMargin,
|
|
|
|
|
|
Cursor = Cursors.Hand,
|
|
|
|
|
|
Name = plugin.ButtonText + " Button"
|
|
|
|
|
|
};
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(pluginButton);
|
2018-02-13 18:30:17 -08:00
|
|
|
|
|
2018-04-13 18:34:58 -07:00
|
|
|
|
if (isFirstItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
pluginButton.Checked = true;
|
|
|
|
|
|
isFirstItem = false;
|
|
|
|
|
|
}
|
2017-08-19 09:50:24 -07:00
|
|
|
|
|
2018-04-13 18:34:58 -07:00
|
|
|
|
if (plugin is IExportWithOptions pluginWithOptions)
|
|
|
|
|
|
{
|
|
|
|
|
|
var optionPanel = pluginWithOptions.GetOptionsPanel();
|
|
|
|
|
|
if (optionPanel != null)
|
2017-08-11 23:13:19 -07:00
|
|
|
|
{
|
2018-04-13 18:34:58 -07:00
|
|
|
|
optionPanel.HAnchor = HAnchor.Stretch;
|
|
|
|
|
|
optionPanel.VAnchor = VAnchor.Fit;
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(optionPanel);
|
2017-03-06 17:32:46 -08:00
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2018-04-13 18:34:58 -07:00
|
|
|
|
|
|
|
|
|
|
exportPluginButtons.Add(pluginButton, plugin);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(new VerticalSpacer());
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
|
|
|
|
|
// TODO: make this work on the mac and then delete this if
|
2017-08-20 02:34:39 -07:00
|
|
|
|
if (AggContext.OperatingSystem == OSType.Windows
|
|
|
|
|
|
|| AggContext.OperatingSystem == OSType.X11)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-11-03 09:13:07 -07:00
|
|
|
|
showInFolderAfterSave = new CheckBox("Show file in folder after save".Localize(), theme.TextColor, 10)
|
2017-08-08 20:37:13 -07:00
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Left,
|
|
|
|
|
|
Cursor = Cursors.Hand
|
|
|
|
|
|
};
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(showInFolderAfterSave);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-14 20:51:01 -07:00
|
|
|
|
var exportButton = theme.CreateDialogButton("Export".Localize());
|
2017-08-30 13:41:49 -07:00
|
|
|
|
exportButton.Name = "Export Button";
|
2017-08-09 07:45:26 -07:00
|
|
|
|
exportButton.Click += (s, e) =>
|
|
|
|
|
|
{
|
2017-08-09 11:07:03 -07:00
|
|
|
|
string fileTypeFilter = "";
|
|
|
|
|
|
string targetExtension = "";
|
2017-08-09 07:49:50 -07:00
|
|
|
|
|
2017-08-10 21:55:16 -07:00
|
|
|
|
IExportPlugin activePlugin = null;
|
2017-08-09 13:24:49 -07:00
|
|
|
|
|
2017-08-11 23:13:19 -07:00
|
|
|
|
// Loop over all plugin buttons, break on the first checked item found
|
|
|
|
|
|
foreach(var button in this.exportPluginButtons.Keys)
|
2017-08-09 11:14:38 -07:00
|
|
|
|
{
|
2017-08-11 23:13:19 -07:00
|
|
|
|
if (button.Checked)
|
2017-08-09 13:24:49 -07:00
|
|
|
|
{
|
2017-08-11 23:13:19 -07:00
|
|
|
|
activePlugin = exportPluginButtons[button];
|
|
|
|
|
|
break;
|
2017-08-09 13:24:49 -07:00
|
|
|
|
}
|
2017-08-11 23:13:19 -07:00
|
|
|
|
}
|
2017-08-09 13:24:49 -07:00
|
|
|
|
|
2017-08-11 23:13:19 -07:00
|
|
|
|
// Early exit if no plugin radio button is selected
|
|
|
|
|
|
if (activePlugin == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
2017-08-09 13:24:49 -07:00
|
|
|
|
}
|
2017-08-09 07:49:50 -07:00
|
|
|
|
|
2017-08-11 23:13:19 -07:00
|
|
|
|
fileTypeFilter = activePlugin.ExtensionFilter;
|
|
|
|
|
|
targetExtension = activePlugin.FileExtension;
|
|
|
|
|
|
|
2017-08-09 07:45:26 -07:00
|
|
|
|
this.Parent.CloseOnIdle();
|
2017-08-11 23:53:27 -07:00
|
|
|
|
|
|
|
|
|
|
if (activePlugin is FolderExport)
|
|
|
|
|
|
{
|
|
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
2017-08-20 02:34:39 -07:00
|
|
|
|
AggContext.FileDialogs.SelectFolderDialog(
|
2017-08-11 23:53:27 -07:00
|
|
|
|
new SelectFolderDialogParams("Select Location To Export Files")
|
|
|
|
|
|
{
|
|
|
|
|
|
ActionButtonLabel = "Export".Localize(),
|
2017-09-20 16:40:57 -07:00
|
|
|
|
Title = ApplicationController.Instance.ProductName + " - " + "Select A Folder".Localize()
|
2017-08-11 23:53:27 -07:00
|
|
|
|
},
|
2018-08-17 17:49:41 -07:00
|
|
|
|
(openParams) =>
|
2017-08-11 23:53:27 -07:00
|
|
|
|
{
|
2018-08-17 17:49:41 -07:00
|
|
|
|
ApplicationController.Instance.Tasks.Execute(
|
|
|
|
|
|
"Saving".Localize() + "...",
|
2018-12-05 13:48:25 -08:00
|
|
|
|
printer,
|
2018-08-17 17:49:41 -07:00
|
|
|
|
async (reporter, cancellationToken) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
string path = openParams.FolderPath;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
await activePlugin.Generate(libraryItems, path, reporter, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2017-08-11 23:53:27 -07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-09 07:45:26 -07:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
2017-09-20 16:40:57 -07:00
|
|
|
|
string title = ApplicationController.Instance.ProductName + " - " + "Export File".Localize();
|
2018-01-03 14:42:26 -08:00
|
|
|
|
string workspaceName = "Workspace " + DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss");
|
2017-08-20 02:34:39 -07:00
|
|
|
|
AggContext.FileDialogs.SaveFileDialog(
|
2017-08-09 11:07:03 -07:00
|
|
|
|
new SaveFileDialogParams(fileTypeFilter)
|
2017-08-09 07:45:26 -07:00
|
|
|
|
{
|
|
|
|
|
|
Title = title,
|
2017-08-09 11:07:03 -07:00
|
|
|
|
ActionButtonLabel = "Export".Localize(),
|
2018-01-03 14:42:26 -08:00
|
|
|
|
FileName = Path.GetFileNameWithoutExtension(libraryItems.FirstOrDefault()?.Name ?? workspaceName)
|
2017-08-09 07:45:26 -07:00
|
|
|
|
},
|
|
|
|
|
|
(saveParams) =>
|
|
|
|
|
|
{
|
2017-08-09 11:07:03 -07:00
|
|
|
|
string savePath = saveParams.FileName;
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(savePath))
|
2017-08-09 07:45:26 -07:00
|
|
|
|
{
|
2018-08-17 17:49:41 -07:00
|
|
|
|
ApplicationController.Instance.Tasks.Execute(
|
|
|
|
|
|
"Exporting".Localize() + "...",
|
2018-12-05 13:48:25 -08:00
|
|
|
|
printer,
|
2018-08-17 17:49:41 -07:00
|
|
|
|
async (reporter, cancellationToken) =>
|
2017-08-09 11:07:03 -07:00
|
|
|
|
{
|
2018-08-17 17:49:41 -07:00
|
|
|
|
string extension = Path.GetExtension(savePath);
|
|
|
|
|
|
if (extension != targetExtension)
|
|
|
|
|
|
{
|
|
|
|
|
|
savePath += targetExtension;
|
|
|
|
|
|
}
|
2017-08-09 11:07:03 -07:00
|
|
|
|
|
2019-01-04 17:09:42 -08:00
|
|
|
|
List<ValidationError> exportErrors = null;
|
2017-08-09 11:07:03 -07:00
|
|
|
|
|
2018-08-17 17:49:41 -07:00
|
|
|
|
if (activePlugin != null)
|
|
|
|
|
|
{
|
2018-09-07 10:25:29 -07:00
|
|
|
|
if(activePlugin is GCodeExport gCodeExport)
|
|
|
|
|
|
{
|
|
|
|
|
|
gCodeExport.CenterOnBed = centerOnBed;
|
|
|
|
|
|
}
|
2019-01-03 16:58:05 -08:00
|
|
|
|
exportErrors = await activePlugin.Generate(libraryItems, savePath, reporter, cancellationToken);
|
2018-08-17 17:49:41 -07:00
|
|
|
|
}
|
2017-08-09 11:07:03 -07:00
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
if (exportErrors == null || exportErrors.Count == 0)
|
2018-08-17 17:49:41 -07:00
|
|
|
|
{
|
|
|
|
|
|
ShowFileIfRequested(savePath);
|
|
|
|
|
|
}
|
2019-01-03 16:58:05 -08:00
|
|
|
|
else
|
2017-08-09 07:45:26 -07:00
|
|
|
|
{
|
2019-01-04 18:05:10 -08:00
|
|
|
|
ApplicationController.Instance.ShowValidationErrors("Export Error".Localize(), exportErrors);
|
2018-08-17 17:49:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2017-08-09 07:45:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-08-23 17:27:30 -07:00
|
|
|
|
this.AddPageAction(exportButton);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-09 12:05:17 -07:00
|
|
|
|
private void ShowFileIfRequested(string filename)
|
|
|
|
|
|
{
|
2017-10-09 14:57:21 -07:00
|
|
|
|
if (AggContext.OperatingSystem == OSType.Windows || AggContext.OperatingSystem == OSType.X11)
|
2017-08-09 12:05:17 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (showInFolderAfterSave.Checked)
|
|
|
|
|
|
{
|
2016-12-01 22:03:15 -08:00
|
|
|
|
AggContext.FileDialogs.ShowFileInFolder(filename);
|
2017-08-09 12:05:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2017-11-16 07:50:42 -08:00
|
|
|
|
}
|