From 218f8d5a15de0ea74aa3daa0afe9c3916f1eb6c1 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Mon, 2 May 2022 07:35:09 -0700 Subject: [PATCH] improved hole behavior --- .../CustomWidgets/SceneOperation.cs | 8 +-- .../DesignTools/Operations/CurveObject3D_3.cs | 40 ++++-------- .../DesignTools/Operations/PinchObject3D_3.cs | 1 + .../DesignTools/Operations/TwistObject3D.cs | 2 + .../OperationSourceContainerObject3D.cs | 44 +++++++++++++ .../View3D/Actions/CombineObject3D_2.cs | 61 ++++++++++++------- .../View3D/Actions/SubtractObject3D_2.cs | 24 +++++++- Submodules/agg-sharp | 2 +- .../PartPreviewTests.cs | 2 +- 9 files changed, 127 insertions(+), 57 deletions(-) diff --git a/MatterControlLib/CustomWidgets/SceneOperation.cs b/MatterControlLib/CustomWidgets/SceneOperation.cs index 82d3cc22f..3d2175d40 100644 --- a/MatterControlLib/CustomWidgets/SceneOperation.cs +++ b/MatterControlLib/CustomWidgets/SceneOperation.cs @@ -99,9 +99,9 @@ namespace MatterHackers.Agg.UI public EventHandler CollapseChanged; - private static string CollapseKey(string opperationGroupName) + private static string CollapseKey(string operationGroupName) { - return $"scene_operation_collapse_{opperationGroupName}"; + return $"scene_operation_collapse_{operationGroupName}"; } public bool Collapse @@ -123,9 +123,9 @@ namespace MatterHackers.Agg.UI public EventHandler VisibleChanged; - private static string VisibleKey(string opperationGroupName) + private static string VisibleKey(string operationGroupName) { - return $"scene_operation_visible_{opperationGroupName}"; + return $"scene_operation_visible_{operationGroupName}"; } public static bool GetVisible(string id, bool defaultIfNotSet) diff --git a/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs b/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs index 3d252b58a..fed67d14f 100644 --- a/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/CurveObject3D_3.cs @@ -69,6 +69,9 @@ namespace MatterHackers.MatterControl.DesignTools Diameter, } + [HideFromEditor] + public Vector3 PostCurveOffset { get; set; } = new Vector3(); + [EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Tabs)] public BendTypes BendType { get; set; } = BendTypes.Angle; @@ -207,7 +210,13 @@ namespace MatterHackers.MatterControl.DesignTools } } - public override Task Rebuild() + public override void Cancel(UndoBuffer undoBuffer) + { + this.Matrix *= Matrix4X4.CreateTranslation(-PostCurveOffset); + base.Cancel(undoBuffer); + } + + public override Task Rebuild() { this.DebugDepth("Rebuild"); @@ -348,36 +357,13 @@ namespace MatterHackers.MatterControl.DesignTools if (firstBuild) { var postAabb = this.GetAxisAlignedBoundingBox(); - this.Matrix *= Matrix4X4.CreateTranslation(initialAabb.Center.X - postAabb.Center.X, + PostCurveOffset = new Vector3(initialAabb.Center.X - postAabb.Center.X, initialAabb.MinXYZ.Y - postAabb.MinXYZ.Y, initialAabb.MinXYZ.Z - postAabb.MinXYZ.Z); + this.Matrix *= Matrix4X4.CreateTranslation(PostCurveOffset); } - var removeItems = Children.Where(c => c.OutputType == PrintOutputTypes.Hole && c.Visible); - if (removeItems.Any()) - { - var keepItems = Children.Where(c => c.OutputType != PrintOutputTypes.Hole && c.Visible); - - // apply any holes before we return - var resultItems = SubtractObject3D_2.DoSubtract(this, - keepItems, - removeItems, - reporter, - cancellationToken.Token); - - RemoveAllButSource(); - - // add back in the results of the hole removal - Children.Modify(list => - { - foreach (var child in resultItems) - { - list.Add(child); - child.Visible = true; - } - }); - } - + ApplyHoles(reporter, cancellationToken.Token); this.cancellationToken = null; UiThread.RunOnIdle(() => diff --git a/MatterControlLib/DesignTools/Operations/PinchObject3D_3.cs b/MatterControlLib/DesignTools/Operations/PinchObject3D_3.cs index 3db90225a..f52428dd9 100644 --- a/MatterControlLib/DesignTools/Operations/PinchObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/PinchObject3D_3.cs @@ -115,6 +115,7 @@ namespace MatterHackers.MatterControl.DesignTools // set the matrix back Matrix = currentMatrix; SourceContainer.Visible = false; + ApplyHoles(reporter, cancellationToken.Token); rebuildLocks.Dispose(); Invalidate(InvalidateType.DisplayValues); diff --git a/MatterControlLib/DesignTools/Operations/TwistObject3D.cs b/MatterControlLib/DesignTools/Operations/TwistObject3D.cs index 598d7978d..f518af265 100644 --- a/MatterControlLib/DesignTools/Operations/TwistObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/TwistObject3D.cs @@ -331,6 +331,8 @@ namespace MatterHackers.MatterControl.DesignTools list.AddRange(twistedChildren); }); + ApplyHoles(reporter, cancellationToken.Token); + UiThread.RunOnIdle(() => { rebuildLocks.Dispose(); diff --git a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs index a17ac147a..5ddd7d5e9 100644 --- a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs @@ -37,6 +37,7 @@ using MatterHackers.Agg.UI; using MatterHackers.DataConverters3D; using MatterHackers.DataConverters3D.UndoCommands; using MatterHackers.Localizations; +using MatterHackers.MatterControl.PartPreviewWindow.View3D; using MatterHackers.PolygonMesh; using Newtonsoft.Json; @@ -320,6 +321,49 @@ namespace MatterHackers.MatterControl.DesignTools.Operations this.Invalidate(InvalidateType.Children); } + + /// + /// Use after source itmes have been processed to modify the transformed children of the inherited operation. + /// Example: A bend operation has been applied and there are still holes, this will remove the holes. + /// + /// Show progress + /// Can check if the operation has been canceled + /// Did any holes get subtracted + public bool ApplyHoles(IProgress reporter, + CancellationToken cancellationToken) + { + var removeItems = Children.Where(c => c.OutputType == PrintOutputTypes.Hole && c.Visible); + if (removeItems.Any()) + { + var keepItems = Children.Where(c => c.OutputType != PrintOutputTypes.Hole && c.Visible); + + if (keepItems.Any()) + { + // apply any holes before we return + var resultItems = SubtractObject3D_2.DoSubtract(null, + keepItems, + removeItems, + reporter, + cancellationToken); + + RemoveAllButSource(); + + // add back in the results of the hole removal + Children.Modify(list => + { + foreach (var child in resultItems) + { + list.Add(child); + child.Visible = true; + } + }); + + return true; + } + } + + return false; + } } public class OperationSourceObject3D : Object3D diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs index 0404ec87a..4434acc35 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs @@ -129,21 +129,49 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D SourceContainer.Visible = true; RemoveAllButSource(); - var participants = SourceContainer.VisibleMeshes(); - if (participants.Count() < 2) + Mesh resultsMesh = null; + var participants = SourceContainer.VisibleMeshes().Where(m => m.WorldOutputType(this) != PrintOutputTypes.Hole); + if (participants.Count() == 0) { - if (participants.Count() == 1) - { - var newMesh = new Object3D(); - newMesh.CopyProperties(participants.First(), Object3DPropertyFlags.All); - newMesh.Mesh = participants.First().Mesh; - this.Children.Add(newMesh); - SourceContainer.Visible = false; - } - return; } + else + { + resultsMesh = CombineParticipanets(reporter, participants, cancellationToken); + } + var resultsItem = new Object3D() + { + Mesh = resultsMesh + }; + + if (resultsMesh != null) + { + var holes = SourceContainer.VisibleMeshes().Where(m => m.WorldOutputType(this) == PrintOutputTypes.Hole); + if (holes != null) + { + var holesMesh = CombineParticipanets(null, holes, cancellationToken); + var holesItem = new Object3D() + { + Mesh = holesMesh + }; + var resultItems = SubtractObject3D_2.DoSubtract(null, + new List() { resultsItem }, + new List() { holesItem }, + null, + cancellationToken); + + resultsItem.Mesh = resultItems.First().Mesh; + } + } + + resultsItem.CopyProperties(participants.First(), Object3DPropertyFlags.All & (~Object3DPropertyFlags.Matrix)); + this.Children.Add(resultsItem); + SourceContainer.Visible = false; + } + + private Mesh CombineParticipanets(IProgress reporter, IEnumerable participants, CancellationToken cancellationToken) + { List> touchingSets = GetTouchingMeshes(participants); var totalOperations = touchingSets.Sum(t => t.Count); @@ -233,16 +261,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D } } - if (resultsMesh != null) - { - var resultsItem = new Object3D() - { - Mesh = resultsMesh - }; - resultsItem.CopyProperties(participants.First(), Object3DPropertyFlags.All & (~Object3DPropertyFlags.Matrix)); - this.Children.Add(resultsItem); - SourceContainer.Visible = false; - } + return resultsMesh; } private List> GetTouchingMeshes(IEnumerable participants) diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs index 344c72aa3..7d2b10e2f 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs @@ -341,15 +341,25 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D }; var resultsMesh = keep.Mesh; - var keepWorldMatrix = keep.WorldMatrix(sourceContainer); + var keepWorldMatrix = keep.Matrix; + if (sourceContainer != null) + { + keepWorldMatrix = keep.WorldMatrix(sourceContainer); + } foreach (var remove in removeItems) { + var removeWorldMatrix = remove.Matrix; + if (sourceContainer != null) + { + removeWorldMatrix = remove.WorldMatrix(sourceContainer); + } + resultsMesh = BooleanProcessing.Do(resultsMesh, keepWorldMatrix, // other mesh remove.Mesh, - remove.WorldMatrix(sourceContainer), + removeWorldMatrix, // operation type CsgModes.Subtract, processingMode, @@ -381,7 +391,15 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D }; // copy all the properties but the matrix - resultsItem.CopyWorldProperties(keep, sourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible))); + if (sourceContainer != null) + { + resultsItem.CopyWorldProperties(keep, sourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible))); + } + else + { + resultsItem.CopyProperties(keep, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible))); + } + // and add it to this results.Add(resultsItem); } diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 8d5c8129f..365d3ed68 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 8d5c8129f1ab281e613a7eef0f05a24074371d90 +Subproject commit 365d3ed687c1502e0d5f12d36ae79b03b92ba9e0 diff --git a/Tests/MatterControl.AutomationTests/PartPreviewTests.cs b/Tests/MatterControl.AutomationTests/PartPreviewTests.cs index 34f8a0b0c..59c8fabf1 100644 --- a/Tests/MatterControl.AutomationTests/PartPreviewTests.cs +++ b/Tests/MatterControl.AutomationTests/PartPreviewTests.cs @@ -420,7 +420,7 @@ namespace MatterHackers.MatterControl.Tests.Automation } [Test] - public async Task DesignTabFileOpperations() + public async Task DesignTabFileOperations() { await MatterControlUtilities.RunTest((testRunner) => {