From b4e22d560df9e34644bd9dbb7c8febced20c1a07 Mon Sep 17 00:00:00 2001 From: jlewin Date: Fri, 22 Mar 2019 13:12:24 -0700 Subject: [PATCH] Generate calibration gcode from in memory object3D - Issue MatterHackers/MCCentral#5207 Calibration prints should shouldn't put models on the bed --- .../ApplicationView/EditContext.cs | 21 +++++++++ .../XyCalibrationStartPrintPage.cs | 44 +++++++++++++------ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/MatterControlLib/ApplicationView/EditContext.cs b/MatterControlLib/ApplicationView/EditContext.cs index 17dec566e..8116141b9 100644 --- a/MatterControlLib/ApplicationView/EditContext.cs +++ b/MatterControlLib/ApplicationView/EditContext.cs @@ -81,6 +81,27 @@ namespace MatterHackers.MatterControl return GCodePath(printer); } + public static string GCodeFilePath(PrinterConfig printer, IObject3D object3D) + { + using (var memoryStream = new MemoryStream()) + { + // Write JSON + object3D.SaveTo(memoryStream); + + // Reposition + memoryStream.Position = 0; + + // Calculate + string fileHashCode = HashGenerator.ComputeSHA1(memoryStream); + + ulong settingsHashCode = printer.Settings.GetGCodeCacheKey(); + + return Path.Combine( + ApplicationDataStorage.Instance.GCodeOutputPath, + $"{fileHashCode}_{ settingsHashCode}.gcode"); + } + } + internal void Save(IObject3D scene) { if (!this.FreezeGCode) diff --git a/MatterControlLib/CustomWidgets/XyCalibrationStartPrintPage.cs b/MatterControlLib/CustomWidgets/XyCalibrationStartPrintPage.cs index 4755b9ba6..939285dbf 100644 --- a/MatterControlLib/CustomWidgets/XyCalibrationStartPrintPage.cs +++ b/MatterControlLib/CustomWidgets/XyCalibrationStartPrintPage.cs @@ -28,12 +28,15 @@ either expressed or implied, of the FreeBSD Project. */ using System; +using System.IO; using System.Threading; +using System.Threading.Tasks; using MatterHackers.Agg.UI; using MatterHackers.DataConverters3D; using MatterHackers.Localizations; using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling; using MatterHackers.MatterControl.DesignTools; +using MatterHackers.MatterControl.Library; using MatterHackers.MatterControl.PrinterCommunication; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; @@ -68,11 +71,9 @@ namespace MatterHackers.MatterControl startCalibrationPrint.Name = "Start Calibration Print"; startCalibrationPrint.Click += async (s, e) => { - // stash the current bed - var scene = printer.Bed.Scene; - scene.Children.Modify((list) => list.Clear()); + var scene = new Object3D(); - // create the item we are adding + // create the calibration objects IObject3D item = CreateCalibrationObject(printer, xyCalibrationData); // add the calibration object to the bed @@ -82,26 +83,41 @@ namespace MatterHackers.MatterControl var bedBounds = printer.Bed.Bounds; var aabb = item.GetAxisAlignedBoundingBox(); item.Matrix *= Matrix4X4.CreateTranslation(bedBounds.Center.X - aabb.MinXYZ.X - aabb.XSize / 2, bedBounds.Center.Y - aabb.MinXYZ.Y - aabb.YSize / 2, -aabb.MinXYZ.Z); - // switch to 3D view + // register callbacks for print completion - printer.Connection.Disposed += Connection_Disposed; - printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged; + printer.Connection.Disposed += this.Connection_Disposed; + printer.Connection.CommunicationStateChanged += this.Connection_CommunicationStateChanged; this.MoveToNextPage(); // hide this window this.DialogWindow.Visible = false; - // Save the bed that we have created before starting print operation - await printer.Bed.SaveChanges(null, CancellationToken.None); + string gcodePath = EditContext.GCodeFilePath(printer, scene); - // start the calibration print - await ApplicationController.Instance.PrintPart( - printer.Bed.EditContext, + printer.Connection.CommunicationState = CommunicationStates.PreparingToPrint; + + (bool slicingSucceeded, string finalGCodePath) = await ApplicationController.Instance.SliceItemLoadOutput( printer, - null, - CancellationToken.None); + scene, + gcodePath); + // Only start print if slicing completed + if (slicingSucceeded) + { + await printer.Bed.LoadContent(new EditContext() + { + SourceItem = new FileSystemFileItem(gcodePath), + ContentStore = null // No content store for GCode + }); + + await printer.Connection.StartPrint(finalGCodePath, allowRecovery: false); + ApplicationController.Instance.MonitorPrintTask(printer); + } + else + { + printer.Connection.CommunicationState = CommunicationStates.Connected; + } }; theme.ApplyPrimaryActionStyle(startCalibrationPrint);