From dd3912a50440165fca433545d6e9d9efec9f8198 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sun, 5 Dec 2021 22:01:50 -0800 Subject: [PATCH] Adding ability to cancel rebuilding --- .../ApplicationView/ApplicationController.cs | 10 +++---- .../ApplicationView/Config/BedConfig.cs | 10 +++---- .../Config/RunningTasksConfig.cs | 4 +-- .../ApplicationView/ISceneContext.cs | 2 +- .../SetupWizards/XyCalibrationWizard.cs | 2 +- .../CustomWidgets/ExportPrintItemPage.cs | 8 +++--- .../Braille/BrailleCardObject3D.cs | 1 + .../DesignTools/Braille/BrailleObject3D.cs | 3 ++- .../Parts/RibbonWithNameObject3D.cs | 2 +- .../DesignTools/LithophaneObject3D.cs | 21 +++++++++++++-- .../DesignTools/Obsolete/CombineObject3D.cs | 7 ++--- .../DesignTools/Obsolete/CurveObject3D.cs | 3 ++- .../Obsolete/FitToBoundsObject3D.cs | 5 +++- .../Obsolete/IntersectionObject3D.cs | 7 ++--- .../DesignTools/Obsolete/MirrorObject3D.cs | 1 + .../DesignTools/Obsolete/PinchObject3D.cs | 1 + .../DesignTools/Obsolete/RotateObject3D.cs | 1 + .../OpenSCAD/OpenScadScriptObject3D.cs | 1 + .../DesignTools/Operations/AlignObject3D.cs | 1 + .../Operations/ArrayAdvancedObject3D.cs | 1 + .../Operations/ArrayLinearObject3D.cs | 1 + .../Operations/ArrayRadialObject3D.cs | 1 + .../DesignTools/Operations/CurveObject3D_2.cs | 1 + .../DesignTools/Operations/CurveObject3D_3.cs | 18 ++++++++++++- .../Operations/DecimateObject3D.cs | 1 + .../Operations/FindSliceObject3D.cs | 1 + .../Operations/FitToBoundsObject3D_2.cs | 2 ++ .../Operations/FitToBoundsObject3D_3.cs | 1 + .../Operations/FitToCylinderObject3D.cs | 1 + .../Operations/HollowOutObject3D.cs | 1 + .../Operations/Image/ImageToPathObject3D.cs | 1 + .../Operations/Image/ImageToPathObject3D_2.cs | 1 + .../Operations/MirrorObject3D_2.cs | 1 + .../Operations/Path/InflatePathObject3D.cs | 1 + .../Operations/Path/LinearExtrudeObject3D.cs | 1 + .../Operations/Path/MergePathObject3D.cs | 5 ++-- .../Operations/Path/OutlinePathObject3D.cs | 1 + .../Operations/Path/RevolveObject3D.cs | 18 ++++++++++++- .../Operations/Path/SmoothPathObject3D.cs | 1 + .../Operations/PlaneCutObject3D.cs | 1 + .../DesignTools/Operations/RepairObject3D.cs | 5 ++-- .../Operations/RotateObject3D_2.cs | 1 + .../DesignTools/Operations/ScaleObject3D.cs | 1 + .../DesignTools/Operations/ScaleObject3D_2.cs | 1 + .../DesignTools/Operations/ScaleObject3D_3.cs | 1 + .../Operations/TransformWrapperObject3D.cs | 3 ++- .../Operations/TranslateObject3D.cs | 1 + .../DesignTools/Operations/TwistObject3D.cs | 1 + .../DesignTools/Primitives/BaseObject3D.cs | 1 + .../DesignTools/Primitives/BoxPathObject3D.cs | 1 + .../DesignTools/Primitives/ConeObject3D.cs | 1 + .../DesignTools/Primitives/CubeObject3D.cs | 5 +++- .../Primitives/CylinderObject3D.cs | 1 + .../Primitives/DescriptionObject3D.cs | 1 + .../Primitives/HalfCylinderObject3D.cs | 1 + .../Primitives/HalfSphereObject3D.cs | 1 + .../Primitives/HalfWedgeObject3D.cs | 1 + .../DesignTools/Primitives/ImageObject3D.cs | 1 + .../Primitives/MarchingSquaresObject3D.cs | 1 + .../Primitives/MeasureToolObject3D.cs | 1 + .../OperationSourceContainerObject3D.cs | 4 +-- .../DesignTools/Primitives/PyramidObject3D.cs | 1 + .../DesignTools/Primitives/RingObject3D.cs | 1 + .../Primitives/SetTemperatureObject3D.cs | 1 + .../DesignTools/Primitives/SphereObject3D.cs | 1 + .../Primitives/TemperatureTowerObject3D.cs | 1 + .../DesignTools/Primitives/TextObject3D.cs | 1 + .../Primitives/TextPathObject3D.cs | 1 + .../DesignTools/Primitives/TorusObject3D.cs | 1 + .../DesignTools/Primitives/WedgeObject3D.cs | 1 + .../DesignTools/Primitives/WedgeObject3D_2.cs | 1 + .../Primitives/XyCalibrationFaceObject3D.cs | 1 + .../Primitives/XyCalibrationTabObject3D.cs | 1 + .../DesignTools/SupportGenerator.cs | 5 ++++ MatterControlLib/HelpIndex.cs | 4 +-- .../Library/Export/GCodeExport.cs | 2 +- .../View3D/Actions/CombineObject3D_2.cs | 26 +++++++++++++++---- .../View3D/Actions/IntersectionObject3D_2.cs | 20 ++++++++++++-- .../Actions/SubtractAndReplaceObject3D.cs | 5 ++-- .../Actions/SubtractAndReplaceObject3D_2.cs | 22 ++++++++++++++-- .../View3D/Actions/SubtractObject3D.cs | 5 ++-- .../View3D/Actions/SubtractObject3D_2.cs | 22 +++++++++++++--- .../View3D/Actions/SubtractPathObject3D.cs | 5 ++-- .../PartPreviewWindow/View3D/SceneActions.cs | 8 +++--- Submodules/MatterSlice | 2 +- Submodules/agg-sharp | 2 +- 86 files changed, 257 insertions(+), 67 deletions(-) diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index 11f8576f1..8bce29739 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -2171,14 +2171,14 @@ namespace MatterHackers.MatterControl printer.ViewState.SlicingItem = true; - await this.Tasks.Execute("Slicing".Localize(), printer, async (reporter, cancellationToken) => + await this.Tasks.Execute("Slicing".Localize(), printer, async (reporter, cancellationTokenSource) => { slicingSucceeded = await Slicer.SliceItem( object3D, gcodeFilePath, printer, reporter, - cancellationToken); + cancellationTokenSource.Token); }); printer.ViewState.SlicingItem = false; @@ -2217,13 +2217,13 @@ namespace MatterHackers.MatterControl } } - await this.Tasks.Execute("Loading GCode".Localize(), printer, (innerProgress, token) => + await this.Tasks.Execute("Loading GCode".Localize(), printer, (innerProgress, concelationTokenSource) => { var status = new ProgressStatus(); innerProgress.Report(status); - printer.Bed.LoadActiveSceneGCode(gcodeFilePath, token, (progress0to1, statusText) => + printer.Bed.LoadActiveSceneGCode(gcodeFilePath, concelationTokenSource.Token, (progress0to1, statusText) => { UiThread.RunOnIdle(() => { @@ -2434,7 +2434,7 @@ namespace MatterHackers.MatterControl public int Priority { get; set; } - public Func, CancellationToken, Task> Action { get; set; } + public Func, CancellationTokenSource, Task> Action { get; set; } } public class StartupAction diff --git a/MatterControlLib/ApplicationView/Config/BedConfig.cs b/MatterControlLib/ApplicationView/Config/BedConfig.cs index d967e1a0f..24d439467 100644 --- a/MatterControlLib/ApplicationView/Config/BedConfig.cs +++ b/MatterControlLib/ApplicationView/Config/BedConfig.cs @@ -159,12 +159,12 @@ namespace MatterHackers.MatterControl public async Task LoadGCodeContent(Stream stream) { - await ApplicationController.Instance.Tasks.Execute("Loading G-Code".Localize(), Printer, (reporter, cancellationToken) => + await ApplicationController.Instance.Tasks.Execute("Loading G-Code".Localize(), Printer, (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); - this.LoadGCode(stream, cancellationToken, (progress0To1, status) => + this.LoadGCode(stream, cancellationTokenSource.Token, (progress0To1, status) => { progressStatus.Status = status; progressStatus.Progress0To1 = progress0To1; @@ -330,7 +330,7 @@ namespace MatterHackers.MatterControl await insertionGroup.LoadingItemsTask; // Persist changes - await this.SaveChanges(null, CancellationToken.None); + await this.SaveChanges(null, null); // Slice and print await ApplicationController.Instance.PrintPart( @@ -650,9 +650,9 @@ namespace MatterHackers.MatterControl /// Persists modified meshes to assets and saves pending changes back to the EditContext /// /// Allows for progress reporting - /// Allows for cancellation during processing + /// Allows for cancellation during processing /// A task representing success - public async Task SaveChanges(IProgress progress, CancellationToken cancellationToken) + public async Task SaveChanges(IProgress progress, CancellationTokenSource cancellationTokenSource) { var progressStatus = new ProgressStatus() { diff --git a/MatterControlLib/ApplicationView/Config/RunningTasksConfig.cs b/MatterControlLib/ApplicationView/Config/RunningTasksConfig.cs index 17af4369d..519ef7471 100644 --- a/MatterControlLib/ApplicationView/Config/RunningTasksConfig.cs +++ b/MatterControlLib/ApplicationView/Config/RunningTasksConfig.cs @@ -100,7 +100,7 @@ namespace MatterHackers.MatterControl }; } - public Task Execute(string taskTitle, object owner, Func, CancellationToken, Task> func, RunningTaskOptions taskActions = null) + public Task Execute(string taskTitle, object owner, Func, CancellationTokenSource, Task> func, RunningTaskOptions taskActions = null) { var tokenSource = new CancellationTokenSource(); @@ -117,7 +117,7 @@ namespace MatterHackers.MatterControl { try { - await func?.Invoke(taskDetails, tokenSource.Token); + await func?.Invoke(taskDetails, tokenSource); } catch { diff --git a/MatterControlLib/ApplicationView/ISceneContext.cs b/MatterControlLib/ApplicationView/ISceneContext.cs index b043d2776..321e302eb 100644 --- a/MatterControlLib/ApplicationView/ISceneContext.cs +++ b/MatterControlLib/ApplicationView/ISceneContext.cs @@ -82,7 +82,7 @@ namespace MatterHackers.MatterControl Task LoadLibraryContent(ILibraryItem libraryItem); - Task SaveChanges(IProgress progress, CancellationToken cancellationToken); + Task SaveChanges(IProgress progress, CancellationTokenSource cancellationToken); // TODO: Isolate printer specifics from ISceneContext diff --git a/MatterControlLib/ConfigurationPage/PrintLeveling/SetupWizards/XyCalibrationWizard.cs b/MatterControlLib/ConfigurationPage/PrintLeveling/SetupWizards/XyCalibrationWizard.cs index 77178ce49..ebd1177e8 100644 --- a/MatterControlLib/ConfigurationPage/PrintLeveling/SetupWizards/XyCalibrationWizard.cs +++ b/MatterControlLib/ConfigurationPage/PrintLeveling/SetupWizards/XyCalibrationWizard.cs @@ -153,7 +153,7 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling originalEditContext = printer.Bed.EditContext; Task.Run(() => { - printer.Bed.SaveChanges(null, CancellationToken.None); + printer.Bed.SaveChanges(null, null); }); // loop until we are done calibrating diff --git a/MatterControlLib/CustomWidgets/ExportPrintItemPage.cs b/MatterControlLib/CustomWidgets/ExportPrintItemPage.cs index 8450584a8..21dedbd05 100644 --- a/MatterControlLib/CustomWidgets/ExportPrintItemPage.cs +++ b/MatterControlLib/CustomWidgets/ExportPrintItemPage.cs @@ -230,12 +230,12 @@ namespace MatterHackers.MatterControl ApplicationController.Instance.Tasks.Execute( "Saving".Localize() + "...", printer, - async (reporter, cancellationToken) => + async (reporter, cancellationTokenSource) => { string path = openParams.FolderPath; if (!string.IsNullOrEmpty(path)) { - await exportPlugin.Generate(libraryItems, path, reporter, cancellationToken); + await exportPlugin.Generate(libraryItems, path, reporter, cancellationTokenSource.Token); } }); }); @@ -264,7 +264,7 @@ namespace MatterHackers.MatterControl ApplicationController.Instance.Tasks.Execute( "Exporting".Localize() + "...", printer, - async (reporter, cancellationToken) => + async (reporter, cancellationTokenSource) => { string extension = Path.GetExtension(savePath); if (!extension.Equals(targetExtension, StringComparison.OrdinalIgnoreCase)) @@ -281,7 +281,7 @@ namespace MatterHackers.MatterControl gCodeExport.CenterOnBed = centerOnBed; } - exportErrors = await exportPlugin.Generate(libraryItems, savePath, reporter, cancellationToken); + exportErrors = await exportPlugin.Generate(libraryItems, savePath, reporter, cancellationTokenSource.Token); } if (exportErrors == null || exportErrors.Count == 0) diff --git a/MatterControlLib/DesignTools/Braille/BrailleCardObject3D.cs b/MatterControlLib/DesignTools/Braille/BrailleCardObject3D.cs index 6820ba32f..409945c54 100644 --- a/MatterControlLib/DesignTools/Braille/BrailleCardObject3D.cs +++ b/MatterControlLib/DesignTools/Braille/BrailleCardObject3D.cs @@ -133,6 +133,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); } } diff --git a/MatterControlLib/DesignTools/Braille/BrailleObject3D.cs b/MatterControlLib/DesignTools/Braille/BrailleObject3D.cs index 7aee7afcb..abf1b1c48 100644 --- a/MatterControlLib/DesignTools/Braille/BrailleObject3D.cs +++ b/MatterControlLib/DesignTools/Braille/BrailleObject3D.cs @@ -263,8 +263,9 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); - return Task.CompletedTask; + return base.Rebuild(); } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/EditorTools/DesignApps/Parts/RibbonWithNameObject3D.cs b/MatterControlLib/DesignTools/EditorTools/DesignApps/Parts/RibbonWithNameObject3D.cs index ff929cf5b..a39b4971d 100644 --- a/MatterControlLib/DesignTools/EditorTools/DesignApps/Parts/RibbonWithNameObject3D.cs +++ b/MatterControlLib/DesignTools/EditorTools/DesignApps/Parts/RibbonWithNameObject3D.cs @@ -86,7 +86,7 @@ namespace MatterHackers.MatterControl.DesignTools this.Mesh = null; this.Invalidate(InvalidateType.Children); - return Task.CompletedTask; + return base.Rebuild(); } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/LithophaneObject3D.cs b/MatterControlLib/DesignTools/LithophaneObject3D.cs index 5554f227d..dab12d8d1 100644 --- a/MatterControlLib/DesignTools/LithophaneObject3D.cs +++ b/MatterControlLib/DesignTools/LithophaneObject3D.cs @@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project. using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; +using System.Threading; using System.Threading.Tasks; using MatterHackers.Agg; using MatterHackers.Agg.Platform; @@ -88,6 +89,19 @@ namespace MatterHackers.MatterControl.Plugins.Lithophane } } + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -110,8 +124,9 @@ namespace MatterHackers.MatterControl.Plugins.Lithophane var rebuildLocks = this.RebuilLockAll(); - ApplicationController.Instance.Tasks.Execute("Generating Lithophane".Localize(), null, (reporter, cancellationToken) => + ApplicationController.Instance.Tasks.Execute("Generating Lithophane".Localize(), null, (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var generatedMesh = Lithophane.Generate( new Lithophane.ImageBufferImageData(activeImage, this.Width), this.Height, @@ -132,16 +147,18 @@ namespace MatterHackers.MatterControl.Plugins.Lithophane // Apply offset this.Matrix *= Matrix4X4.CreateTranslation(-this.ImageOffset); + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); return Task.CompletedTask; }); - return Task.CompletedTask; + return base.Rebuild(); } } } diff --git a/MatterControlLib/DesignTools/Obsolete/CombineObject3D.cs b/MatterControlLib/DesignTools/Obsolete/CombineObject3D.cs index 92598f72d..1609d7aaa 100644 --- a/MatterControlLib/DesignTools/Obsolete/CombineObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/CombineObject3D.cs @@ -84,14 +84,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Combine".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Combine(cancellationToken, reporter); + Combine(cancellationTokenSource.Token, reporter); } catch { @@ -100,10 +100,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); - return Task.CompletedTask; + return base.Rebuild(); }); } diff --git a/MatterControlLib/DesignTools/Obsolete/CurveObject3D.cs b/MatterControlLib/DesignTools/Obsolete/CurveObject3D.cs index cffd9de43..9eba328eb 100644 --- a/MatterControlLib/DesignTools/Obsolete/CurveObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/CurveObject3D.cs @@ -194,10 +194,11 @@ namespace MatterHackers.MatterControl.DesignTools Matrix = currentMatrix; } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); Invalidate(InvalidateType.DisplayValues); - return Task.CompletedTask; + return base.Rebuild(); } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Obsolete/FitToBoundsObject3D.cs b/MatterControlLib/DesignTools/Obsolete/FitToBoundsObject3D.cs index 1aa5ae149..abbabf4cf 100644 --- a/MatterControlLib/DesignTools/Obsolete/FitToBoundsObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/FitToBoundsObject3D.cs @@ -120,6 +120,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations }); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); } @@ -141,6 +142,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations }); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); } @@ -200,8 +202,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); - return Task.CompletedTask; + return base.Rebuild(); } public override AxisAlignedBoundingBox GetAxisAlignedBoundingBox(Matrix4X4 matrix) diff --git a/MatterControlLib/DesignTools/Obsolete/IntersectionObject3D.cs b/MatterControlLib/DesignTools/Obsolete/IntersectionObject3D.cs index da8a3c291..c8612d15a 100644 --- a/MatterControlLib/DesignTools/Obsolete/IntersectionObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/IntersectionObject3D.cs @@ -78,14 +78,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { var rebuildLocks = this.RebuilLockAll(); - return ApplicationController.Instance.Tasks.Execute("Intersection".Localize(), null, (reporter, cancellationToken) => + return ApplicationController.Instance.Tasks.Execute("Intersection".Localize(), null, (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Intersect(cancellationToken, reporter); + Intersect(cancellationTokenSource.Token, reporter); } catch { @@ -94,9 +94,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); - return Task.CompletedTask; + return base.Rebuild(); }); } diff --git a/MatterControlLib/DesignTools/Obsolete/MirrorObject3D.cs b/MatterControlLib/DesignTools/Obsolete/MirrorObject3D.cs index 260d3cc9f..75055b7b0 100644 --- a/MatterControlLib/DesignTools/Obsolete/MirrorObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/MirrorObject3D.cs @@ -93,6 +93,7 @@ namespace MatterHackers.MatterControl.DesignTools this.Matrix = oldMatrix; } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Obsolete/PinchObject3D.cs b/MatterControlLib/DesignTools/Obsolete/PinchObject3D.cs index 8fdd446ea..0b2c98d55 100644 --- a/MatterControlLib/DesignTools/Obsolete/PinchObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/PinchObject3D.cs @@ -125,6 +125,7 @@ namespace MatterHackers.MatterControl.DesignTools Matrix = currentMatrix; } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Obsolete/RotateObject3D.cs b/MatterControlLib/DesignTools/Obsolete/RotateObject3D.cs index 155b7baee..8dd2fc683 100644 --- a/MatterControlLib/DesignTools/Obsolete/RotateObject3D.cs +++ b/MatterControlLib/DesignTools/Obsolete/RotateObject3D.cs @@ -93,6 +93,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/OpenSCAD/OpenScadScriptObject3D.cs b/MatterControlLib/DesignTools/OpenSCAD/OpenScadScriptObject3D.cs index 0d500fb36..7991506da 100644 --- a/MatterControlLib/DesignTools/OpenSCAD/OpenScadScriptObject3D.cs +++ b/MatterControlLib/DesignTools/OpenSCAD/OpenScadScriptObject3D.cs @@ -91,6 +91,7 @@ namespace MatterHackers.MatterControl.Library Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/AlignObject3D.cs b/MatterControlLib/DesignTools/Operations/AlignObject3D.cs index 0aa947c86..102992511 100644 --- a/MatterControlLib/DesignTools/Operations/AlignObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/AlignObject3D.cs @@ -386,6 +386,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations }); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/ArrayAdvancedObject3D.cs b/MatterControlLib/DesignTools/Operations/ArrayAdvancedObject3D.cs index 41948180a..e5058a48e 100644 --- a/MatterControlLib/DesignTools/Operations/ArrayAdvancedObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/ArrayAdvancedObject3D.cs @@ -108,6 +108,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations UiThread.RunOnIdle(() => { rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/ArrayLinearObject3D.cs b/MatterControlLib/DesignTools/Operations/ArrayLinearObject3D.cs index a2546cbfd..02ad761f6 100644 --- a/MatterControlLib/DesignTools/Operations/ArrayLinearObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/ArrayLinearObject3D.cs @@ -98,6 +98,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations SourceContainer.Visible = false; rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/ArrayRadialObject3D.cs b/MatterControlLib/DesignTools/Operations/ArrayRadialObject3D.cs index 35463882b..cdee91d9e 100644 --- a/MatterControlLib/DesignTools/Operations/ArrayRadialObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/ArrayRadialObject3D.cs @@ -135,6 +135,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations UiThread.RunOnIdle(() => { rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs b/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs index 212d354e4..986c035aa 100644 --- a/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs @@ -227,6 +227,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs b/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs index d8ebf1974..2014dc20b 100644 --- a/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs @@ -162,6 +162,19 @@ namespace MatterHackers.MatterControl.DesignTools } } + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -197,8 +210,9 @@ namespace MatterHackers.MatterControl.DesignTools return ApplicationController.Instance.Tasks.Execute( "Curve".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var sourceAabb = this.SourceContainer.GetAxisAlignedBoundingBox(); var radius = diameter / 2; @@ -295,10 +309,12 @@ namespace MatterHackers.MatterControl.DesignTools list.AddRange(curvedChildren); }); + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs b/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs index bce3bae83..e3e2b25f9 100644 --- a/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs @@ -172,6 +172,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/FindSliceObject3D.cs b/MatterControlLib/DesignTools/Operations/FindSliceObject3D.cs index 7e0ab348e..df1a76665 100644 --- a/MatterControlLib/DesignTools/Operations/FindSliceObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/FindSliceObject3D.cs @@ -182,6 +182,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs index 2ce6d9310..393ce053c 100644 --- a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs @@ -223,6 +223,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix | additonalInvalidate)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_3.cs b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_3.cs index 8b10792ee..995590c67 100644 --- a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_3.cs @@ -191,6 +191,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix | additonalInvalidate)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs b/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs index 1789ae34b..616fe9824 100644 --- a/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs @@ -172,6 +172,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/HollowOutObject3D.cs b/MatterControlLib/DesignTools/Operations/HollowOutObject3D.cs index 01857c675..35ce5d3f7 100644 --- a/MatterControlLib/DesignTools/Operations/HollowOutObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/HollowOutObject3D.cs @@ -144,6 +144,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D.cs b/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D.cs index 1be94892f..e84076bb1 100644 --- a/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D.cs @@ -370,6 +370,7 @@ namespace MatterHackers.MatterControl.DesignTools UiThread.RunOnIdle(() => { rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); }); diff --git a/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D_2.cs b/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D_2.cs index 628c8c813..f64515784 100644 --- a/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/Image/ImageToPathObject3D_2.cs @@ -415,6 +415,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLock.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); }); diff --git a/MatterControlLib/DesignTools/Operations/MirrorObject3D_2.cs b/MatterControlLib/DesignTools/Operations/MirrorObject3D_2.cs index f3fd0fc69..73dd0dc6a 100644 --- a/MatterControlLib/DesignTools/Operations/MirrorObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/MirrorObject3D_2.cs @@ -113,6 +113,7 @@ namespace MatterHackers.MatterControl.DesignTools UiThread.RunOnIdle(() => { rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/Path/InflatePathObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/InflatePathObject3D.cs index 4d0cc9806..fbc2e692e 100644 --- a/MatterControlLib/DesignTools/Operations/Path/InflatePathObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/InflatePathObject3D.cs @@ -103,6 +103,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations this.Mesh = this.VertexSource.Extrude(Constants.PathPolygonsHeight); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/Path/LinearExtrudeObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/LinearExtrudeObject3D.cs index fbfccad6b..eccd48449 100644 --- a/MatterControlLib/DesignTools/Operations/Path/LinearExtrudeObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/LinearExtrudeObject3D.cs @@ -189,6 +189,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { rebuildLock.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); }); diff --git a/MatterControlLib/DesignTools/Operations/Path/MergePathObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/MergePathObject3D.cs index 20f2cb74e..8ab65a45f 100644 --- a/MatterControlLib/DesignTools/Operations/Path/MergePathObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/MergePathObject3D.cs @@ -83,14 +83,14 @@ namespace MatterHackers.MatterControl.DesignTools.Operations return ApplicationController.Instance.Tasks.Execute( operationName, null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Merge(reporter, cancellationToken); + Merge(reporter, cancellationTokenSource.Token); } catch { @@ -102,6 +102,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/Path/OutlinePathObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/OutlinePathObject3D.cs index 1be2da7d5..c1306e424 100644 --- a/MatterControlLib/DesignTools/Operations/Path/OutlinePathObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/OutlinePathObject3D.cs @@ -114,6 +114,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/Path/RevolveObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/RevolveObject3D.cs index 4f41598ee..6f4a8f65e 100644 --- a/MatterControlLib/DesignTools/Operations/Path/RevolveObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/RevolveObject3D.cs @@ -150,6 +150,19 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -176,8 +189,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations return ApplicationController.Instance.Tasks.Execute( "Revolve".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var vertexSource = this.VertexSource; var pathBounds = vertexSource.GetBounds(); vertexSource = vertexSource.Translate(-pathBounds.Left - axisPosition, 0); @@ -197,9 +211,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations Mesh = mesh; + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLock.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/Path/SmoothPathObject3D.cs b/MatterControlLib/DesignTools/Operations/Path/SmoothPathObject3D.cs index 1d0c8d172..67d8f1cc4 100644 --- a/MatterControlLib/DesignTools/Operations/Path/SmoothPathObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/Path/SmoothPathObject3D.cs @@ -107,6 +107,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { rebuildLock.Dispose(); this.Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); }); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs b/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs index a23aeefa7..e80a04037 100644 --- a/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs @@ -175,6 +175,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/RepairObject3D.cs b/MatterControlLib/DesignTools/Operations/RepairObject3D.cs index 3183b0b12..29274f8d4 100644 --- a/MatterControlLib/DesignTools/Operations/RepairObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/RepairObject3D.cs @@ -74,7 +74,7 @@ namespace MatterHackers.MatterControl.DesignTools return TaskBuilder( "Repair".Localize(), - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { SourceContainer.Visible = true; RemoveAllButSource(); @@ -88,7 +88,7 @@ namespace MatterHackers.MatterControl.DesignTools var originalMesh = sourceItem.Mesh; inititialFaces += originalMesh.Faces.Count; inititialVertices += originalMesh.Vertices.Count; - var repairedMesh = Repair(originalMesh, cancellationToken); + var repairedMesh = Repair(originalMesh, cancellationTokenSource.Token); finalFaces += repairedMesh.Faces.Count; finalVertices += repairedMesh.Vertices.Count; @@ -111,6 +111,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Operations/RotateObject3D_2.cs b/MatterControlLib/DesignTools/Operations/RotateObject3D_2.cs index 53e1ad050..c6dd6bce6 100644 --- a/MatterControlLib/DesignTools/Operations/RotateObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/RotateObject3D_2.cs @@ -152,6 +152,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations ItemWithTransform.Matrix = RotationMatrix; } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D.cs index 36a729741..920afff86 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D.cs @@ -245,6 +245,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs index 47ccc9133..14f673792 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs @@ -297,6 +297,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs index ab85d48df..d4c84239f 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs @@ -373,6 +373,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Operations/TransformWrapperObject3D.cs b/MatterControlLib/DesignTools/Operations/TransformWrapperObject3D.cs index fc149fa08..8954c5659 100644 --- a/MatterControlLib/DesignTools/Operations/TransformWrapperObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/TransformWrapperObject3D.cs @@ -181,7 +181,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations scene.SelectedItem = this; - + this.CancelAllParentBuilding(); parent?.Invalidate(new InvalidateArgs(parent, InvalidateType.Children)); } @@ -228,6 +228,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations parentLock?.Dispose(); + this.CancelAllParentBuilding(); parent?.Invalidate(new InvalidateArgs(parent, InvalidateType.Children)); } } diff --git a/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs b/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs index 42642f2f3..b98b16885 100644 --- a/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs @@ -106,6 +106,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations ItemWithTransform.Matrix = Matrix4X4.CreateTranslation(Translation); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Matrix)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Operations/TwistObject3D.cs b/MatterControlLib/DesignTools/Operations/TwistObject3D.cs index 2a7d3db67..34b87d82c 100644 --- a/MatterControlLib/DesignTools/Operations/TwistObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/TwistObject3D.cs @@ -326,6 +326,7 @@ namespace MatterHackers.MatterControl.DesignTools UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); Invalidate(InvalidateType.DisplayValues); }); diff --git a/MatterControlLib/DesignTools/Primitives/BaseObject3D.cs b/MatterControlLib/DesignTools/Primitives/BaseObject3D.cs index 6615fbc7e..162a15fb3 100644 --- a/MatterControlLib/DesignTools/Primitives/BaseObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/BaseObject3D.cs @@ -259,6 +259,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLock.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/DesignTools/Primitives/BoxPathObject3D.cs b/MatterControlLib/DesignTools/Primitives/BoxPathObject3D.cs index 5a6fbce8e..5669894f3 100644 --- a/MatterControlLib/DesignTools/Primitives/BoxPathObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/BoxPathObject3D.cs @@ -129,6 +129,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs b/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs index dafcafd45..73fafcafc 100644 --- a/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs @@ -107,6 +107,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs index e7ba0ce1f..732aee4eb 100644 --- a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs @@ -146,8 +146,11 @@ namespace MatterHackers.MatterControl.DesignTools } } + // if any of our parest are re-bulding cancel it as we just changed and they need to start over + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); - return Task.CompletedTask; + + return base.Rebuild(); } public void UpdateControls(PublicPropertyChange change) diff --git a/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs b/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs index 939904e82..a2f75200c 100644 --- a/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs @@ -207,6 +207,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/DescriptionObject3D.cs b/MatterControlLib/DesignTools/Primitives/DescriptionObject3D.cs index 393078d62..ce4ec1016 100644 --- a/MatterControlLib/DesignTools/Primitives/DescriptionObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/DescriptionObject3D.cs @@ -220,6 +220,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs index 7e87459ae..6b93c793a 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs @@ -112,6 +112,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/HalfSphereObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfSphereObject3D.cs index be644530f..952f045db 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfSphereObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfSphereObject3D.cs @@ -135,6 +135,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs index 1d92a84fc..240cb9bb5 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs @@ -102,6 +102,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/ImageObject3D.cs b/MatterControlLib/DesignTools/Primitives/ImageObject3D.cs index f8ab0a3c6..82a1c72b8 100644 --- a/MatterControlLib/DesignTools/Primitives/ImageObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/ImageObject3D.cs @@ -114,6 +114,7 @@ namespace MatterHackers.MatterControl.DesignTools _image = image; // send the invalidate on image change + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Image)); Invalidate(InvalidateType.DisplayValues); } diff --git a/MatterControlLib/DesignTools/Primitives/MarchingSquaresObject3D.cs b/MatterControlLib/DesignTools/Primitives/MarchingSquaresObject3D.cs index 22f6a432e..7e9500eee 100644 --- a/MatterControlLib/DesignTools/Primitives/MarchingSquaresObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/MarchingSquaresObject3D.cs @@ -128,6 +128,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; }); diff --git a/MatterControlLib/DesignTools/Primitives/MeasureToolObject3D.cs b/MatterControlLib/DesignTools/Primitives/MeasureToolObject3D.cs index db186d164..8e1c95ce2 100644 --- a/MatterControlLib/DesignTools/Primitives/MeasureToolObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/MeasureToolObject3D.cs @@ -228,6 +228,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs index 1992ff0f7..4d1084d89 100644 --- a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs @@ -44,8 +44,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { public class OperationSourceContainerObject3D : Object3D { - public static Func, CancellationToken, Task>, Task> TaskBuilder { get; set; } = - (name, func) => Task.Run(() => func(null, CancellationToken.None)); + public static Func, CancellationTokenSource, Task>, Task> TaskBuilder { get; set; } = + (name, func) => Task.Run(() => func(null, null)); public override Mesh Mesh { diff --git a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs index 8ab89e79f..d14b5fd7c 100644 --- a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs @@ -103,6 +103,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/RingObject3D.cs b/MatterControlLib/DesignTools/Primitives/RingObject3D.cs index 64a281211..a5bb27981 100644 --- a/MatterControlLib/DesignTools/Primitives/RingObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/RingObject3D.cs @@ -232,6 +232,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/SetTemperatureObject3D.cs b/MatterControlLib/DesignTools/Primitives/SetTemperatureObject3D.cs index c29c5a4be..24ddc7ce6 100644 --- a/MatterControlLib/DesignTools/Primitives/SetTemperatureObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/SetTemperatureObject3D.cs @@ -105,6 +105,7 @@ namespace MatterHackers.MatterControl.DesignTools UpdateTexture(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/SphereObject3D.cs b/MatterControlLib/DesignTools/Primitives/SphereObject3D.cs index 6f2e50f4b..abde10a61 100644 --- a/MatterControlLib/DesignTools/Primitives/SphereObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/SphereObject3D.cs @@ -156,6 +156,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/TemperatureTowerObject3D.cs b/MatterControlLib/DesignTools/Primitives/TemperatureTowerObject3D.cs index 68c8ad233..ffebecc87 100644 --- a/MatterControlLib/DesignTools/Primitives/TemperatureTowerObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TemperatureTowerObject3D.cs @@ -186,6 +186,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs index 3c1fc5b95..1cce5feff 100644 --- a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs @@ -191,6 +191,7 @@ namespace MatterHackers.MatterControl.DesignTools { rebuildLock.Dispose(); Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); }); diff --git a/MatterControlLib/DesignTools/Primitives/TextPathObject3D.cs b/MatterControlLib/DesignTools/Primitives/TextPathObject3D.cs index 8fe8716ca..288fe10d4 100644 --- a/MatterControlLib/DesignTools/Primitives/TextPathObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TextPathObject3D.cs @@ -153,6 +153,7 @@ namespace MatterHackers.MatterControl.DesignTools this.Mesh = this.VertexSource.Extrude(Constants.PathPolygonsHeight); } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/TorusObject3D.cs b/MatterControlLib/DesignTools/Primitives/TorusObject3D.cs index 10ec5959b..ada6161d2 100644 --- a/MatterControlLib/DesignTools/Primitives/TorusObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TorusObject3D.cs @@ -158,6 +158,7 @@ namespace MatterHackers.MatterControl.DesignTools Invalidate(InvalidateType.DisplayValues); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs index b7ac8d2a1..df3ae3d5d 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs @@ -132,6 +132,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D_2.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D_2.cs index c3ad53507..78dad5a95 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D_2.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D_2.cs @@ -146,6 +146,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; diff --git a/MatterControlLib/DesignTools/Primitives/XyCalibrationFaceObject3D.cs b/MatterControlLib/DesignTools/Primitives/XyCalibrationFaceObject3D.cs index 2c8e34ec7..35ae1b009 100644 --- a/MatterControlLib/DesignTools/Primitives/XyCalibrationFaceObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/XyCalibrationFaceObject3D.cs @@ -157,6 +157,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/Primitives/XyCalibrationTabObject3D.cs b/MatterControlLib/DesignTools/Primitives/XyCalibrationTabObject3D.cs index 530c0b5cd..8975379aa 100644 --- a/MatterControlLib/DesignTools/Primitives/XyCalibrationTabObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/XyCalibrationTabObject3D.cs @@ -149,6 +149,7 @@ namespace MatterHackers.MatterControl.DesignTools } } + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } diff --git a/MatterControlLib/DesignTools/SupportGenerator.cs b/MatterControlLib/DesignTools/SupportGenerator.cs index 2c758e221..ee25a32f6 100644 --- a/MatterControlLib/DesignTools/SupportGenerator.cs +++ b/MatterControlLib/DesignTools/SupportGenerator.cs @@ -144,6 +144,11 @@ namespace MatterHackers.MatterControl.DesignTools } public Task Create(IProgress progress, CancellationToken cancellationToken) + { + return Create(progress, null); + } + + public Task Create(IProgress progress, CancellationTokenSource cancellationTokenSource) { var selectedItem = scene.SelectedItem; diff --git a/MatterControlLib/HelpIndex.cs b/MatterControlLib/HelpIndex.cs index 5115f1ea7..76a30e93d 100644 --- a/MatterControlLib/HelpIndex.cs +++ b/MatterControlLib/HelpIndex.cs @@ -80,11 +80,11 @@ namespace MatterControlLib return ApplicationController.Instance.Tasks.Execute( "Preparing help index".Localize(), null, - (progress, cancellationToken) => + (progress, cancellationTokenSource) => { string relativePath = System.IO.Path.Combine("OemSettings", "help-docs.zip"); - IndexZipFile(relativePath, progress, cancellationToken); + IndexZipFile(relativePath, progress, cancellationTokenSource.Token); return Task.CompletedTask; }); diff --git a/MatterControlLib/Library/Export/GCodeExport.cs b/MatterControlLib/Library/Export/GCodeExport.cs index e5723507b..a5cec98a0 100644 --- a/MatterControlLib/Library/Export/GCodeExport.cs +++ b/MatterControlLib/Library/Export/GCodeExport.cs @@ -254,7 +254,7 @@ namespace MatterHackers.MatterControl.Library.Export Printer, (reporter, cancellationToken2) => { - return Slicer.SliceItem(loadedItem, gcodePath, Printer, reporter, cancellationToken2); + return Slicer.SliceItem(loadedItem, gcodePath, Printer, reporter, cancellationToken2.Token); }); Printer.Settings.SetValue(SettingsKey.spiral_vase, oldSpiralVaseSetting ? "1" : "0"); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs index e9d3e91c2..f1c653540 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs @@ -43,9 +43,11 @@ using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { [ShowUpdateButton(false)] - public class CombineObject3D_2 : OperationSourceContainerObject3D, IPropertyGridModifier + public class CombineObject3D_2 : OperationSourceContainerObject3D, IPropertyGridModifier, IBuildsOnThread { - public CombineObject3D_2() + private CancellationTokenSource cancellationToken; + + public CombineObject3D_2() { Name = "Combine"; } @@ -67,6 +69,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D private IplicitSurfaceMethod MeshAnalysis { get; set; } private ProcessingResolution InputResolution { get; set; } = ProcessingResolution._64; #endif + public bool IsBuilding => this.cancellationToken != null; + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -76,22 +80,25 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Combine".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Combine(cancellationToken, reporter); + Combine(cancellationTokenSource.Token, reporter); } catch { } + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); return Task.CompletedTask; @@ -197,5 +204,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D change.SetRowVisible(nameof(MeshAnalysis), () => Processing != ProcessingModes.Polygons); change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons && MeshAnalysis == IplicitSurfaceMethod.Grid); } - } + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if(threadSafe != null) + { + threadSafe.Cancel(); + } + } + } } diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs index 1d79e5cfc..6cb03e3d1 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs @@ -68,6 +68,19 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D private ProcessingResolution InputResolution { get; set; } = ProcessingResolution._64; #endif + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -77,22 +90,25 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Intersection".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Intersect(cancellationToken, reporter); + Intersect(cancellationTokenSource.Token, reporter); } catch { } + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D.cs index efe8e0c30..57ebc46ee 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D.cs @@ -202,11 +202,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D var rebuildLocks = this.RebuilLockAll(); // spin up a task to calculate the paint - return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(), null, (reporter, cancellationToken) => + return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(), null, (reporter, cancellationTokenSource) => { try { - SubtractAndReplace(cancellationToken, reporter); + SubtractAndReplace(cancellationTokenSource.Token, reporter); } catch { @@ -215,6 +215,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D_2.cs index 1d3e56db6..ce0390a92 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractAndReplaceObject3D_2.cs @@ -129,16 +129,32 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D } } + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { var rebuildLocks = this.RebuilLockAll(); // spin up a task to calculate the paint - return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(), null, (reporter, cancellationToken) => + return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(), + null, + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; try { - SubtractAndReplace(cancellationToken, reporter); + SubtractAndReplace(cancellationTokenSource.Token, reporter); var newComputedChildren = new SelectedChildren(); foreach (var id in SelectedChildren) @@ -152,9 +168,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { } + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D.cs index ebeffbeef..f49eb232d 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D.cs @@ -85,14 +85,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Subtract".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Subtract(cancellationToken, reporter); + Subtract(cancellationTokenSource.Token, reporter); } catch { @@ -101,6 +101,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs index e2adfcdd9..9c9d543e0 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs @@ -45,7 +45,7 @@ using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { [ShowUpdateButton(false)] - public class SubtractObject3D_2 : OperationSourceContainerObject3D, ISelectableChildContainer, ICustomEditorDraw, IPropertyGridModifier + public class SubtractObject3D_2 : OperationSourceContainerObject3D, ISelectableChildContainer, ICustomEditorDraw, IPropertyGridModifier, IBuildsOnThread { public SubtractObject3D_2() { @@ -130,6 +130,19 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D } } + private CancellationTokenSource cancellationToken; + + public bool IsBuilding => this.cancellationToken != null; + + public void CancelBuild() + { + var threadSafe = this.cancellationToken; + if (threadSafe != null) + { + threadSafe.Cancel(); + } + } + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -139,22 +152,25 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Subtract".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { + this.cancellationToken = cancellationTokenSource as CancellationTokenSource; var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Subtract(cancellationToken, reporter); + Subtract(cancellationTokenSource.Token, reporter); } catch { } + this.cancellationToken = null; UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractPathObject3D.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractPathObject3D.cs index c3370c1a0..39d55e3cb 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractPathObject3D.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractPathObject3D.cs @@ -106,14 +106,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D return ApplicationController.Instance.Tasks.Execute( "Subtract".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); try { - Subtract(cancellationToken, reporter); + Subtract(cancellationTokenSource.Token, reporter); } catch { @@ -133,6 +133,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); + this.CancelAllParentBuilding(); Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); }); diff --git a/MatterControlLib/PartPreviewWindow/View3D/SceneActions.cs b/MatterControlLib/PartPreviewWindow/View3D/SceneActions.cs index 27540d7f6..b8abb287a 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/SceneActions.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/SceneActions.cs @@ -78,7 +78,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow await ApplicationController.Instance.Tasks.Execute( "Ungroup".Localize(), null, - (reporter, cancellationToken) => + (reporter, cancellationTokenSource) => { var progressStatus = new ProgressStatus(); reporter.Report(progressStatus); @@ -90,16 +90,16 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // try to cut it up into multiple meshes progressStatus.Status = "Split".Localize(); - var cleanMesh = selectedItem.Mesh.Copy(cancellationToken); + var cleanMesh = selectedItem.Mesh.Copy(cancellationTokenSource.Token); cleanMesh.MergeVertices(.01); - var discreetMeshes = CreateDiscreteMeshes.SplitVolumesIntoMeshes(cleanMesh, cancellationToken, (double progress0To1, string processingState) => + var discreetMeshes = CreateDiscreteMeshes.SplitVolumesIntoMeshes(cleanMesh, cancellationTokenSource.Token, (double progress0To1, string processingState) => { progressStatus.Progress0To1 = .5 + progress0To1 * .5; progressStatus.Status = processingState; reporter.Report(progressStatus); }); - if (cancellationToken.IsCancellationRequested) + if (cancellationTokenSource.IsCancellationRequested) { return Task.CompletedTask; } diff --git a/Submodules/MatterSlice b/Submodules/MatterSlice index 17fb182f8..dc55feeb1 160000 --- a/Submodules/MatterSlice +++ b/Submodules/MatterSlice @@ -1 +1 @@ -Subproject commit 17fb182f8ef221a9fbe94eb831cef9956c6c1ba6 +Subproject commit dc55feeb1e043b0772aa2c21b4d6947e1cccc14f diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index e996ffa5e..8c801ba65 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit e996ffa5eb793d8b7e4d832b9d1b33e08bf0c152 +Subproject commit 8c801ba65845521e2a0e13a00f318952caa80336