Revise export interface to support reporting

- Issue MatterHackers/MCCentral#3975
Show progress when exporting G-Code files
This commit is contained in:
John Lewin 2018-08-17 17:49:41 -07:00
parent 01c9a1d581
commit a94076cca4
9 changed files with 86 additions and 47 deletions

View file

@ -2186,7 +2186,7 @@ namespace MatterHackers.MatterControl
object3D,
gcodeFilePath,
printer,
new SliceProgressReporter(reporter, printer),
reporter,
cancellationToken);
});

View file

@ -162,13 +162,19 @@ namespace MatterHackers.MatterControl
ActionButtonLabel = "Export".Localize(),
Title = ApplicationController.Instance.ProductName + " - " + "Select A Folder".Localize()
},
async (openParams) =>
(openParams) =>
{
string path = openParams.FolderPath;
if (!string.IsNullOrEmpty(path))
{
await activePlugin.Generate(libraryItems, path);
}
ApplicationController.Instance.Tasks.Execute(
"Saving".Localize() + "...",
async (reporter, cancellationToken) =>
{
string path = openParams.FolderPath;
if (!string.IsNullOrEmpty(path))
{
await activePlugin.Generate(libraryItems, path, reporter, cancellationToken);
}
});
});
});
@ -192,33 +198,35 @@ namespace MatterHackers.MatterControl
if (!string.IsNullOrEmpty(savePath))
{
Task.Run(async () =>
{
string extension = Path.GetExtension(savePath);
if (extension != targetExtension)
ApplicationController.Instance.Tasks.Execute(
"Exporting".Localize() + "...",
async (reporter, cancellationToken) =>
{
savePath += targetExtension;
}
bool succeeded = false;
if (activePlugin != null)
{
succeeded = await activePlugin.Generate(libraryItems, savePath);
}
if (succeeded)
{
ShowFileIfRequested(savePath);
}
else
{
UiThread.RunOnIdle(() =>
string extension = Path.GetExtension(savePath);
if (extension != targetExtension)
{
StyledMessageBox.ShowMessageBox("Export failed".Localize(), title);
});
}
});
savePath += targetExtension;
}
bool succeeded = false;
if (activePlugin != null)
{
succeeded = await activePlugin.Generate(libraryItems, savePath, reporter, cancellationToken);
}
if (succeeded)
{
ShowFileIfRequested(savePath);
}
else
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox("Export failed".Localize(), title);
});
}
});
}
});
});

View file

@ -31,7 +31,9 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
@ -57,7 +59,7 @@ namespace MatterHackers.MatterControl.Library.Export
public bool ExportPossible(ILibraryAsset libraryItem) => true;
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath)
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
{
var streamItems = libraryItems.OfType<ILibraryAssetStream>();
if (streamItems.Any())

View file

@ -114,7 +114,7 @@ namespace MatterHackers.MatterControl.Library.Export
return container;
}
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath)
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
{
var firstItem = libraryItems.OfType<ILibraryAsset>().FirstOrDefault();
if (firstItem != null)
@ -144,7 +144,17 @@ namespace MatterHackers.MatterControl.Library.Export
}
else if (firstItem is ILibraryObject3D object3DItem)
{
var status = new ProgressStatus()
{
Status = "Saving Asset".Localize()
};
loadedItem = await object3DItem.CreateContent(null);
await loadedItem.PersistAssets((percentComplete, text) =>
{
status.Progress0To1 = percentComplete;
progress.Report(status);
}, publishAssets: false);
}
else if (assetStream != null)
{
@ -189,10 +199,12 @@ namespace MatterHackers.MatterControl.Library.Export
printer.Settings.SetValue(SettingsKey.spiral_vase, "1");
}
await ApplicationController.Instance.Tasks.Execute("Slicing Item".Localize() + " " + loadedItem.Name, (reporter, cancellationToken) =>
{
return Slicer.SliceItem(loadedItem, gcodePath, printer, reporter, cancellationToken);
});
await ApplicationController.Instance.Tasks.Execute(
"Slicing Item".Localize() + " " + loadedItem.Name,
(reporter, cancellationToken2) =>
{
return Slicer.SliceItem(loadedItem, gcodePath, printer, reporter, cancellationToken2);
});
}
finally
{

View file

@ -26,8 +26,11 @@ 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;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.Library;
@ -43,7 +46,7 @@ namespace MatterHackers.MatterControl
void Initialize(PrinterConfig printer);
Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath);
Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken);
bool Enabled { get; }

