From 45ff2d5e54f19e0f21fb0485eeca13926772da3a Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 18 Jul 2018 14:59:23 -0700 Subject: [PATCH] Add support for printing GCode - Issue MatterHackers/MatterControl#3457 MC2 2.0.0.9727 Gcode files do not print - Enable print from current bed plate - Enable print from library selection --- ApplicationView/PrinterModels.cs | 22 ++++++++++++++ Library/Widgets/PrintLibraryWidget.cs | 30 +++++++++++++++++-- .../View3D/PrinterBar/PrintPopupMenu.cs | 5 +++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/ApplicationView/PrinterModels.cs b/ApplicationView/PrinterModels.cs index d6a501a3d..33e509473 100644 --- a/ApplicationView/PrinterModels.cs +++ b/ApplicationView/PrinterModels.cs @@ -189,6 +189,28 @@ namespace MatterHackers.MatterControl return insertionGroup; } + public async Task StashAndPrintGCode(ILibraryItem libraryItem) + { + // Clear plate + await this.ClearPlate(); + + // Add content + await this.LoadContent( + new EditContext() + { + SourceItem = libraryItem, + // No content store for GCode, otherwise PlatingHistory + ContentStore = this.EditContext.ContentStore + }); + + // Slice and print + await ApplicationController.Instance.PrintPart( + this.EditContext, + this.Printer, + null, + CancellationToken.None); + } + public async Task StashAndPrint(IEnumerable selectedLibraryItems) { // Clear plate diff --git a/Library/Widgets/PrintLibraryWidget.cs b/Library/Widgets/PrintLibraryWidget.cs index f5a1c22fb..cae94eb74 100644 --- a/Library/Widgets/PrintLibraryWidget.cs +++ b/Library/Widgets/PrintLibraryWidget.cs @@ -525,7 +525,14 @@ namespace MatterHackers.MatterControl.PrintLibrary printer.Connection.StartSdCardPrint(sdcardItem.Name.ToLower()); break; case FileSystemFileItem fileItem when Path.GetExtension(fileItem.FileName).ToUpper() == ".GCODE": - //ApplicationController.Instance.ActivePrintItem = new PrintItemWrapper(new PrintItem(fileItem.Name, fileItem.Path)); + if (printer != null) + { + UiThread.RunOnIdle(async () => + { + await printer.Bed.StashAndPrintGCode(fileItem); + }); + } + break; default: //TODO: Otherwise add the selected items to the plate and print the plate? @@ -559,9 +566,26 @@ namespace MatterHackers.MatterControl.PrintLibrary Title = "Add to Plate".Localize(), Action = (selectedLibraryItems, listView) => { - // TODO: Sort out the right way to have an ActivePrinter context that looks and behaves correctly var activeContext = ApplicationController.Instance.DragDropData; - activeContext.SceneContext.AddToPlate(selectedLibraryItems); + var printer = activeContext.Printer; + + if (listView.SelectedItems.Count == 1 && + selectedLibraryItems.FirstOrDefault() is ILibraryAssetStream assetStream + && assetStream.ContentType == "gcode") + { + // Drop handler for special case of GCode or similar (change loaded scene to new context) + printer.Bed.LoadContent( + new EditContext() + { + SourceItem = assetStream, + // No content store for GCode, otherwise PlatingHistory + ContentStore = printer.Bed.EditContext.ContentStore + }).ConfigureAwait(false); + } + else + { + activeContext.SceneContext.AddToPlate(selectedLibraryItems); + } }, IsEnabled = (selectedListItems, listView) => { diff --git a/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs b/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs index 9ab50a058..ff3265fc5 100644 --- a/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs +++ b/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs @@ -35,6 +35,7 @@ using MatterHackers.Agg; using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.CustomWidgets; +using MatterHackers.MatterControl.Library; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; @@ -145,7 +146,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow }; button.Click += (s, e) => { - if (!printer.Bed.Scene.Children.Any()) + // Exit if the bed is not GCode and the bed has no items + if ((printer.Bed.EditContext.SourceItem as ILibraryAsset)?.ContentType != "gcode" + && printer.Bed.Scene.Children.Count <= 0) { return; }