mattercontrol/CustomWidgets/ExportPrintItemPage.cs

463 lines
14 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
2014-06-19 16:09:38 -07:00
using MatterHackers.Agg.PlatformAbstract;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
2017-03-15 16:17:06 -07:00
using MatterHackers.DataConverters3D;
2014-01-29 19:09:30 -08:00
using MatterHackers.GCodeVisualizer;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrinterCommunication.Io;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.Queue.OptionsMenu;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.PolygonMesh;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
public class ExportPrintItemPage : WizardPage
2015-04-08 15:20:10 -07:00
{
private CheckBox showInFolderAfterSave;
private CheckBox applyLeveling;
private PrintItemWrapper printItemWrapper;
private string gcodePathAndFilenameToSave;
private bool partIsGCode = false;
private string documentsPath;
public ExportPrintItemPage(PrintItemWrapper printItemWrapper)
: base(unlocalizedTextForTitle: "File export options:")
2015-04-08 15:20:10 -07:00
{
this.printItemWrapper = printItemWrapper;
if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == ".GCODE")
{
partIsGCode = true;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
this.Name = "Export Item Window";
2015-04-08 15:20:10 -07:00
CreateWindowContent();
PrinterSettings.PrintLevelingEnabledChanged.RegisterEvent(ReloadAfterPrinterProfileChanged, ref unregisterEvents);
2015-04-08 15:20:10 -07:00
}
public void CreateWindowContent()
{
bool modelCanBeExported = !partIsGCode;
if(modelCanBeExported
&& printItemWrapper != null
&& (printItemWrapper.PrintItem.Protected
|| printItemWrapper.PrintItem.ReadOnly))
2015-04-08 15:20:10 -07:00
{
modelCanBeExported = false;
}
if (modelCanBeExported)
{
// put in stl export
2017-08-08 20:31:05 -07:00
Button exportAsStlButton = textImageButtonFactory.Generate("Export as".Localize() + " STL");
exportAsStlButton.Name = "Export as STL button";
exportAsStlButton.HAnchor = HAnchor.Left;
2015-04-08 15:20:10 -07:00
exportAsStlButton.Cursor = Cursors.Hand;
exportAsStlButton.Click += exportSTL_Click;
contentRow.AddChild(exportAsStlButton);
2015-04-08 15:20:10 -07:00
// put in amf export
2017-08-08 20:31:05 -07:00
Button exportAsAmfButton = textImageButtonFactory.Generate("Export as".Localize() + " AMF");
exportAsAmfButton.Name = "Export as AMF button";
exportAsAmfButton.HAnchor = HAnchor.Left;
2015-04-08 15:20:10 -07:00
exportAsAmfButton.Cursor = Cursors.Hand;
exportAsAmfButton.Click += exportAMF_Click;
contentRow.AddChild(exportAsAmfButton);
2015-04-08 15:20:10 -07:00
}
bool showExportGCodeButton = ActiveSliceSettings.Instance.PrinterSelected || partIsGCode;
2015-04-08 15:20:10 -07:00
if (showExportGCodeButton)
{
2017-08-08 20:31:05 -07:00
Button exportGCode = textImageButtonFactory.Generate("Export as".Localize() + " G-Code");
exportGCode.Name = "Export as GCode Button";
exportGCode.HAnchor = HAnchor.Left;
2015-04-08 15:20:10 -07:00
exportGCode.Cursor = Cursors.Hand;
exportGCode.Click += (s, e) =>
2015-04-08 15:20:10 -07:00
{
UiThread.RunOnIdle(ExportGCode_Click);
};
contentRow.AddChild(exportGCode);
2014-06-23 13:18:51 -07:00
var gcodeExportPlugins = PluginFinder.CreateInstancesOf<ExportGcodePlugin>();
2016-03-17 08:57:58 -07:00
foreach (ExportGcodePlugin plugin in gcodeExportPlugins)
2016-03-17 08:57:58 -07:00
{
if (plugin.EnabledForCurrentPart(printItemWrapper))
{
//Create export button for each Plugin found
string exportButtonText = plugin.GetButtonText().Localize();
Button exportButton = textImageButtonFactory.Generate(exportButtonText);
exportButton.HAnchor = HAnchor.Left;
exportButton.Cursor = Cursors.Hand;
exportButton.Click += (s, e) =>
2016-03-17 08:57:58 -07:00
{
UiThread.RunOnIdle(() =>
{
// Close the export window
Close();
// Open a SaveFileDialog. If Save is clicked, slice the part if needed and pass the plugin the
// path to the gcode file and the target save path
FileDialog.SaveFileDialog(
new SaveFileDialogParams(plugin.GetExtensionFilter())
2016-03-17 08:57:58 -07:00
{
Title = "MatterControl: Export File",
FileName = printItemWrapper.Name,
ActionButtonLabel = "Export"
},
(SaveFileDialogParams saveParam) =>
{
string extension = Path.GetExtension(saveParam.FileName);
if (extension == "")
{
saveParam.FileName += plugin.GetFileExtension();
}
if (partIsGCode)
{
try
{
plugin.Generate(printItemWrapper.FileLocation, saveParam.FileName);
}
catch (Exception exception)
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, exception.Message, "Couldn't save file".Localize());
});
}
}
else
{
SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper);
printItemWrapper.SlicingDone += (printItem, eventArgs) =>
{
PrintItemWrapper sliceItem = (PrintItemWrapper)printItem;
if (File.Exists(sliceItem.GetGCodePathAndFileName()))
{
try
{
plugin.Generate(sliceItem.GetGCodePathAndFileName(), saveParam.FileName);
}
catch (Exception exception)
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, exception.Message, "Couldn't save file".Localize());
});
}
}
};
}
});
});
}; // End exportButton Click handler
contentRow.AddChild(exportButton);
}
2015-04-08 15:20:10 -07:00
}
}
contentRow.AddChild(new VerticalSpacer());
2015-04-08 15:20:10 -07:00
// If print leveling is enabled then add in a check box 'Apply Leveling During Export' and default checked.
if (showExportGCodeButton && ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.print_leveling_enabled))
2015-04-08 15:20:10 -07:00
{
2017-08-08 20:31:05 -07:00
applyLeveling = new CheckBox("Apply leveling to G-Code during export".Localize(), ActiveTheme.Instance.PrimaryTextColor, 10);
2015-04-08 15:20:10 -07:00
applyLeveling.Checked = true;
applyLeveling.HAnchor = HAnchor.Left;
2015-04-08 15:20:10 -07:00
applyLeveling.Cursor = Cursors.Hand;
//applyLeveling.Margin = new BorderDouble(top: 10);
contentRow.AddChild(applyLeveling);
2015-04-08 15:20:10 -07:00
}
// TODO: make this work on the mac and then delete this if
if (OsInformation.OperatingSystem == OSType.Windows
|| OsInformation.OperatingSystem == OSType.X11)
{
2017-01-04 07:23:30 -08:00
showInFolderAfterSave = new CheckBox("Show file in folder after save".Localize(), ActiveTheme.Instance.PrimaryTextColor, 10);
showInFolderAfterSave.HAnchor = HAnchor.Left;
2015-04-08 15:20:10 -07:00
showInFolderAfterSave.Cursor = Cursors.Hand;
//showInFolderAfterSave.Margin = new BorderDouble(top: 10);
contentRow.AddChild(showInFolderAfterSave);
2015-04-08 15:20:10 -07:00
}
if (!showExportGCodeButton)
{
2017-08-08 20:31:05 -07:00
var noGCodeMessage = new TextWidget(
"Note".Localize() + ": " + "To enable GCode export, select a printer profile.".Localize(),
textColor: ActiveTheme.Instance.PrimaryTextColor,
pointSize: 10);
noGCodeMessage.HAnchor = HAnchor.Left;
contentRow.AddChild(noGCodeMessage);
2015-04-08 15:20:10 -07:00
}
footerRow.AddChild(new HorizontalSpacer());
footerRow.AddChild(cancelButton);
2015-04-08 15:20:10 -07:00
}
private string Get8Name(string longName)
{
longName.Replace(' ', '_');
return longName.Substring(0, Math.Min(longName.Length, 8));
}
private void ExportGCode_Click()
2015-04-08 15:20:10 -07:00
{
SaveFileDialogParams saveParams = new SaveFileDialogParams("Export GCode|*.gcode", title: "Export GCode");
2014-01-29 19:09:30 -08:00
saveParams.Title = "MatterControl: Export File";
saveParams.ActionButtonLabel = "Export";
2015-04-08 15:20:10 -07:00
saveParams.FileName = Path.GetFileNameWithoutExtension(printItemWrapper.Name);
2014-01-29 19:09:30 -08:00
Close();
2014-10-11 15:11:26 -07:00
FileDialog.SaveFileDialog(saveParams, onExportGcodeFileSelected);
2015-04-08 15:20:10 -07:00
}
2015-04-08 15:20:10 -07:00
private void onExportGcodeFileSelected(SaveFileDialogParams saveParams)
2014-10-11 15:11:26 -07:00
{
if (!string.IsNullOrEmpty(saveParams.FileName))
2014-10-11 15:11:26 -07:00
{
2016-03-17 08:57:58 -07:00
ExportGcodeCommandLineUtility(saveParams.FileName);
2014-10-11 15:11:26 -07:00
}
}
2014-01-29 19:09:30 -08:00
public void ExportGcodeCommandLineUtility(String nameOfFile)
{
try
{
if (!string.IsNullOrEmpty(nameOfFile))
{
gcodePathAndFilenameToSave = nameOfFile;
string extension = Path.GetExtension(gcodePathAndFilenameToSave);
if (extension == "")
{
File.Delete(gcodePathAndFilenameToSave);
gcodePathAndFilenameToSave += ".gcode";
}
string sourceExtension = Path.GetExtension(printItemWrapper.FileLocation).ToUpper();
if (MeshFileIo.ValidFileExtensions().Contains(sourceExtension))
{
SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper);
printItemWrapper.SlicingDone += sliceItem_Done;
}
else if (partIsGCode)
{
SaveGCodeToNewLocation(printItemWrapper.FileLocation, gcodePathAndFilenameToSave);
}
}
}
catch
{
}
}
private void SaveGCodeToNewLocation(string gcodeFilename, string dest)
2015-04-08 15:20:10 -07:00
{
try
2015-04-08 15:20:10 -07:00
{
GCodeFileStream gCodeFileStream = new GCodeFileStream(GCodeFile.Load(gcodeFilename, CancellationToken.None));
bool addLevelingStream = ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.print_leveling_enabled) && applyLeveling.Checked;
var queueStream = new QueuedCommandsStream(gCodeFileStream);
// this is added to ensure we are rewriting the G0 G1 commands as needed
GCodeStream finalStream = addLevelingStream
? new ProcessWriteRegexStream(new PrintLevelingStream(queueStream, false), queueStream)
: new ProcessWriteRegexStream(queueStream, queueStream);
using (StreamWriter file = new StreamWriter(dest))
2015-04-08 15:20:10 -07:00
{
string nextLine = finalStream.ReadLine();
while (nextLine != null)
2015-04-08 15:20:10 -07:00
{
if (nextLine.Trim().Length > 0)
2015-04-08 15:20:10 -07:00
{
file.WriteLine(nextLine);
2015-04-08 15:20:10 -07:00
}
nextLine = finalStream.ReadLine();
2015-04-08 15:20:10 -07:00
}
}
ShowFileIfRequested(dest);
2015-04-08 15:20:10 -07:00
}
catch (Exception e)
2015-04-08 15:20:10 -07:00
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, e.Message, "Couldn't save file".Localize());
});
2015-04-08 15:20:10 -07:00
}
}
private void ShowFileIfRequested(string filename)
{
if (OsInformation.OperatingSystem == OSType.Windows)
2014-01-29 19:09:30 -08:00
{
2015-04-08 15:20:10 -07:00
if (showInFolderAfterSave.Checked)
2014-01-29 19:09:30 -08:00
{
#if IS_WINDOWS_FORMS
2015-04-08 15:20:10 -07:00
WindowsFormsAbstract.ShowFileInFolder(filename);
#endif
2014-01-29 19:09:30 -08:00
}
}
2015-04-08 15:20:10 -07:00
}
public override void OnClosed(ClosedEventArgs e)
2015-04-08 15:20:10 -07:00
{
2016-03-17 08:57:58 -07:00
printItemWrapper.SlicingDone -= sliceItem_Done;
2015-04-08 15:20:10 -07:00
if (unregisterEvents != null)
{
unregisterEvents(this, null);
}
base.OnClosed(e);
}
private EventHandler unregisterEvents;
2015-04-08 15:20:10 -07:00
private void ReloadAfterPrinterProfileChanged(object sender, EventArgs e)
{
CreateWindowContent();
}
private void exportAMF_Click(object sender, EventArgs mouseEvent)
{
UiThread.RunOnIdle(() =>
{
2017-03-15 16:17:06 -07:00
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save as AMF|*.amf", initialDirectory: documentsPath)
{
Title = "MatterControl: Export File",
ActionButtonLabel = "Export",
FileName = printItemWrapper.Name
};
Close();
FileDialog.SaveFileDialog(saveParams, onExportAmfFileSelected);
});
2015-04-08 15:20:10 -07:00
}
private async void onExportAmfFileSelected(SaveFileDialogParams saveParams)
{
await Task.Run(() => SaveAmf(saveParams));
}
2014-01-29 19:09:30 -08:00
private void SaveAmf(SaveFileDialogParams saveParams)
{
try
{
if (!string.IsNullOrEmpty(saveParams.FileName))
{
string filePathToSave = saveParams.FileName;
if (filePathToSave != null && 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);
}
else
{
IObject3D item = Object3D.Load(printItemWrapper.FileLocation, CancellationToken.None);
2017-03-15 16:17:06 -07:00
MeshFileIo.Save(item, filePathToSave);
}
ShowFileIfRequested(filePathToSave);
}
}
}
catch (Exception e)
{
UiThread.RunOnIdle (() => {
StyledMessageBox.ShowMessageBox(null, e.Message, "Couldn't save file".Localize());
});
}
}
2014-11-06 07:16:55 -08:00
2015-04-08 15:20:10 -07:00
private void exportSTL_Click(object sender, EventArgs mouseEvent)
{
UiThread.RunOnIdle(() =>
{
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save as STL|*.stl");
saveParams.Title = "MatterControl: Export File";
saveParams.ActionButtonLabel = "Export";
saveParams.FileName = printItemWrapper.Name;
Close();
FileDialog.SaveFileDialog(saveParams, onExportStlFileSelected);
});
}
2014-11-06 07:16:55 -08:00
private async void onExportStlFileSelected(SaveFileDialogParams saveParams)
{
await Task.Run(() => SaveStl(saveParams));
}
2014-11-06 07:16:55 -08:00
private void SaveStl(SaveFileDialogParams saveParams)
2014-10-11 15:11:26 -07:00
{
try
{
if (!string.IsNullOrEmpty(saveParams.FileName))
2014-10-11 15:11:26 -07:00
{
string filePathToSave = saveParams.FileName;
if (filePathToSave != null && filePathToSave != "")
2015-04-08 15:20:10 -07:00
{
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);
}
else
{
IObject3D loadedItem = Object3D.Load(printItemWrapper.FileLocation, CancellationToken.None);
2017-03-15 16:17:06 -07:00
if (!MeshFileIo.Save(new List<MeshGroup> { loadedItem.Flatten() }, filePathToSave))
{
UiThread.RunOnIdle (() => {
StyledMessageBox.ShowMessageBox(null, "AMF to STL conversion failed", "Couldn't save file".Localize());
});
}
}
ShowFileIfRequested(filePathToSave);
2015-04-08 15:20:10 -07:00
}
2014-10-11 15:11:26 -07:00
}
}
catch (Exception e)
{
UiThread.RunOnIdle (() => {
StyledMessageBox.ShowMessageBox(null, e.Message, "Couldn't save file".Localize());
});
}
2014-10-11 15:11:26 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void sliceItem_Done(object sender, EventArgs e)
{
PrintItemWrapper sliceItem = (PrintItemWrapper)sender;
2014-01-29 19:09:30 -08:00
printItemWrapper.SlicingDone -= sliceItem_Done;
2015-04-08 15:20:10 -07:00
SaveGCodeToNewLocation(sliceItem.GetGCodePathAndFileName(), gcodePathAndFilenameToSave);
}
}
}