From f852ddf7089ee3d2ed774d92b864b7d7cc2f0055 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Thu, 29 Mar 2018 12:31:36 -0700 Subject: [PATCH] First pass at leveling consolidation --- .../PrintLeveling/LevelWizard13PointRadial.cs | 61 +-- .../PrintLeveling/LevelWizard3Point.cs | 137 ++---- .../PrintLeveling/LevelWizard3x3Mesh.cs | 104 ++++ .../PrintLeveling/LevelWizard7PointRadial.cs | 251 +--------- .../PrintLeveling/LevelWizardBase.cs | 216 +++++---- .../PrintLeveling/LevelWizardMesh.cs | 443 ------------------ .../PrintLeveling/MeshLevlingFunctions.cs | 230 +++++++++ MatterControl.csproj | 3 +- .../Io/PrintLevelingStream.cs | 22 +- Queue/OptionsMenu/ExportToFolderProcess.cs | 20 +- Submodules/MatterSlice | 2 +- .../MatterControl/LevelingTests.cs | 121 ----- .../MatterControl/SettingsParseTests.cs | 51 +- 13 files changed, 546 insertions(+), 1115 deletions(-) create mode 100644 ConfigurationPage/PrintLeveling/LevelWizard3x3Mesh.cs delete mode 100644 ConfigurationPage/PrintLeveling/LevelWizardMesh.cs create mode 100644 ConfigurationPage/PrintLeveling/MeshLevlingFunctions.cs diff --git a/ConfigurationPage/PrintLeveling/LevelWizard13PointRadial.cs b/ConfigurationPage/PrintLeveling/LevelWizard13PointRadial.cs index 7b3208df1..8550c3195 100644 --- a/ConfigurationPage/PrintLeveling/LevelWizard13PointRadial.cs +++ b/ConfigurationPage/PrintLeveling/LevelWizard13PointRadial.cs @@ -27,65 +27,38 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ -using MatterHackers.Agg; -using MatterHackers.GCodeVisualizer; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.DataStorage; -using MatterHackers.MatterControl.PrinterCommunication; +using System; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; -using System; -using System.Collections.Generic; -using System.Text; namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling { - public class LevelWizard13PointRadial : LevelWizardRadialBase + public class LevelWizard13PointRadial : LevelWizardBase { - static readonly int numberOfRadialSamples = 12; - public LevelWizard13PointRadial(PrinterConfig printer, LevelWizardBase.RuningState runningState) - : base(printer, runningState, 500, 370, (numberOfRadialSamples + 1) * 3, numberOfRadialSamples) + : base(printer, runningState, 3 * 3) { } - public static string ApplyLeveling(PrinterSettings printerSettings, string lineBeingSent, Vector3 currentDestination) + public override int ProbeCount => 3; + + public override Vector2 GetPrintLevelPositionToSample(int index) { - if (printerSettings?.GetValue(SettingsKey.print_leveling_enabled) == true - && (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 ")) - && lineBeingSent.Length > 2 - && lineBeingSent[2] == ' ') + int numberOfRadialSamples = 6; + double bedRadius = Math.Min(printer.Settings.GetValue(SettingsKey.bed_size).X, printer.Settings.GetValue(SettingsKey.bed_size).Y) / 2; + + Vector2 bedCenter = printer.Settings.GetValue(SettingsKey.print_center); + if (index < numberOfRadialSamples) { - return GetLevelingFunctions(printerSettings, numberOfRadialSamples, printerSettings.Helpers.GetPrintLevelingData(), printerSettings.GetValue(SettingsKey.print_center)) - .DoApplyLeveling(lineBeingSent, currentDestination); + Vector2 position = new Vector2(bedRadius, 0); + position.Rotate(MathHelper.Tau / numberOfRadialSamples * index); + position += bedCenter; + return position; } - - return lineBeingSent; - } - - public override Vector2 GetPrintLevelPositionToSample(int index, double radius) - { - PrintLevelingData levelingData = printer.Settings.Helpers.GetPrintLevelingData(); - return GetLevelingFunctions(printer.Settings, numberOfRadialSamples, levelingData, printer.Settings.GetValue(SettingsKey.print_center)) - .GetPrintLevelPositionToSample(index, radius); - } - - public static List ProcessCommand(string lineBeingSent) - { - int commentIndex = lineBeingSent.IndexOf(';'); - if (commentIndex > 0) // there is content in front of the ; + else { - lineBeingSent = lineBeingSent.Substring(0, commentIndex).Trim(); + return bedCenter; } - List lines = new List(); - lines.Add(lineBeingSent); - if (lineBeingSent.StartsWith("G28") - || lineBeingSent.StartsWith("G29")) - { - lines.Add("M114"); - } - - return lines; } } } \ No newline at end of file diff --git a/ConfigurationPage/PrintLeveling/LevelWizard3Point.cs b/ConfigurationPage/PrintLeveling/LevelWizard3Point.cs index a386cc2de..d561ce320 100644 --- a/ConfigurationPage/PrintLeveling/LevelWizard3Point.cs +++ b/ConfigurationPage/PrintLeveling/LevelWizard3Point.cs @@ -27,120 +27,67 @@ 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 MatterControl.Printing; using MatterHackers.Agg; using MatterHackers.Localizations; using MatterHackers.MatterControl.SlicerConfiguration; +using MatterHackers.MeshVisualizer; using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling { public class LevelWizard3Point : LevelWizardBase { - private LevelingStrings levelingStrings; - public LevelWizard3Point(PrinterConfig printer, LevelWizardBase.RuningState runningState) - : base(printer, 500, 370, 9) + : base(printer, runningState, 3 * 3) { - levelingStrings = new LevelingStrings(printer.Settings); - string printLevelWizardTitle = ApplicationController.Instance.ProductName; - string printLevelWizardTitleFull = "Print Leveling Wizard".Localize(); - Title = string.Format("{0} - {1}", printLevelWizardTitle, printLevelWizardTitleFull); - List probePositions = new List(3); - probePositions.Add(new ProbePosition()); - probePositions.Add(new ProbePosition()); - probePositions.Add(new ProbePosition()); - - printLevelWizard = new WizardControl(); - AddChild(printLevelWizard); - - if (runningState == LevelWizardBase.RuningState.InitialStartupCalibration) - { - string requiredPageInstructions = "{0}\n\n{1}".FormatWith(levelingStrings.requiredPageInstructions1, levelingStrings.requiredPageInstructions2); - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.initialPrinterSetupStepText, requiredPageInstructions)); - } - - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.OverviewText, levelingStrings.WelcomeText(3, 3))); - - bool useZProbe = printer.Settings.Helpers.UseZProbe(); - if (!useZProbe) - { - printLevelWizard.AddPage(new CleanExtruderInstructionPage(printer, "Check Nozzle".Localize(), levelingStrings.CleanExtruder)); - } - - // To make sure the bed is at the correct temp, put in a filament selection page. - bool hasHeatedBed = printer.Settings.GetValue(SettingsKey.has_heated_bed); - if (hasHeatedBed) - { - string filamentSelectionPage = "{0}\n\n{1}".FormatWith(levelingStrings.materialPageInstructions1, levelingStrings.materialPageInstructions2); - printLevelWizard.AddPage(new SelectMaterialPage(printer, levelingStrings.materialStepText, filamentSelectionPage)); - } - - printLevelWizard.AddPage(new HomePrinterPage(printer, printLevelWizard, - levelingStrings.HomingPageStepText, - levelingStrings.HomingPageInstructions(useZProbe), - useZProbe)); - if (hasHeatedBed) - { - printLevelWizard.AddPage(new WaitForTempPage(printer, printLevelWizard, levelingStrings)); - } - - string positionLabel = "Position".Localize(); - string autoCalibrateLabel = "Auto Calibrate".Localize(); - string lowPrecisionLabel = "Low Precision".Localize(); - string medPrecisionLabel = "Medium Precision".Localize(); - string highPrecisionLabel = "High Precision".Localize(); - - double startProbeHeight = printer.Settings.GetValue(SettingsKey.print_leveling_probe_start); - - for (int i = 0; i < 3; i++) - { - Vector2 probePosition = LevelWizardBase.GetPrintLevelPositionToSample(printer.Settings, i); - - if (printer.Settings.Helpers.UseZProbe()) - { - var stepString = string.Format("{0} {1} {2} {3}:", levelingStrings.stepTextBeg, i + 1, levelingStrings.stepTextEnd, 3); - printLevelWizard.AddPage(new AutoProbeFeedback(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", stepString, positionLabel, i + 1, autoCalibrateLabel), probePositions, i)); - } - else - { - printLevelWizard.AddPage(new GetCoarseBedHeight(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, lowPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, medPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetUltraFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, highPrecisionLabel), probePositions, i, levelingStrings)); - } - } - - printLevelWizard.AddPage(new LastPagelInstructions(printer, printLevelWizard, "Done".Localize(), levelingStrings.DoneInstructions, probePositions)); } - public static string ApplyLeveling(PrinterSettings printerSettings, string lineBeingSent, Vector3 currentDestination, PrinterMachineInstruction.MovementTypes movementMode) + public override int ProbeCount => 3; + + public override Vector2 GetPrintLevelPositionToSample(int index) { - if (printerSettings?.GetValue(SettingsKey.print_leveling_enabled) == true - && (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 "))) - { - lineBeingSent = PrintLevelingPlane.Instance.ApplyLeveling(currentDestination, lineBeingSent); - } + Vector2 bedSize = printer.Settings.GetValue(SettingsKey.bed_size); + Vector2 printCenter = printer.Settings.GetValue(SettingsKey.print_center); - return lineBeingSent; - } - - public static List ProcessCommand(string lineBeingSent) - { - int commentIndex = lineBeingSent.IndexOf(';'); - if (commentIndex > 0) // there is content in front of the ; + switch (printer.Settings.GetValue(SettingsKey.bed_shape)) { - lineBeingSent = lineBeingSent.Substring(0, commentIndex).Trim(); - } - List lines = new List(); - lines.Add(lineBeingSent); - if (lineBeingSent.StartsWith("G28") - || lineBeingSent.StartsWith("G29")) - { - lines.Add("M114"); - } + case BedShape.Circular: + Vector2 firstPosition = new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .5); + switch (index) + { + case 0: + return firstPosition; - return lines; + case 1: + return Vector2.Rotate(firstPosition, MathHelper.Tau / 3); + + case 2: + return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3); + + default: + throw new IndexOutOfRangeException(); + } + + case BedShape.Rectangular: + default: + switch (index) + { + case 0: + return new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .8); + + case 1: + return new Vector2(printCenter.X - (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); + + case 2: + return new Vector2(printCenter.X + (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); + + default: + throw new IndexOutOfRangeException(); + } + } } } } \ No newline at end of file diff --git a/ConfigurationPage/PrintLeveling/LevelWizard3x3Mesh.cs b/ConfigurationPage/PrintLeveling/LevelWizard3x3Mesh.cs new file mode 100644 index 000000000..66f6cdb8c --- /dev/null +++ b/ConfigurationPage/PrintLeveling/LevelWizard3x3Mesh.cs @@ -0,0 +1,104 @@ +/* +Copyright (c) 2014, Lars Brubaker +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +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 MatterHackers.MatterControl.SlicerConfiguration; +using MatterHackers.MeshVisualizer; +using MatterHackers.VectorMath; + +namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling +{ + public class LevelWizard3x3Mesh : LevelWizardBase + { + public LevelWizard3x3Mesh(PrinterConfig printer, LevelWizardBase.RuningState runningState) + : base(printer, runningState, 9 * 3) + { + } + + public override int ProbeCount => 9; + + public override Vector2 GetPrintLevelPositionToSample(int index) + { + Vector2 bedSize = printer.Settings.GetValue(SettingsKey.bed_size); + Vector2 printCenter = printer.Settings.GetValue(SettingsKey.print_center); + + if (printer.Settings.GetValue(SettingsKey.bed_shape) == BedShape.Circular) + { + // reduce the bed size by the ratio of the radius (square root of 2) so that the sample positions will fit on a ciclular bed + bedSize *= 1.0 / Math.Sqrt(2); + } + + // we know we are getting 3x3 sample positions they run like this + // 6 7 8 Y max + // 3 4 5 + // 0 1 2 Y min + int xIndex = index % 3; + int yIndex = index / 3; + + Vector2 samplePosition = new Vector2(); + switch (xIndex) + { + case 0: + samplePosition.X = printCenter.X - (bedSize.X / 2) * .8; + break; + + case 1: + samplePosition.X = printCenter.X; + break; + + case 2: + samplePosition.X = printCenter.X + (bedSize.X / 2) * .8; + break; + + default: + throw new IndexOutOfRangeException(); + } + + switch (yIndex) + { + case 0: + samplePosition.Y = printCenter.Y - (bedSize.Y / 2) * .8; + break; + + case 1: + samplePosition.Y = printCenter.Y; + break; + + case 2: + samplePosition.Y = printCenter.Y + (bedSize.Y / 2) * .8; + break; + + default: + throw new IndexOutOfRangeException(); + } + + return samplePosition; + } + } +} \ No newline at end of file diff --git a/ConfigurationPage/PrintLeveling/LevelWizard7PointRadial.cs b/ConfigurationPage/PrintLeveling/LevelWizard7PointRadial.cs index 9cdacd8bf..8822b5a06 100644 --- a/ConfigurationPage/PrintLeveling/LevelWizard7PointRadial.cs +++ b/ConfigurationPage/PrintLeveling/LevelWizard7PointRadial.cs @@ -28,261 +28,30 @@ either expressed or implied, of the FreeBSD Project. */ using System; -using System.Collections.Generic; -using System.Text; -using MatterControl.Printing; -using MatterHackers.Agg; -using MatterHackers.Localizations; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling { - public class LevelWizard7PointRadial : LevelWizardRadialBase + public class LevelWizard7PointRadial : LevelWizardBase { - private static readonly int numberOfRadialSamples = 6; - public LevelWizard7PointRadial(PrinterConfig printer, LevelWizardBase.RuningState runningState) - : base(printer, runningState, 500, 370, 21, numberOfRadialSamples) + : base(printer, runningState, 3 * 3) { } - public static string ApplyLeveling(PrinterSettings printerSettings, string lineBeingSent, Vector3 currentDestination) + public override int ProbeCount => 3; + + public override Vector2 GetPrintLevelPositionToSample(int index) { - if (printerSettings?.GetValue(SettingsKey.print_leveling_enabled) == true - && (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 ")) - && lineBeingSent.Length > 2 - && lineBeingSent[2] == ' ') - { - PrintLevelingData levelingData = printerSettings.Helpers.GetPrintLevelingData(); - return GetLevelingFunctions(printerSettings, numberOfRadialSamples, levelingData, printerSettings.GetValue(SettingsKey.print_center)) - .DoApplyLeveling(lineBeingSent, currentDestination); - } - - return lineBeingSent; - } - - public static List ProcessCommand(string lineBeingSent) - { - int commentIndex = lineBeingSent.IndexOf(';'); - if (commentIndex > 0) // there is content in front of the ; - { - lineBeingSent = lineBeingSent.Substring(0, commentIndex).Trim(); - } - List lines = new List(); - lines.Add(lineBeingSent); - if (lineBeingSent.StartsWith("G28") - || lineBeingSent.StartsWith("G29")) - { - lines.Add("M114"); - } - - return lines; - } - - public override Vector2 GetPrintLevelPositionToSample(int index, double radius) - { - PrintLevelingData levelingData = printer.Settings.Helpers.GetPrintLevelingData(); - return GetLevelingFunctions(printer.Settings, numberOfRadialSamples, levelingData, printer.Settings.GetValue(SettingsKey.print_center)) - .GetPrintLevelPositionToSample(index, radius); - } - } - - public abstract class LevelWizardRadialBase : LevelWizardBase - { - private static RadialLevlingFunctions currentLevelingFunctions = null; - private LevelingStrings levelingStrings; - - public LevelWizardRadialBase(PrinterConfig printer, LevelWizardBase.RuningState runningState, int width, int height, int totalSteps, int numberOfRadialSamples) - : base(printer, width, height, totalSteps) - { - levelingStrings = new LevelingStrings(printer.Settings); - string printLevelWizardTitle = ApplicationController.Instance.ProductName; - string printLevelWizardTitleFull = "Print Leveling Wizard".Localize(); - Title = string.Format("{0} - {1}", printLevelWizardTitle, printLevelWizardTitleFull); - List probePositions = new List(numberOfRadialSamples + 1); - for (int i = 0; i < numberOfRadialSamples + 1; i++) - { - probePositions.Add(new ProbePosition()); - } - - printLevelWizard = new WizardControl(); - AddChild(printLevelWizard); - - if (runningState == LevelWizardBase.RuningState.InitialStartupCalibration) - { - string requiredPageInstructions = "{0}\n\n{1}".FormatWith(levelingStrings.requiredPageInstructions1, levelingStrings.requiredPageInstructions2); - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.initialPrinterSetupStepText, requiredPageInstructions)); - } - - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.OverviewText, levelingStrings.WelcomeText(numberOfRadialSamples + 1, 5))); - - bool useZProbe = printer.Settings.Helpers.UseZProbe(); - if (!useZProbe) - { - printLevelWizard.AddPage(new CleanExtruderInstructionPage(printer, "Check Nozzle".Localize(), levelingStrings.CleanExtruder)); - } - - // To make sure the bed is at the correct temp, put in a filament selection page. - bool hasHeatedBed = printer.Settings.GetValue(SettingsKey.has_heated_bed); - if (hasHeatedBed) - { - string filamentSelectionPage = "{0}\n\n{1}".FormatWith(levelingStrings.materialPageInstructions1, levelingStrings.materialPageInstructions2); - printLevelWizard.AddPage(new SelectMaterialPage(printer, levelingStrings.materialStepText, filamentSelectionPage)); - } - printLevelWizard.AddPage(new HomePrinterPage(printer, printLevelWizard, - levelingStrings.HomingPageStepText, - levelingStrings.HomingPageInstructions(useZProbe), - useZProbe)); - if (hasHeatedBed) - { - printLevelWizard.AddPage(new WaitForTempPage(printer, printLevelWizard, levelingStrings)); - } - - string positionLabel = "Position".Localize(); - string autoCalibrateLabel = "Auto Calibrate".Localize(); - string lowPrecisionLabel = "Low Precision".Localize(); - string medPrecisionLabel = "Medium Precision".Localize(); - string highPrecisionLabel = "High Precision".Localize(); - + int numberOfRadialSamples = 6; double bedRadius = Math.Min(printer.Settings.GetValue(SettingsKey.bed_size).X, printer.Settings.GetValue(SettingsKey.bed_size).Y) / 2; - double startProbeHeight = printer.Settings.GetValue(SettingsKey.print_leveling_probe_start); - for (int i = 0; i < numberOfRadialSamples + 1; i++) + Vector2 bedCenter = printer.Settings.GetValue(SettingsKey.print_center); + if (index < numberOfRadialSamples) { - Vector2 probePosition = GetPrintLevelPositionToSample(i, bedRadius); - - if (printer.Settings.Helpers.UseZProbe()) - { - var stepString = string.Format("{0} {1} {2} {3}:", levelingStrings.stepTextBeg, i + 1, levelingStrings.stepTextEnd, numberOfRadialSamples + 1); - printLevelWizard.AddPage(new AutoProbeFeedback(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", stepString, positionLabel, i + 1, autoCalibrateLabel), probePositions, i)); - } - else - { - printLevelWizard.AddPage(new GetCoarseBedHeight(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, lowPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, medPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetUltraFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, highPrecisionLabel), probePositions, i, levelingStrings)); - } - } - - printLevelWizard.AddPage(new LastPagelInstructions(printer, printLevelWizard, "Done".Localize(), levelingStrings.DoneInstructions, probePositions)); - } - - public static RadialLevlingFunctions GetLevelingFunctions(PrinterSettings printerSettings, int numberOfRadialSamples, PrintLevelingData levelingData, Vector2 bedCenter) - { - if (currentLevelingFunctions == null - || currentLevelingFunctions.NumberOfRadialSamples != numberOfRadialSamples - || currentLevelingFunctions.BedCenter != bedCenter - || !levelingData.SamplesAreSame(currentLevelingFunctions.SampledPositions)) - { - if (currentLevelingFunctions != null) - { - currentLevelingFunctions.Dispose(); - } - - currentLevelingFunctions = new RadialLevlingFunctions(printerSettings, numberOfRadialSamples, levelingData, bedCenter); - } - - return currentLevelingFunctions; - } - - public abstract Vector2 GetPrintLevelPositionToSample(int index, double radius); - } - - public class RadialLevlingFunctions : IDisposable - { - PrinterSettings printerSettings; - - public RadialLevlingFunctions(PrinterSettings printerSettings, int numberOfRadialSamples, PrintLevelingData levelingData, Vector2 bedCenter) - { - this.printerSettings = printerSettings; - this.SampledPositions = new List(levelingData.SampledPositions); - this.BedCenter = bedCenter; - this.NumberOfRadialSamples = numberOfRadialSamples; - } - - public Vector2 BedCenter - { - get; set; - } - - public List SampledPositions { get; private set; } - - public int NumberOfRadialSamples { get; set; } - - public void Dispose() - { - } - - public string DoApplyLeveling(string lineBeingSent, Vector3 currentDestination) - { - double extruderDelta = 0; - GCodeFile.GetFirstNumberAfter("E", lineBeingSent, ref extruderDelta); - double feedRate = 0; - GCodeFile.GetFirstNumberAfter("F", lineBeingSent, ref feedRate); - - StringBuilder newLine = new StringBuilder("G1 "); - - if (lineBeingSent.Contains("X") || lineBeingSent.Contains("Y") || lineBeingSent.Contains("Z")) - { - Vector3 outPosition = GetPositionWithZOffset(currentDestination); - - newLine = newLine.Append(String.Format("X{0:0.##} Y{1:0.##} Z{2:0.###}", outPosition.X, outPosition.Y, outPosition.Z)); - } - - if (extruderDelta != 0) - { - newLine = newLine.Append(String.Format(" E{0:0.###}", extruderDelta)); - } - - if (feedRate != 0) - { - newLine = newLine.Append(String.Format(" F{0:0.##}", feedRate)); - } - - lineBeingSent = newLine.ToString(); - - return lineBeingSent; - } - - public Vector3 GetPositionWithZOffset(Vector3 currentDestination) - { - if (SampledPositions.Count == NumberOfRadialSamples + 1) - { - Vector2 destinationFromCenter = new Vector2(currentDestination) - BedCenter; - - double angleToPoint = Math.Atan2(destinationFromCenter.Y, destinationFromCenter.X); - - if (angleToPoint < 0) - { - angleToPoint += MathHelper.Tau; - } - - double oneSegmentAngle = MathHelper.Tau / NumberOfRadialSamples; - int firstIndex = (int)(angleToPoint / oneSegmentAngle); - int lastIndex = firstIndex + 1; - if (lastIndex == NumberOfRadialSamples) - { - lastIndex = 0; - } - - Plane currentPlane = new Plane(SampledPositions[firstIndex], SampledPositions[lastIndex], SampledPositions[NumberOfRadialSamples]); - - double hitDistance = currentPlane.GetDistanceToIntersection(new Vector3(currentDestination.X, currentDestination.Y, 0), Vector3.UnitZ); - - currentDestination.Z += hitDistance; - } - - return currentDestination; - } - - public Vector2 GetPrintLevelPositionToSample(int index, double radius) - { - Vector2 bedCenter = printerSettings.GetValue(SettingsKey.print_center); - if (index < NumberOfRadialSamples) - { - Vector2 position = new Vector2(radius, 0); - position.Rotate(MathHelper.Tau / NumberOfRadialSamples * index); + Vector2 position = new Vector2(bedRadius, 0); + position.Rotate(MathHelper.Tau / numberOfRadialSamples * index); position += bedCenter; return position; } diff --git a/ConfigurationPage/PrintLeveling/LevelWizardBase.cs b/ConfigurationPage/PrintLeveling/LevelWizardBase.cs index 1022f0173..8f4931b8f 100644 --- a/ConfigurationPage/PrintLeveling/LevelWizardBase.cs +++ b/ConfigurationPage/PrintLeveling/LevelWizardBase.cs @@ -27,6 +27,7 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ +using MatterHackers.Agg; using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.PrinterCommunication; @@ -44,8 +45,9 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling public Vector3 position; } - public class LevelWizardBase : SystemWindow + public abstract class LevelWizardBase : SystemWindow { + private static MeshLevlingFunctions currentLevelingFunctions = null; private LevelingStrings levelingStrings; public enum RuningState { InitialStartupCalibration, UserRequestedCalibration } @@ -55,106 +57,148 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling protected int totalSteps { get; private set; } protected PrinterConfig printer; - public LevelWizardBase(PrinterConfig printer, int width, int height, int totalSteps) - : base(width, height) + public abstract int ProbeCount { get; } + + public LevelWizardBase(PrinterConfig printer, RuningState runningState, int totalSteps) + : base(500, 370) { levelingStrings = new LevelingStrings(printer.Settings); this.printer = printer; AlwaysOnTopOfMain = true; this.totalSteps = totalSteps; - } - public static List GetManualPositions(string settingsValue, int requiredCount) - { - // can look like "0,1:100,2:50,101" - if (!string.IsNullOrEmpty(settingsValue)) + + + + + + levelingStrings = new LevelingStrings(printer.Settings); + string printLevelWizardTitle = ApplicationController.Instance.ProductName; + string printLevelWizardTitleFull = "Print Leveling Wizard".Localize(); + Title = string.Format("{0} - {1}", printLevelWizardTitle, printLevelWizardTitleFull); + List probePositions = new List(ProbeCount); + for (int i = 0; i < ProbeCount; i++) { - var coordinates = settingsValue.Split(':'); - if(coordinates.Length == requiredCount) - { - var result = new List(); - foreach(var coordinate in coordinates) - { - var xyData = coordinate.Split(','); - if(xyData.Length != 2) - { - // bad data - return null; - } + probePositions.Add(new ProbePosition()); + } - Vector2 probePosition = new Vector2(); - if (!double.TryParse(xyData[0], out probePosition.X)) - { - // error - return null; - } - if (!double.TryParse(xyData[1], out probePosition.Y)) - { - // error - return null; - } - result.Add(probePosition); - } - if (result.Count == requiredCount) - { - return result; - } + printLevelWizard = new WizardControl(); + AddChild(printLevelWizard); + + if (runningState == LevelWizardBase.RuningState.InitialStartupCalibration) + { + string requiredPageInstructions = "{0}\n\n{1}".FormatWith(levelingStrings.requiredPageInstructions1, levelingStrings.requiredPageInstructions2); + printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.initialPrinterSetupStepText, requiredPageInstructions)); + } + + printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.OverviewText, levelingStrings.WelcomeText(ProbeCount, 5))); + + bool useZProbe = printer.Settings.Helpers.UseZProbe(); + if (!useZProbe) + { + printLevelWizard.AddPage(new CleanExtruderInstructionPage(printer, "Check Nozzle".Localize(), levelingStrings.CleanExtruder)); + } + + var printerSettings = printer.Settings; + + // To make sure the bed is at the correct temp, put in a filament selection page. + bool hasHeatedBed = printerSettings.GetValue(SettingsKey.has_heated_bed); + if (hasHeatedBed) + { + string filamentSelectionPage = "{0}\n\n{1}".FormatWith(levelingStrings.materialPageInstructions1, levelingStrings.materialPageInstructions2); + printLevelWizard.AddPage(new SelectMaterialPage(printer, levelingStrings.materialStepText, filamentSelectionPage)); + } + printLevelWizard.AddPage(new HomePrinterPage(printer, printLevelWizard, + levelingStrings.HomingPageStepText, + levelingStrings.HomingPageInstructions(useZProbe), + useZProbe)); + if (hasHeatedBed) + { + printLevelWizard.AddPage(new WaitForTempPage(printer, printLevelWizard, levelingStrings)); + } + + string positionLabel = "Position".Localize(); + string autoCalibrateLabel = "Auto Calibrate".Localize(); + string lowPrecisionLabel = "Low Precision".Localize(); + string medPrecisionLabel = "Medium Precision".Localize(); + string highPrecisionLabel = "High Precision".Localize(); + + double bedRadius = Math.Min(printerSettings.GetValue(SettingsKey.bed_size).X, printerSettings.GetValue(SettingsKey.bed_size).Y) / 2; + + double startProbeHeight = printerSettings.GetValue(SettingsKey.print_leveling_probe_start); + for (int i = 0; i < ProbeCount; i++) + { + Vector2 probePosition = GetPrintLevelPositionToSample(i); + + if (printerSettings.Helpers.UseZProbe()) + { + var stepString = string.Format("{0} {1} {2} {3}:", levelingStrings.stepTextBeg, i + 1, levelingStrings.stepTextEnd, ProbeCount); + printLevelWizard.AddPage(new AutoProbeFeedback(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", stepString, positionLabel, i + 1, autoCalibrateLabel), probePositions, i)); + } + else + { + printLevelWizard.AddPage(new GetCoarseBedHeight(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, lowPrecisionLabel), probePositions, i, levelingStrings)); + printLevelWizard.AddPage(new GetFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, medPrecisionLabel), probePositions, i, levelingStrings)); + printLevelWizard.AddPage(new GetUltraFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, highPrecisionLabel), probePositions, i, levelingStrings)); } } - return null; - } - public static Vector2 GetPrintLevelPositionToSample(PrinterSettings printerSettings, int index) - { - var manualPositions = GetManualPositions(printerSettings.GetValue(SettingsKey.leveling_manual_positions), 3); - if(manualPositions != null) - { - return manualPositions[index]; - } - - Vector2 bedSize = printerSettings.GetValue(SettingsKey.bed_size); - Vector2 printCenter = printerSettings.GetValue(SettingsKey.print_center); - - switch (printerSettings.GetValue(SettingsKey.bed_shape)) - { - case BedShape.Circular: - Vector2 firstPosition = new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .5); - switch (index) - { - case 0: - return firstPosition; - - case 1: - return Vector2.Rotate(firstPosition, MathHelper.Tau / 3); - - case 2: - return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3); - - default: - throw new IndexOutOfRangeException(); - } - - case BedShape.Rectangular: - default: - switch (index) - { - case 0: - return new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .8); - - case 1: - return new Vector2(printCenter.X - (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); - - case 2: - return new Vector2(printCenter.X + (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); - - default: - throw new IndexOutOfRangeException(); - } - } + printLevelWizard.AddPage(new LastPagelInstructions(printer, printLevelWizard, "Done".Localize(), levelingStrings.DoneInstructions, probePositions)); } private static SystemWindow printLevelWizardWindow; + public static string ApplyLeveling(PrinterSettings printerSettings, string lineBeingSent, Vector3 currentDestination) + { + if (printerSettings?.GetValue(SettingsKey.print_leveling_enabled) == true + && (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 ")) + && lineBeingSent.Length > 2 + && lineBeingSent[2] == ' ') + { + PrintLevelingData levelingData = printerSettings.Helpers.GetPrintLevelingData(); + return GetLevelingFunctions(printerSettings, 3, 3, levelingData) + .DoApplyLeveling(lineBeingSent, currentDestination); + } + + return lineBeingSent; + } + + public static List ProcessCommand(string lineBeingSent) + { + int commentIndex = lineBeingSent.IndexOf(';'); + if (commentIndex > 0) // there is content in front of the ; + { + lineBeingSent = lineBeingSent.Substring(0, commentIndex).Trim(); + } + List lines = new List(); + lines.Add(lineBeingSent); + if (lineBeingSent.StartsWith("G28") + || lineBeingSent.StartsWith("G29")) + { + lines.Add("M114"); + } + + return lines; + } + + public static MeshLevlingFunctions GetLevelingFunctions(PrinterSettings printerSettings, int gridWidth, int gridHeight, PrintLevelingData levelingData) + { + if (currentLevelingFunctions == null + || !levelingData.SamplesAreSame(currentLevelingFunctions.SampledPositions)) + { + if (currentLevelingFunctions != null) + { + currentLevelingFunctions.Dispose(); + } + + currentLevelingFunctions = new MeshLevlingFunctions(printerSettings, gridWidth, gridHeight, levelingData); + } + + return currentLevelingFunctions; + } + + public abstract Vector2 GetPrintLevelPositionToSample(int index); + public static void ShowPrintLevelWizard(PrinterConfig printer) { LevelWizardBase.RuningState runningState = LevelWizardBase.RuningState.UserRequestedCalibration; diff --git a/ConfigurationPage/PrintLeveling/LevelWizardMesh.cs b/ConfigurationPage/PrintLeveling/LevelWizardMesh.cs deleted file mode 100644 index 2bfa21b5f..000000000 --- a/ConfigurationPage/PrintLeveling/LevelWizardMesh.cs +++ /dev/null @@ -1,443 +0,0 @@ -/* -Copyright (c) 2014, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -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.Text; -using MatterControl.Printing; -using MatterHackers.Agg; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.SlicerConfiguration; -using MatterHackers.MeshVisualizer; -using MatterHackers.VectorMath; - -namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling -{ - public class LevelWizard3x3Mesh : LevelWizardMeshBase - { - public LevelWizard3x3Mesh(PrinterConfig printer, LevelWizardBase.RuningState runningState) - : base(printer, runningState, 500, 370, 27, 3, 3) - { - } - - public static string ApplyLeveling(PrinterSettings printerSettings, string lineBeingSent, Vector3 currentDestination) - { - if (printerSettings?.GetValue(SettingsKey.print_leveling_enabled) == true - && (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 ")) - && lineBeingSent.Length > 2 - && lineBeingSent[2] == ' ') - { - PrintLevelingData levelingData = printerSettings.Helpers.GetPrintLevelingData(); - return GetLevelingFunctions(printerSettings, 3, 3, levelingData) - .DoApplyLeveling(lineBeingSent, currentDestination); - } - - return lineBeingSent; - } - - public static List ProcessCommand(string lineBeingSent) - { - int commentIndex = lineBeingSent.IndexOf(';'); - if (commentIndex > 0) // there is content in front of the ; - { - lineBeingSent = lineBeingSent.Substring(0, commentIndex).Trim(); - } - List lines = new List(); - lines.Add(lineBeingSent); - if (lineBeingSent.StartsWith("G28") - || lineBeingSent.StartsWith("G29")) - { - lines.Add("M114"); - } - - return lines; - } - - public override Vector2 GetPrintLevelPositionToSample(int index) - { - var manualPositions = GetManualPositions(printer.Settings.GetValue(SettingsKey.leveling_manual_positions), 9); - if (manualPositions != null) - { - return manualPositions[index]; - } - - Vector2 bedSize = printer.Settings.GetValue(SettingsKey.bed_size); - Vector2 printCenter = printer.Settings.GetValue(SettingsKey.print_center); - - if (printer.Settings.GetValue(SettingsKey.bed_shape) == BedShape.Circular) - { - // reduce the bed size by the ratio of the radius (square root of 2) so that the sample positions will fit on a ciclular bed - bedSize *= 1.0 / Math.Sqrt(2); - } - - // we know we are getting 3x3 sample positions they run like this - // 6 7 8 Y max - // 3 4 5 - // 0 1 2 Y min - int xIndex = index % 3; - int yIndex = index / 3; - - Vector2 samplePosition = new Vector2(); - switch (xIndex) - { - case 0: - samplePosition.X = printCenter.X - (bedSize.X / 2) * .8; - break; - - case 1: - samplePosition.X = printCenter.X; - break; - - case 2: - samplePosition.X = printCenter.X + (bedSize.X / 2) * .8; - break; - - default: - throw new IndexOutOfRangeException(); - } - - switch (yIndex) - { - case 0: - samplePosition.Y = printCenter.Y - (bedSize.Y / 2) * .8; - break; - - case 1: - samplePosition.Y = printCenter.Y; - break; - - case 2: - samplePosition.Y = printCenter.Y + (bedSize.Y / 2) * .8; - break; - - default: - throw new IndexOutOfRangeException(); - } - - return samplePosition; - } - } - - public abstract class LevelWizardMeshBase : LevelWizardBase - { - private static MeshLevlingFunctions currentLevelingFunctions = null; - protected LevelingStrings levelingStrings; - - public LevelWizardMeshBase(PrinterConfig printer, LevelWizardBase.RuningState runningState, int width, int height, int totalSteps, int gridWidth, int gridHeight) - : base(printer, width, height, totalSteps) - { - levelingStrings = new LevelingStrings(printer.Settings); - string printLevelWizardTitle = ApplicationController.Instance.ProductName; - string printLevelWizardTitleFull = "Print Leveling Wizard".Localize(); - Title = string.Format("{0} - {1}", printLevelWizardTitle, printLevelWizardTitleFull); - int probeCount = gridWidth * gridHeight; - List probePositions = new List(probeCount); - for (int i = 0; i < probeCount; i++) - { - probePositions.Add(new ProbePosition()); - } - - printLevelWizard = new WizardControl(); - AddChild(printLevelWizard); - - if (runningState == LevelWizardBase.RuningState.InitialStartupCalibration) - { - string requiredPageInstructions = "{0}\n\n{1}".FormatWith(levelingStrings.requiredPageInstructions1, levelingStrings.requiredPageInstructions2); - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.initialPrinterSetupStepText, requiredPageInstructions)); - } - - printLevelWizard.AddPage(new FirstPageInstructions(printer, levelingStrings.OverviewText, levelingStrings.WelcomeText(probeCount, 5))); - - bool useZProbe = printer.Settings.Helpers.UseZProbe(); - if (!useZProbe) - { - printLevelWizard.AddPage(new CleanExtruderInstructionPage(printer, "Check Nozzle".Localize(), levelingStrings.CleanExtruder)); - } - - var printerSettings = printer.Settings; - - // To make sure the bed is at the correct temp, put in a filament selection page. - bool hasHeatedBed = printerSettings.GetValue(SettingsKey.has_heated_bed); - if (hasHeatedBed) - { - string filamentSelectionPage = "{0}\n\n{1}".FormatWith(levelingStrings.materialPageInstructions1, levelingStrings.materialPageInstructions2); - printLevelWizard.AddPage(new SelectMaterialPage(printer, levelingStrings.materialStepText, filamentSelectionPage)); - } - printLevelWizard.AddPage(new HomePrinterPage(printer, printLevelWizard, - levelingStrings.HomingPageStepText, - levelingStrings.HomingPageInstructions(useZProbe), - useZProbe)); - if (hasHeatedBed) - { - printLevelWizard.AddPage(new WaitForTempPage(printer, printLevelWizard, levelingStrings)); - } - - string positionLabel = "Position".Localize(); - string autoCalibrateLabel = "Auto Calibrate".Localize(); - string lowPrecisionLabel = "Low Precision".Localize(); - string medPrecisionLabel = "Medium Precision".Localize(); - string highPrecisionLabel = "High Precision".Localize(); - - double bedRadius = Math.Min(printerSettings.GetValue(SettingsKey.bed_size).X, printerSettings.GetValue(SettingsKey.bed_size).Y) / 2; - - double startProbeHeight = printerSettings.GetValue(SettingsKey.print_leveling_probe_start); - for (int i = 0; i < probeCount; i++) - { - Vector2 probePosition = GetPrintLevelPositionToSample(i); - - if (printerSettings.Helpers.UseZProbe()) - { - var stepString = string.Format("{0} {1} {2} {3}:", levelingStrings.stepTextBeg, i + 1, levelingStrings.stepTextEnd, probeCount); - printLevelWizard.AddPage(new AutoProbeFeedback(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", stepString, positionLabel, i + 1, autoCalibrateLabel), probePositions, i)); - } - else - { - printLevelWizard.AddPage(new GetCoarseBedHeight(printer, printLevelWizard, new Vector3(probePosition, startProbeHeight), string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, lowPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, medPrecisionLabel), probePositions, i, levelingStrings)); - printLevelWizard.AddPage(new GetUltraFineBedHeight(printer, printLevelWizard, string.Format("{0} {1} {2} - {3}", levelingStrings.GetStepString(totalSteps), positionLabel, i + 1, highPrecisionLabel), probePositions, i, levelingStrings)); - } - } - - printLevelWizard.AddPage(new LastPagelInstructions(printer, printLevelWizard, "Done".Localize(), levelingStrings.DoneInstructions, probePositions)); - } - - public static MeshLevlingFunctions GetLevelingFunctions(PrinterSettings printerSettings, int gridWidth, int gridHeight, PrintLevelingData levelingData) - { - if (currentLevelingFunctions == null - || !levelingData.SamplesAreSame(currentLevelingFunctions.SampledPositions)) - { - if (currentLevelingFunctions != null) - { - currentLevelingFunctions.Dispose(); - } - - currentLevelingFunctions = new MeshLevlingFunctions(printerSettings, gridWidth, gridHeight, levelingData); - } - - return currentLevelingFunctions; - } - - new public abstract Vector2 GetPrintLevelPositionToSample(int index); - } - - public class MeshLevlingFunctions : IDisposable - { - private Vector3 lastDestinationWithLevelingApplied = new Vector3(); - - PrinterSettings printerSettings; - - public MeshLevlingFunctions(PrinterSettings printerSettings, int gridWidth, int gridHeight, PrintLevelingData levelingData) - { - this.printerSettings = printerSettings; - this.SampledPositions = new List(levelingData.SampledPositions); - - for (int y = 0; y < gridHeight - 1; y++) - { - for (int x = 0; x < gridWidth - 1; x++) - { - // add all the regions - Regions.Add(new Region() - { - LeftBottom = levelingData.SampledPositions[y * gridWidth + x], - RightBottom = levelingData.SampledPositions[y * gridWidth + x + 1], - LeftTop = levelingData.SampledPositions[(y + 1) * gridWidth + x], - RightTop = levelingData.SampledPositions[(y + 1) * gridWidth + x + 1], - }); - } - } - } - - // you can only set this on construction - public List SampledPositions { get; private set; } - - public List Regions { get; private set; } = new List(); - - public void Dispose() - { - } - - public string DoApplyLeveling(string lineBeingSent, Vector3 currentDestination) - { - double extruderDelta = 0; - GCodeFile.GetFirstNumberAfter("E", lineBeingSent, ref extruderDelta); - double feedRate = 0; - GCodeFile.GetFirstNumberAfter("F", lineBeingSent, ref feedRate); - - StringBuilder newLine = new StringBuilder("G1 "); - - if (lineBeingSent.Contains("X") || lineBeingSent.Contains("Y") || lineBeingSent.Contains("Z")) - { - Vector3 outPosition = GetPositionWithZOffset(currentDestination); - - lastDestinationWithLevelingApplied = outPosition; - - newLine = newLine.Append(String.Format("X{0:0.##} Y{1:0.##} Z{2:0.###}", outPosition.X, outPosition.Y, outPosition.Z)); - } - - if (extruderDelta != 0) - { - newLine = newLine.Append(String.Format(" E{0:0.###}", extruderDelta)); - } - - if (feedRate != 0) - { - newLine = newLine.Append(String.Format(" F{0:0.##}", feedRate)); - } - - lineBeingSent = newLine.ToString(); - - return lineBeingSent; - } - - public Vector3 GetPositionWithZOffset(Vector3 currentDestination) - { - Region region = GetCorrectRegion(currentDestination); - - return region.GetPositionWithZOffset(currentDestination); - } - - public Vector2 GetPrintLevelPositionToSample(int index, int gridWidth, int gridHeight) - { - var manualPositions = LevelWizardBase.GetManualPositions(printerSettings.GetValue(SettingsKey.leveling_manual_positions), gridWidth * gridHeight); - if (manualPositions != null) - { - return manualPositions[index]; - } - - Vector2 bedSize = printerSettings.GetValue(SettingsKey.bed_size); - Vector2 printCenter = printerSettings.GetValue(SettingsKey.print_center); - - switch (printerSettings.GetValue(SettingsKey.bed_shape)) - { - case BedShape.Circular: - Vector2 firstPosition = new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .5); - switch (index) - { - case 0: - return firstPosition; - - case 1: - return Vector2.Rotate(firstPosition, MathHelper.Tau / 3); - - case 2: - return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3); - - default: - throw new IndexOutOfRangeException(); - } - - case BedShape.Rectangular: - default: - switch (index) - { - case 0: - return new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .8); - - case 1: - return new Vector2(printCenter.X - (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); - - case 2: - return new Vector2(printCenter.X + (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); - - default: - throw new IndexOutOfRangeException(); - } - } - } - - private Region GetCorrectRegion(Vector3 currentDestination) - { - int bestIndex = 0; - double bestDist = double.PositiveInfinity; - - currentDestination.Z = 0; - for (int regionIndex = 0; regionIndex < Regions.Count; regionIndex++) - { - var dist = (Regions[regionIndex].Center - currentDestination).LengthSquared; - if(dist < bestDist) - { - bestIndex = regionIndex; - bestDist = dist; - } - } - - return Regions[bestIndex]; - } - - public class Region - { - public Vector3 LeftBottom { get; set; } - public Vector3 LeftTop { get; set; } - public Vector3 RightBottom { get; set; } - public Vector3 RightTop { get; set; } - - internal Vector3 Center { get; private set; } - internal Vector3 LeftBottomCenter { get; private set; } - internal Vector3 RightTopCenter { get; private set; } - - internal Plane LeftBottomPlane { get; private set; } - internal Plane RightTopPlane { get; private set; } - - internal Vector3 GetPositionWithZOffset(Vector3 currentDestination) - { - if (LeftBottomPlane.PlaneNormal == Vector3.Zero) - { - InitializePlanes(); - } - - var destinationAtZ0 = new Vector3(currentDestination.X, currentDestination.Y, 0); - - // which triangle to check (distance to the centers) - if ((LeftBottomCenter - destinationAtZ0).LengthSquared < (RightTopCenter - destinationAtZ0).LengthSquared) - { - double hitDistance = LeftBottomPlane.GetDistanceToIntersection(destinationAtZ0, Vector3.UnitZ); - currentDestination.Z += hitDistance; - } - else - { - double hitDistance = RightTopPlane.GetDistanceToIntersection(destinationAtZ0, Vector3.UnitZ); - currentDestination.Z += hitDistance; - } - - return currentDestination; - } - - private void InitializePlanes() - { - LeftBottomPlane = new Plane(LeftBottom, RightBottom, LeftTop); - LeftBottomCenter = (LeftBottom + RightBottom + LeftTop) / 3; - - RightTopPlane = new Plane(RightBottom, RightTop, LeftTop); - RightTopCenter = (RightBottom + RightTop + LeftTop) / 3; - - Center = (LeftBottomCenter + RightTopCenter) / 2; - } - } - } -} \ No newline at end of file diff --git a/ConfigurationPage/PrintLeveling/MeshLevlingFunctions.cs b/ConfigurationPage/PrintLeveling/MeshLevlingFunctions.cs new file mode 100644 index 000000000..9e250da22 --- /dev/null +++ b/ConfigurationPage/PrintLeveling/MeshLevlingFunctions.cs @@ -0,0 +1,230 @@ +/* +Copyright (c) 2014, Lars Brubaker +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +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.Text; +using MatterControl.Printing; +using MatterHackers.Agg; +using MatterHackers.Localizations; +using MatterHackers.MatterControl.SlicerConfiguration; +using MatterHackers.MeshVisualizer; +using MatterHackers.VectorMath; + +namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling +{ + public class MeshLevlingFunctions : IDisposable + { + private Vector3 lastDestinationWithLevelingApplied = new Vector3(); + + PrinterSettings printerSettings; + + public MeshLevlingFunctions(PrinterSettings printerSettings, int gridWidth, int gridHeight, PrintLevelingData levelingData) + { + this.printerSettings = printerSettings; + this.SampledPositions = new List(levelingData.SampledPositions); + + for (int y = 0; y < gridHeight - 1; y++) + { + for (int x = 0; x < gridWidth - 1; x++) + { + // add all the regions + Regions.Add(new Region() + { + LeftBottom = levelingData.SampledPositions[y * gridWidth + x], + RightBottom = levelingData.SampledPositions[y * gridWidth + x + 1], + LeftTop = levelingData.SampledPositions[(y + 1) * gridWidth + x], + RightTop = levelingData.SampledPositions[(y + 1) * gridWidth + x + 1], + }); + } + } + } + + // you can only set this on construction + public List SampledPositions { get; private set; } + + public List Regions { get; private set; } = new List(); + + public void Dispose() + { + } + + public string DoApplyLeveling(string lineBeingSent, Vector3 currentDestination) + { + double extruderDelta = 0; + GCodeFile.GetFirstNumberAfter("E", lineBeingSent, ref extruderDelta); + double feedRate = 0; + GCodeFile.GetFirstNumberAfter("F", lineBeingSent, ref feedRate); + + StringBuilder newLine = new StringBuilder("G1 "); + + if (lineBeingSent.Contains("X") || lineBeingSent.Contains("Y") || lineBeingSent.Contains("Z")) + { + Vector3 outPosition = GetPositionWithZOffset(currentDestination); + + lastDestinationWithLevelingApplied = outPosition; + + newLine = newLine.Append(String.Format("X{0:0.##} Y{1:0.##} Z{2:0.###}", outPosition.X, outPosition.Y, outPosition.Z)); + } + + if (extruderDelta != 0) + { + newLine = newLine.Append(String.Format(" E{0:0.###}", extruderDelta)); + } + + if (feedRate != 0) + { + newLine = newLine.Append(String.Format(" F{0:0.##}", feedRate)); + } + + lineBeingSent = newLine.ToString(); + + return lineBeingSent; + } + + public Vector3 GetPositionWithZOffset(Vector3 currentDestination) + { + Region region = GetCorrectRegion(currentDestination); + + return region.GetPositionWithZOffset(currentDestination); + } + + public Vector2 GetPrintLevelPositionToSample(int index, int gridWidth, int gridHeight) + { + Vector2 bedSize = printerSettings.GetValue(SettingsKey.bed_size); + Vector2 printCenter = printerSettings.GetValue(SettingsKey.print_center); + + switch (printerSettings.GetValue(SettingsKey.bed_shape)) + { + case BedShape.Circular: + Vector2 firstPosition = new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .5); + switch (index) + { + case 0: + return firstPosition; + + case 1: + return Vector2.Rotate(firstPosition, MathHelper.Tau / 3); + + case 2: + return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3); + + default: + throw new IndexOutOfRangeException(); + } + + case BedShape.Rectangular: + default: + switch (index) + { + case 0: + return new Vector2(printCenter.X, printCenter.Y + (bedSize.Y / 2) * .8); + + case 1: + return new Vector2(printCenter.X - (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); + + case 2: + return new Vector2(printCenter.X + (bedSize.X / 2) * .8, printCenter.Y - (bedSize.Y / 2) * .8); + + default: + throw new IndexOutOfRangeException(); + } + } + } + + private Region GetCorrectRegion(Vector3 currentDestination) + { + int bestIndex = 0; + double bestDist = double.PositiveInfinity; + + currentDestination.Z = 0; + for (int regionIndex = 0; regionIndex < Regions.Count; regionIndex++) + { + var dist = (Regions[regionIndex].Center - currentDestination).LengthSquared; + if(dist < bestDist) + { + bestIndex = regionIndex; + bestDist = dist; + } + } + + return Regions[bestIndex]; + } + + public class Region + { + public Vector3 LeftBottom { get; set; } + public Vector3 LeftTop { get; set; } + public Vector3 RightBottom { get; set; } + public Vector3 RightTop { get; set; } + + internal Vector3 Center { get; private set; } + internal Vector3 LeftBottomCenter { get; private set; } + internal Vector3 RightTopCenter { get; private set; } + + internal Plane LeftBottomPlane { get; private set; } + internal Plane RightTopPlane { get; private set; } + + internal Vector3 GetPositionWithZOffset(Vector3 currentDestination) + { + if (LeftBottomPlane.PlaneNormal == Vector3.Zero) + { + InitializePlanes(); + } + + var destinationAtZ0 = new Vector3(currentDestination.X, currentDestination.Y, 0); + + // which triangle to check (distance to the centers) + if ((LeftBottomCenter - destinationAtZ0).LengthSquared < (RightTopCenter - destinationAtZ0).LengthSquared) + { + double hitDistance = LeftBottomPlane.GetDistanceToIntersection(destinationAtZ0, Vector3.UnitZ); + currentDestination.Z += hitDistance; + } + else + { + double hitDistance = RightTopPlane.GetDistanceToIntersection(destinationAtZ0, Vector3.UnitZ); + currentDestination.Z += hitDistance; + } + + return currentDestination; + } + + private void InitializePlanes() + { + LeftBottomPlane = new Plane(LeftBottom, RightBottom, LeftTop); + LeftBottomCenter = (LeftBottom + RightBottom + LeftTop) / 3; + + RightTopPlane = new Plane(RightBottom, RightTop, LeftTop); + RightTopCenter = (RightBottom + RightTop + LeftTop) / 3; + + Center = (LeftBottomCenter + RightTopCenter) / 2; + } + } + } +} \ No newline at end of file diff --git a/MatterControl.csproj b/MatterControl.csproj index 8cf78ffd2..14aabfe25 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -75,6 +75,8 @@ + + @@ -225,7 +227,6 @@ - diff --git a/PrinterCommunication/Io/PrintLevelingStream.cs b/PrinterCommunication/Io/PrintLevelingStream.cs index c12092a04..8a3a85424 100644 --- a/PrinterCommunication/Io/PrintLevelingStream.cs +++ b/PrinterCommunication/Io/PrintLevelingStream.cs @@ -99,27 +99,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io PrintLevelingData levelingData = ActiveSliceSettings.Instance.Helpers.GetPrintLevelingData(); if (levelingData != null) { - switch (levelingData.CurrentPrinterLevelingSystem) - { - case PrintLevelingData.LevelingSystem.Probe3Points: - lineBeingSent = LevelWizard3Point.ApplyLeveling(printerSettings, lineBeingSent, currentDestination.position, PrinterMachineInstruction.MovementTypes.Absolute); - break; - - case PrintLevelingData.LevelingSystem.Probe7PointRadial: - lineBeingSent = LevelWizard7PointRadial.ApplyLeveling(printerSettings, lineBeingSent, currentDestination.position); - break; - - case PrintLevelingData.LevelingSystem.Probe13PointRadial: - lineBeingSent = LevelWizard13PointRadial.ApplyLeveling(printerSettings, lineBeingSent, currentDestination.position); - break; - - case PrintLevelingData.LevelingSystem.Probe3x3Mesh: - lineBeingSent = LevelWizard3x3Mesh.ApplyLeveling(printerSettings, lineBeingSent, currentDestination.position); - break; - - default: - throw new NotImplementedException(); - } + lineBeingSent = LevelWizardBase.ApplyLeveling(printerSettings, lineBeingSent, currentDestination.position); } return lineBeingSent; diff --git a/Queue/OptionsMenu/ExportToFolderProcess.cs b/Queue/OptionsMenu/ExportToFolderProcess.cs index fad000c42..3b5ca2b3b 100644 --- a/Queue/OptionsMenu/ExportToFolderProcess.cs +++ b/Queue/OptionsMenu/ExportToFolderProcess.cs @@ -188,30 +188,14 @@ namespace MatterHackers.MatterControl.PrintQueue #if DEBUG if (instruction.movementType != PrinterMachineInstruction.MovementTypes.Absolute) { - throw new Exception("Radial functions can only execute absolute moves."); + throw new Exception("Leveling functions can only execute absolute moves."); } #endif Vector3 currentDestination = instruction.Position; var printerSettings = ActiveSliceSettings.Instance; - switch (levelingData.CurrentPrinterLevelingSystem) - { - case PrintLevelingData.LevelingSystem.Probe3Points: - instruction.Line = LevelWizard3Point.ApplyLeveling(printerSettings, instruction.Line, currentDestination, instruction.movementType); - break; - - case PrintLevelingData.LevelingSystem.Probe7PointRadial: - instruction.Line = LevelWizard7PointRadial.ApplyLeveling(printerSettings, instruction.Line, currentDestination); - break; - - case PrintLevelingData.LevelingSystem.Probe13PointRadial: - instruction.Line = LevelWizard13PointRadial.ApplyLeveling(printerSettings, instruction.Line, currentDestination); - break; - - default: - throw new NotImplementedException(); - } + instruction.Line = LevelWizardBase.ApplyLeveling(printerSettings, instruction.Line, currentDestination); } unleveledGCode.Save(outputPathAndName); } diff --git a/Submodules/MatterSlice b/Submodules/MatterSlice index 3ca863d64..58899e208 160000 --- a/Submodules/MatterSlice +++ b/Submodules/MatterSlice @@ -1 +1 @@ -Subproject commit 3ca863d643ff47053e74424efd13db13048c7892 +Subproject commit 58899e2087aefac4fe3ea0a480288f37cedb4c74 diff --git a/Tests/MatterControl.Tests/MatterControl/LevelingTests.cs b/Tests/MatterControl.Tests/MatterControl/LevelingTests.cs index 923b80257..539b5a93a 100644 --- a/Tests/MatterControl.Tests/MatterControl/LevelingTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/LevelingTests.cs @@ -42,127 +42,6 @@ namespace MatterControl.Tests.MatterControl [TestFixture] public class LevelingTests { - [Test, Category("Leveling")] - public void Leveling7PointsNeverGetsTooHigh() - { - AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); - MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); - - var printerSettings = ActiveSliceSettings.Instance; - var levelingData = new PrintLevelingData(printerSettings); - - double radius = 100; - levelingData.SampledPositions = new List(); - levelingData.SampledPositions.Add(new Vector3(130.00, 0.00, 0)); - levelingData.SampledPositions.Add(new Vector3(65.00, 112.58, 10)); - levelingData.SampledPositions.Add(new Vector3(-65.00, 112.58, 0)); - levelingData.SampledPositions.Add(new Vector3(-130.00, 0.00, 10)); - levelingData.SampledPositions.Add(new Vector3(-65.00, -112.58, 0)); - levelingData.SampledPositions.Add(new Vector3(65.00, -112.58, 10)); - - levelingData.SampledPositions.Add(new Vector3(0, 0, 0)); - - levelingData.SampledPositions.Add(new Vector3(0, 0, 6)); - - Vector2 bedCenter = Vector2.Zero; - - RadialLevlingFunctions levelingFunctions7Point = new RadialLevlingFunctions(printerSettings, 6, levelingData, bedCenter); - int totalPoints = 2000; - for (int curPoint = 0; curPoint < totalPoints; curPoint++) - { - Vector2 currentTestPoint = new Vector2(radius, 0); - currentTestPoint.Rotate(MathHelper.Tau / totalPoints * curPoint); - Vector3 destPosition = new Vector3(currentTestPoint, 0); - - Vector3 outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - Assert.IsTrue(outPosition.Z <= 10); - - string outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - double outZ = 0; - Assert.IsTrue(GCodeFile.GetFirstNumberAfter("Z", outPositionString, ref outZ)); - Assert.IsTrue(outZ <= 10); - } - } - - [Test, Category("Leveling")] - public void Leveling7PointsCorectInterpolation() - { - AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); - MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); - - var printerSettings = ActiveSliceSettings.Instance; - var levelingData = new PrintLevelingData(printerSettings); - - double radius = 100; - levelingData.SampledPositions = new List(); - Vector2 currentEdgePoint = new Vector2(radius, 0); - for (int i = 0; i < 6; i++) - { - levelingData.SampledPositions.Add(new Vector3(currentEdgePoint, i)); - currentEdgePoint.Rotate(MathHelper.Tau / 6); - } - - levelingData.SampledPositions.Add(new Vector3(0, 0, 6)); - - Vector2 bedCenter = Vector2.Zero; - - RadialLevlingFunctions levelingFunctions7Point = new RadialLevlingFunctions(printerSettings, 6, levelingData, bedCenter); - for (int curPoint = 0; curPoint < 6; curPoint++) - { - int nextPoint = curPoint < 5 ? curPoint + 1 : 0; - - // test actual sample position - Vector2 currentTestPoint = new Vector2(radius, 0); - currentTestPoint.Rotate(MathHelper.Tau / 6 * curPoint); - Vector3 destPosition = new Vector3(currentTestPoint, 0); - Vector3 outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - Assert.AreEqual(outPosition.Z, levelingData.SampledPositions[curPoint].Z, .001); - string outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - Assert.AreEqual(GetGCodeString(outPosition), outPositionString); - - // test mid point between samples - Vector3 midPoint = (levelingData.SampledPositions[curPoint] + levelingData.SampledPositions[nextPoint]) / 2; - currentTestPoint = new Vector2(midPoint.X, midPoint.Y); - destPosition = new Vector3(currentTestPoint, 0); - outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - Assert.AreEqual(outPosition.Z, midPoint.Z, .001); - outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - Assert.AreEqual(GetGCodeString(outPosition), outPositionString); - - // test mid point between samples with offset - Vector3 midPointWithOffset = (levelingData.SampledPositions[curPoint] + levelingData.SampledPositions[nextPoint]) / 2 + new Vector3(0, 0, 3); - currentTestPoint = new Vector2(midPointWithOffset.X, midPointWithOffset.Y); - destPosition = new Vector3(currentTestPoint, 3); - outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - Assert.AreEqual(outPosition.Z, midPointWithOffset.Z, .001); - outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - Assert.AreEqual(GetGCodeString(outPosition), outPositionString); - - // test 1/2 angles (mid way between samples on radius) - currentTestPoint = new Vector2(radius, 0); - currentTestPoint.Rotate(MathHelper.Tau / 6 * (curPoint + .5)); - destPosition = new Vector3(currentTestPoint, 0); - outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - // the center is the higest point so the point on the radius has to be less than the mid point of the sample points (it is lower) - Assert.IsTrue(outPosition.Z < (levelingData.SampledPositions[curPoint].Z + levelingData.SampledPositions[nextPoint].Z) / 2 - .001); - outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - Assert.AreEqual(GetGCodeString(outPosition), outPositionString); - - // test 1/2 to center - currentTestPoint = new Vector2(radius / 2, 0); - currentTestPoint.Rotate(MathHelper.Tau / 6 * curPoint); - destPosition = new Vector3(currentTestPoint, 0); - outPosition = levelingFunctions7Point.GetPositionWithZOffset(destPosition); - Assert.AreEqual(outPosition.Z, (levelingData.SampledPositions[curPoint].Z + levelingData.SampledPositions[6].Z) / 2, .001); - outPositionString = levelingFunctions7Point.DoApplyLeveling(GetGCodeString(destPosition), destPosition); - Assert.AreEqual(GetGCodeString(outPosition), outPositionString); - } - - Vector3 outPosition2 = levelingFunctions7Point.GetPositionWithZOffset(Vector3.Zero); - Assert.AreEqual(outPosition2.Z, levelingData.SampledPositions[6].Z, .001); - } - - [Test, Category("Leveling")] public void LevelingMesh3x3CorectInterpolation() { diff --git a/Tests/MatterControl.Tests/MatterControl/SettingsParseTests.cs b/Tests/MatterControl.Tests/MatterControl/SettingsParseTests.cs index fa99704a7..d37a600ef 100644 --- a/Tests/MatterControl.Tests/MatterControl/SettingsParseTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/SettingsParseTests.cs @@ -20,11 +20,12 @@ namespace MatterControl.Tests.MatterControl { AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); + LevelWizard3Point levelingSolution = new LevelWizard3Point(ActiveSliceSettings.Instance.printer, LevelWizardBase.RuningState.InitialStartupCalibration); var printerSettings = ActiveSliceSettings.Instance; { - var sample0 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 0); - var sample1 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 1); - var sample2 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 2); + var sample0 = levelingSolution.GetPrintLevelPositionToSample(0); + var sample1 = levelingSolution.GetPrintLevelPositionToSample(1); + var sample2 = levelingSolution.GetPrintLevelPositionToSample(2); Assert.AreEqual("200,200", ActiveSliceSettings.Instance.GetValue(SettingsKey.bed_size)); Assert.AreEqual("100,100", ActiveSliceSettings.Instance.GetValue(SettingsKey.print_center)); Assert.AreEqual("rectangular", ActiveSliceSettings.Instance.GetValue(SettingsKey.bed_shape)); @@ -34,49 +35,11 @@ namespace MatterControl.Tests.MatterControl Assert.AreEqual(new Vector2(180, 20), sample2); } - { - // nothing set - var manualPositions = LevelWizardBase.GetManualPositions("", 3); - Assert.IsNull(manualPositions); - - // not enough points - manualPositions = LevelWizardBase.GetManualPositions("0,0:100,50", 3); - Assert.IsNull(manualPositions); - - // too many points - manualPositions = LevelWizardBase.GetManualPositions("0,0:100,0:200,200:50,3", 3); - Assert.IsNull(manualPositions); - - // bad data - manualPositions = LevelWizardBase.GetManualPositions("0,oe:100,0:200,200", 3); - Assert.IsNull(manualPositions); - - // good data - manualPositions = LevelWizardBase.GetManualPositions("0,1:100,2:50,101", 3); - Assert.IsTrue(manualPositions.Count == 3); - Assert.IsTrue(manualPositions[0] == new Vector2(0, 1)); - Assert.IsTrue(manualPositions[1] == new Vector2(100, 2)); - Assert.IsTrue(manualPositions[2] == new Vector2(50, 101)); - - // good data - manualPositions = LevelWizardBase.GetManualPositions("0,1:100,2:50,103:0,4:100,5:50,106:0,7:100,8:50,109", 9); - Assert.IsTrue(manualPositions.Count == 9); - Assert.IsTrue(manualPositions[0] == new Vector2(0, 1)); - Assert.IsTrue(manualPositions[1] == new Vector2(100, 2)); - Assert.IsTrue(manualPositions[2] == new Vector2(50, 103)); - Assert.IsTrue(manualPositions[3] == new Vector2(0, 4)); - Assert.IsTrue(manualPositions[4] == new Vector2(100, 5)); - Assert.IsTrue(manualPositions[5] == new Vector2(50, 106)); - Assert.IsTrue(manualPositions[6] == new Vector2(0, 7)); - Assert.IsTrue(manualPositions[7] == new Vector2(100, 8)); - Assert.IsTrue(manualPositions[8] == new Vector2(50, 109)); - } - { ActiveSliceSettings.Instance.SetValue(SettingsKey.leveling_manual_positions, "1,2:211,3:113,104"); - var sample0 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 0); - var sample1 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 1); - var sample2 = LevelWizardBase.GetPrintLevelPositionToSample(printerSettings, 2); + var sample0 = levelingSolution.GetPrintLevelPositionToSample(0); + var sample1 = levelingSolution.GetPrintLevelPositionToSample(1); + var sample2 = levelingSolution.GetPrintLevelPositionToSample(2); Assert.IsTrue(sample0 == new Vector2(1, 2)); Assert.IsTrue(sample1 == new Vector2(211, 3)); Assert.IsTrue(sample2 == new Vector2(113, 104));