From e7333057f05de8f71f662f5ccce22ccb1dc6b85b Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sat, 11 Jul 2020 19:28:30 -0700 Subject: [PATCH] Adding export feature --- MatterControl.csproj | 3 + .../ControlElements/StyledMessageBoxWindow.cs | 5 +- MatterControlLib/DataStorage/Models.cs | 4 + MatterControlLib/History/HistoryListView.cs | 107 +++++++++++++++++- .../History/PrintHistoryListItem.cs | 86 +++++++++++--- MatterControlLib/MatterControlLib.csproj | 1 + .../PrinterCommunication/PrinterConnection.cs | 4 +- 7 files changed, 185 insertions(+), 25 deletions(-) diff --git a/MatterControl.csproj b/MatterControl.csproj index 99fe37c3a..f2583953e 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -134,6 +134,9 @@ + + 15.0.5 + 5.2.6 diff --git a/MatterControlLib/ControlElements/StyledMessageBoxWindow.cs b/MatterControlLib/ControlElements/StyledMessageBoxWindow.cs index c6137bd4b..6551a1cef 100644 --- a/MatterControlLib/ControlElements/StyledMessageBoxWindow.cs +++ b/MatterControlLib/ControlElements/StyledMessageBoxWindow.cs @@ -167,12 +167,13 @@ namespace MatterHackers.MatterControl private void AdjustTextWrap() { - if (messageContainer != null) + if (messageContainer != null + && messageContainer is TextWidget textWidget) { double wrappingSize = contentRow.Width - (contentRow.Padding.Width + messageContainer.Margin.Width); if (wrappingSize > 0) { - var wrapper = new EnglishTextWrapping(12 * GuiWidget.DeviceScale); + var wrapper = new EnglishTextWrapping(textWidget.PointSize * GuiWidget.DeviceScale); messageContainer.Text = wrapper.InsertCRs(unwrappedMessage, wrappingSize); } } diff --git a/MatterControlLib/DataStorage/Models.cs b/MatterControlLib/DataStorage/Models.cs index 60e03115d..24e17c8a0 100644 --- a/MatterControlLib/DataStorage/Models.cs +++ b/MatterControlLib/DataStorage/Models.cs @@ -351,6 +351,10 @@ namespace MatterHackers.MatterControl.DataStorage public string PrinterName { get; set; } + public string QualitySettingsName { get; set; } + + public string MaterialSettingsName { get; set; } + public override void Commit() { if (this.PrintEnd != DateTime.MinValue) diff --git a/MatterControlLib/History/HistoryListView.cs b/MatterControlLib/History/HistoryListView.cs index 12b3c15a8..2dd2799b5 100644 --- a/MatterControlLib/History/HistoryListView.cs +++ b/MatterControlLib/History/HistoryListView.cs @@ -27,11 +27,18 @@ 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.Globalization; +using System.IO; using System.Linq; +using CsvHelper; using MatterHackers.Agg; +using MatterHackers.Agg.Platform; using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.CustomWidgets; +using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.Library; using MatterHackers.MatterControl.PartPreviewWindow; using MatterHackers.VectorMath; @@ -58,6 +65,31 @@ namespace MatterHackers.MatterControl.PrintHistory this.theme = theme; } + public class RowData + { + public string Printer { get; set; } + + public string QualitySettingsName { get; set; } + + public string MaterialSettingsName { get; set; } + + public string OverridSettings { get; set; } + + public string Name { get; set; } + + public DateTime Start { get; set; } + + public DateTime End { get; set; } + + public int Minutes { get; set; } + + public bool Compleated { get; set; } + + public double RecoveryCount { get; set; } + + public string ItemsPrinted { get; set; } + } + protected override void OnClick(MouseEventArgs mouseEvent) { if (mouseEvent.Button == MouseButtons.Right) @@ -67,18 +99,22 @@ namespace MatterHackers.MatterControl.PrintHistory // show a right click menu ('Set as Default' & 'Help') var popupMenu = new PopupMenu(theme); - var historyItems = PrintHistoryData.Instance.GetHistoryItems(1).Select(f => new PrintHistoryItem(f)).ToList(); + var historyItems = PrintHistoryData.Instance.GetHistoryItems(100); var exportPrintHistory = popupMenu.CreateMenuItem("Export History".Localize() + "..."); - exportPrintHistory.Enabled = historyItems.Count > 0; + exportPrintHistory.Enabled = historyItems.Any(); exportPrintHistory.Click += (s, e) => { if (ApplicationController.Instance.IsMatterControlPro()) { - // do the export + ExportToCsv(historyItems); } else // upsell MatterControl Pro { + StyledMessageBox.ShowMessageBox( + "Exporting print history is a MatterControl Pro feature. Upgrade to Pro to unlock MatterControl Pro.".Localize(), + "Upgrade to Pro".Localize(), + StyledMessageBox.MessageType.OK); } }; @@ -100,7 +136,7 @@ namespace MatterHackers.MatterControl.PrintHistory popupMenu.CreateSeparator(); var clearPrintHistory = popupMenu.CreateMenuItem("Clear History".Localize()); - clearPrintHistory.Enabled = historyItems.Count > 0; + clearPrintHistory.Enabled = historyItems.Any(); clearPrintHistory.Click += (s, e) => { // clear history @@ -124,6 +160,69 @@ namespace MatterHackers.MatterControl.PrintHistory base.OnClick(mouseEvent); } + private static void ExportToCsv(IEnumerable historyItems) + { + // right click success or fail + // user settings that are different + var records = new List(); + + // do the export + foreach (var item in historyItems) + { + string groupNames = PrintHistoryListItem.GetItemNamesFromMcx(item.PrintName); + + records.Add(new RowData() + { + Printer = item.PrinterName, + Name = item.PrintName, + Start = item.PrintStart, + End = item.PrintEnd, + Compleated = item.PrintComplete, + ItemsPrinted = groupNames, + Minutes = item.PrintTimeMinutes, + RecoveryCount = item.RecoveryCount, + QualitySettingsName = item.QualitySettingsName, + MaterialSettingsName = item.MaterialSettingsName, + OverridSettings = GetUserOverrides(), + }); + } + + AggContext.FileDialogs.SaveFileDialog( + new SaveFileDialogParams("MatterControl Printer Export|*.printer", title: "Export Printer Settings") + { + FileName = "Pinter Histor.csv", + Filter = "CSV Files|*.csv" + }, + (saveParams) => + { + try + { + if (!string.IsNullOrWhiteSpace(saveParams.FileName)) + { + using (var writer = new StreamWriter(saveParams.FileName)) + { + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteRecords(records); + } + } + } + } + catch (Exception e2) + { + UiThread.RunOnIdle(() => + { + StyledMessageBox.ShowMessageBox(e2.Message, "Couldn't save file".Localize()); + }); + } + }); + } + + private static string GetUserOverrides() + { + return null; + } + public ListViewItemBase AddItem(ListViewItem item) { var historyRowItem = item.Model as PrintHistoryItem; diff --git a/MatterControlLib/History/PrintHistoryListItem.cs b/MatterControlLib/History/PrintHistoryListItem.cs index 9011ea06d..d12e893d3 100644 --- a/MatterControlLib/History/PrintHistoryListItem.cs +++ b/MatterControlLib/History/PrintHistoryListItem.cs @@ -32,6 +32,8 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using System.Text.RegularExpressions; +using Lucene.Net.Util; using MatterHackers.Agg; using MatterHackers.Agg.UI; using MatterHackers.Localizations; @@ -39,6 +41,7 @@ using MatterHackers.MatterControl.CustomWidgets; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.PrintQueue; using MatterHackers.SerialPortCommunication; +using Newtonsoft.Json; namespace MatterHackers.MatterControl.PrintHistory { @@ -93,10 +96,10 @@ namespace MatterHackers.MatterControl.PrintHistory labelName = printTask.PrinterName + ":\n" + labelName; } - var mcxFileName = Path.Combine(ApplicationDataStorage.Instance.PlatingDirectory, printTask.PrintName); - if (File.Exists(mcxFileName)) + string groupNames = GetItemNamesFromMcx(printTask.PrintName); + if (!string.IsNullOrEmpty(groupNames)) { - labelName += "\n" + GetStlNames(mcxFileName); + labelName += "\n" + groupNames; } var partLabel = new TextWidget(labelName, pointSize: 15) @@ -191,25 +194,31 @@ namespace MatterHackers.MatterControl.PrintHistory this.BackgroundColor = new Color(255, 255, 255, 255); } - private string GetStlNames(string mcxFileName) + public static string GetItemNamesFromMcx(string mcxFileName) { - var foundNames = new HashSet(); - var allLines = File.ReadAllLines(mcxFileName); - foreach (var line in allLines) + // add in the cache path + mcxFileName = Path.Combine(ApplicationDataStorage.Instance.PlatingDirectory, mcxFileName); + if (File.Exists(mcxFileName)) { - var find = "\"Name\":"; - var end = line.IndexOf(find) + find.Length; - if (end >= find.Length - && !line.ToLower().Contains(".mcx")) - { - var nameStart = line.IndexOf("\"", end); - var nameEnd = line.IndexOf("\"", nameStart + 1); - var name = line.Substring(nameStart + 1, nameEnd - nameStart - 1); - foundNames.Add(name); - } + var names = JsonConvert.DeserializeObject(File.ReadAllText(mcxFileName)).AllNames(); + var grouped = names.GroupBy(n => n) + .Select(g => + { + if (g.Count() > 1) + { + return g.Key + " (" + g.Count() + ")"; + } + else + { + return g.Key; + } + }) + .OrderBy(n => n); + var groupNames = string.Join(", ", grouped); + return groupNames; } - return string.Join(", ", foundNames); + return null; } private void AddTimeStamp(PrintTask printTask, Color timeTextColor, FlowLayoutWidget primaryFlow) @@ -322,4 +331,45 @@ namespace MatterHackers.MatterControl.PrintHistory } } } + + public static class McxDocument + { + public class McxNode + { + public List Children { get; set; } + + public string Name { get; set; } + + public bool Visible { get; set; } + + public string MeshPath { get; set; } + + private static Regex fileNameNumberMatch = new Regex("\\(\\d+\\)\\s*$", RegexOptions.Compiled); + + public IEnumerable AllNames() + { + if (Children?.Count > 0) + { + foreach (var child in Children) + { + foreach (var name in child.AllNames()) + { + yield return name; + } + } + } + else if (!string.IsNullOrWhiteSpace(Name)) + { + if (Name.Contains("(")) + { + yield return fileNameNumberMatch.Replace(Name, "").Trim(); + } + else + { + yield return Name; + } + } + } + } + } } \ No newline at end of file diff --git a/MatterControlLib/MatterControlLib.csproj b/MatterControlLib/MatterControlLib.csproj index 9d3f50deb..30895d642 100644 --- a/MatterControlLib/MatterControlLib.csproj +++ b/MatterControlLib/MatterControlLib.csproj @@ -86,6 +86,7 @@ + diff --git a/MatterControlLib/PrinterCommunication/PrinterConnection.cs b/MatterControlLib/PrinterCommunication/PrinterConnection.cs index 2e3cc951d..4429547af 100644 --- a/MatterControlLib/PrinterCommunication/PrinterConnection.cs +++ b/MatterControlLib/PrinterCommunication/PrinterConnection.cs @@ -2144,7 +2144,9 @@ Make sure that your printer is turned on. Some printers will appear to be connec PrinterName = this.Printer.Settings.GetValue(SettingsKey.printer_name), PrintItemId = activePrintItem.PrintItem.Id, PrintingGCodeFileName = gcodeFileNameForTask, - PrintComplete = false + PrintComplete = false, + QualitySettingsName = this.Printer.Settings.QualityLayer?.Name, + MaterialSettingsName = this.Printer.Settings.MaterialLayer?.Name, }; ActivePrintTask.Commit();