Generate calibration gcode from in memory object3D

- Issue MatterHackers/MCCentral#5207
Calibration prints should shouldn't put models on the bed
This commit is contained in:
jlewin 2019-03-22 13:12:24 -07:00
parent df803eb9eb
commit b4e22d560d
2 changed files with 51 additions and 14 deletions

View file

@ -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)

View file

@ -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);