View file

@ -27,10 +27,13 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
@ -56,7 +59,7 @@ namespace MatterHackers.MatterControl.Library.Export
public bool ExportPossible(ILibraryAsset libraryItem) => true;
public Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath)
public Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
{
if (libraryItems.OfType<ILibraryAsset>().FirstOrDefault() is ILibraryAsset libraryItem)
{

View file

@ -32,7 +32,9 @@ using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
@ -58,7 +60,7 @@ namespace MatterHackers.MatterControl.Library.Export
public bool ExportPossible(ILibraryAsset libraryItem) => true;
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath)
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
{
var streamItems = libraryItems.OfType<ILibraryAssetStream>();
if (streamItems.Any())

View file

@ -31,7 +31,9 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.MatterControl.Library;
@ -65,7 +67,7 @@ namespace MatterHackers.MatterControl.Plugins.X3GDriver
public bool ExportPossible(ILibraryAsset libraryItem) => true;
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath)
public async Task<bool> Generate(IEnumerable<ILibraryItem> libraryItems, string outputPath, IProgress<ProgressStatus> progress, CancellationToken cancellationToken)
{
ILibraryAssetStream libraryContent = libraryItems.OfType<ILibraryAssetStream>().FirstOrDefault();

View file

@ -39,6 +39,7 @@ using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.DataConverters3D;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.SettingsManagement;
using MatterHackers.MeshVisualizer;
@ -235,8 +236,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return SliceItem(stlFileLocations, mergeRules, gcodeFilePath, printer, progressReporter, cancellationToken);
}
public static Task<bool> SliceItem(List<(Matrix4X4 matrix, string fileName)> stlFileLocations, string mergeRules, string gcodeFilePath, PrinterConfig printer, IProgress<ProgressStatus> progressReporter, CancellationToken cancellationToken)
public static Task<bool> SliceItem(List<(Matrix4X4 matrix, string fileName)> stlFileLocations, string mergeRules, string gcodeFilePath, PrinterConfig printer, IProgress<ProgressStatus> reporter, CancellationToken cancellationToken)
{
// Wrap the reporter with a specialized MatterSlice string parser for percent from string results
var sliceProgressReporter = new SliceProgressReporter(reporter, printer);
bool slicingSucceeded = true;
if(stlFileLocations.Count > 0)
@ -245,14 +249,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
{
Status = "Generating Config"
};
progressReporter.Report(progressStatus);
sliceProgressReporter.Report(progressStatus);
string configFilePath = Path.Combine(
ApplicationDataStorage.Instance.GCodeOutputPath,
string.Format("config_{0}.ini", printer.Settings.GetLongHashCode().ToString()));
progressStatus.Status = "Starting slicer";
progressReporter.Report(progressStatus);
sliceProgressReporter.Report(progressStatus);
if (!File.Exists(gcodeFilePath)
|| !HasCompletedSuccessfully(gcodeFilePath))
@ -301,7 +305,10 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
if (s is string stringValue)
{
progressReporter?.Report(new ProgressStatus()
sliceProgressReporter?.Report(new ProgressStatus()
{
Status = stringValue
});
@ -350,7 +357,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
message += "...";
progressReporter?.Report(new ProgressStatus()
sliceProgressReporter?.Report(new ProgressStatus()
{
Status = message
});