From 39af203e959c40f91ef5bc3ad880421b9301ea9e Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Thu, 27 Jan 2022 15:15:32 -0800 Subject: [PATCH] Making better names for collection items (combine, align, subtract, intersect) --- .../ApplicationView/ApplicationController.cs | 4 +- .../ApplicationView/Config/BedConfig.cs | 24 ++++++--- .../ApplicationView/SceneOperations.cs | 2 + .../DesignTools/Operations/AlignObject3D.cs | 34 ++++++------- .../Operations/Object3DExtensions.cs | 12 ----- .../OperationSourceContainerObject3D.cs | 14 +++++ .../Providers/FileSystem/FileSystemItem.cs | 29 +++++++---- .../MatterControl/PlatingHistoryContainer.cs | 20 +++++++- .../PartPreviewWindow/MainViewWidget.cs | 2 +- .../View3D/Actions/CombineObject3D_2.cs | 11 ++++ .../View3D/Actions/IntersectionObject3D_2.cs | 11 ++++ .../View3D/Actions/SubtractObject3D_2.cs | 51 ++----------------- MatterControlLib/RootSystemWindow.cs | 2 + StaticData/Translations/Master.txt | 3 ++ Submodules/agg-sharp | 2 +- 15 files changed, 121 insertions(+), 100 deletions(-) diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index 661b22638..67fafe70d 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -938,7 +938,7 @@ namespace MatterHackers.MatterControl new EditContext() { ContentStore = historyContainer, - SourceItem = historyContainer.NewPlatingItem(onlyPrinter.Bed.Scene) + SourceItem = historyContainer.NewBedPlate(onlyPrinter.Bed) }); UiThread.RunOnIdle(() => @@ -1568,7 +1568,7 @@ namespace MatterHackers.MatterControl await workspace.SceneContext.LoadContent(new EditContext() { ContentStore = history, - SourceItem = history.NewPlatingItem(workspace.SceneContext.Scene) + SourceItem = history.NewBedPlate(workspace.Printer.Bed) }); this.OpenWorkspace(workspace); diff --git a/MatterControlLib/ApplicationView/Config/BedConfig.cs b/MatterControlLib/ApplicationView/Config/BedConfig.cs index 320fdf79d..e02e8802f 100644 --- a/MatterControlLib/ApplicationView/Config/BedConfig.cs +++ b/MatterControlLib/ApplicationView/Config/BedConfig.cs @@ -189,15 +189,23 @@ namespace MatterHackers.MatterControl if (this.Printer != null) { this.Printer.ViewState.ViewMode = PartViewMode.Model; - } - // Load - this.LoadEmptyContent( - new EditContext() - { - ContentStore = historyContainer, - SourceItem = historyContainer.NewPlatingItem(this.Scene) - }); + this.LoadEmptyContent( + new EditContext() + { + ContentStore = historyContainer, + SourceItem = historyContainer.NewBedPlate(this) + }); + } + else + { + this.LoadEmptyContent( + new EditContext() + { + ContentStore = historyContainer, + SourceItem = historyContainer.NewDesign() + }); + } } public InsertionGroupObject3D AddToPlate(IEnumerable itemsToAdd) diff --git a/MatterControlLib/ApplicationView/SceneOperations.cs b/MatterControlLib/ApplicationView/SceneOperations.cs index 4de720636..176d88ed8 100644 --- a/MatterControlLib/ApplicationView/SceneOperations.cs +++ b/MatterControlLib/ApplicationView/SceneOperations.cs @@ -690,6 +690,8 @@ namespace MatterHackers.MatterControl var selectedItem = scene.SelectedItem; var align = new AlignObject3D(); align.AddSelectionAsChildren(scene, selectedItem); + align.Name = align.NameFromChildren(); + align.NameOverriden = false; }, Icon = (theme) => StaticData.Instance.LoadIcon("align_left_dark.png", 16, 16).SetToColor(theme.TextColor).SetPreMultiply(), HelpTextGetter = () => "At least 2 parts must be selected".Localize().Stars(), diff --git a/MatterControlLib/DesignTools/Operations/AlignObject3D.cs b/MatterControlLib/DesignTools/Operations/AlignObject3D.cs index bf3a1578c..c642aab3e 100644 --- a/MatterControlLib/DesignTools/Operations/AlignObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/AlignObject3D.cs @@ -217,23 +217,6 @@ namespace MatterHackers.MatterControl.DesignTools.Operations public override bool CanApply => true; - private List CurrentChildrenBounds - { - get - { - var currentChildrenBounds = new List(); - this.Children.Modify(list => - { - foreach (var child in list) - { - currentChildrenBounds.Add(child.GetAxisAlignedBoundingBox()); - } - }); - - return currentChildrenBounds; - } - } - [JsonIgnore] private Aabb AnchorBounds { @@ -330,6 +313,12 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { await Rebuild(); } + else if (invalidateArgs.InvalidateType.HasFlag(InvalidateType.Name) + && !NameOverriden) + { + Name = NameFromChildren(); + NameOverriden = false; + } else if (SheetObject3D.NeedsRebuild(this, invalidateArgs)) { await Rebuild(); @@ -384,6 +373,12 @@ namespace MatterHackers.MatterControl.DesignTools.Operations AlignAxis(2, ZAlign, GetAlignToOffset(anchorBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset.Value(this), child); } }); + + if (!NameOverriden) + { + Name = NameFromChildren(); + NameOverriden = false; + } } this.CancelAllParentBuilding(); @@ -482,5 +477,10 @@ namespace MatterHackers.MatterControl.DesignTools.Operations throw new NotImplementedException(); } } + + public string NameFromChildren() + { + return CalculateName(this.Children, ", "); + } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Operations/Object3DExtensions.cs b/MatterControlLib/DesignTools/Operations/Object3DExtensions.cs index fed1569ca..40fd30581 100644 --- a/MatterControlLib/DesignTools/Operations/Object3DExtensions.cs +++ b/MatterControlLib/DesignTools/Operations/Object3DExtensions.cs @@ -419,23 +419,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { using (selectedItem.Parent.RebuildLock()) { - var newName = ""; foreach (var item in selectedItems) { - if (newName == "") - { - newName = item.Name; - } - else - { - newName += $" & {item.Name}"; - } - newParent.Children.Add(item.Clone()); } - newParent.Name = newName; - newParent.MakeNameNonColliding(); scene.UndoBuffer.AddAndDo( diff --git a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs index 70b3461f5..e366edf65 100644 --- a/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/OperationSourceContainerObject3D.cs @@ -205,12 +205,23 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { await Rebuild(); } + else if (invalidateArgs.InvalidateType.HasFlag(InvalidateType.Name) + && !NameOverriden) + { + Name = NameFromChildren(); + NameOverriden = false; + } else { base.OnInvalidate(invalidateArgs); } } + public virtual string NameFromChildren() + { + return Name; + } + public override void Cancel(UndoBuffer undoBuffer) { using (RebuildLock()) @@ -286,6 +297,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations new List { this })); await this.Rebuild(); + + Name = NameFromChildren(); + NameOverriden = false; } } diff --git a/MatterControlLib/Library/Providers/FileSystem/FileSystemItem.cs b/MatterControlLib/Library/Providers/FileSystem/FileSystemItem.cs index 6c9b1ca8b..7da4cff97 100644 --- a/MatterControlLib/Library/Providers/FileSystem/FileSystemItem.cs +++ b/MatterControlLib/Library/Providers/FileSystem/FileSystemItem.cs @@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project. using MatterHackers.Agg; using System; using System.IO; +using System.Text.RegularExpressions; namespace MatterHackers.MatterControl.Library { @@ -97,29 +98,35 @@ namespace MatterHackers.MatterControl.Library string sourceFile = this.FilePath; if (File.Exists(sourceFile)) { - string extension = Path.GetExtension(sourceFile); - string destFile = Path.Combine(Path.GetDirectoryName(sourceFile), value); - destFile = Path.ChangeExtension(destFile, extension); + var extension = Path.GetExtension(sourceFile); + var fileNameNumberMatch = new Regex("\\s*\\(\\d+\\)" + extension, RegexOptions.Compiled); + + var directory = Path.GetDirectoryName(sourceFile); + var destName = value; + var destPathAndName = Path.Combine(directory, Path.ChangeExtension(destName, extension)); var uniqueFileIncrement = 0; - while(File.Exists(destFile)) + while(File.Exists(destPathAndName)) { - uniqueFileIncrement++; - destFile = Path.Combine(Path.GetDirectoryName(sourceFile), value + $" ({uniqueFileIncrement})"); - destFile = Path.ChangeExtension(destFile, extension); + // remove any number + destName = fileNameNumberMatch.Replace(sourceFile, ""); + // add the new number + destName += $" ({++uniqueFileIncrement})"; + destName = Path.ChangeExtension(destName, extension); + destPathAndName = Path.Combine(directory, Path.ChangeExtension(destName, extension)); - if (sourceFile == destFile) + if (sourceFile == destPathAndName) { // we have gotten back to the name we currently have (don't change it) break; } } - if (sourceFile != destFile) + if (sourceFile != destPathAndName) { - File.Move(sourceFile, destFile); + File.Move(sourceFile, destPathAndName); - this.FilePath = destFile; + this.FilePath = destPathAndName; NameChanged?.Invoke(this, EventArgs.Empty); } diff --git a/MatterControlLib/Library/Providers/MatterControl/PlatingHistoryContainer.cs b/MatterControlLib/Library/Providers/MatterControl/PlatingHistoryContainer.cs index c642a108f..b81279a49 100644 --- a/MatterControlLib/Library/Providers/MatterControl/PlatingHistoryContainer.cs +++ b/MatterControlLib/Library/Providers/MatterControl/PlatingHistoryContainer.cs @@ -36,6 +36,7 @@ using MatterHackers.Agg.Image; using MatterHackers.DataConverters3D; using MatterHackers.Localizations; using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SlicerConfiguration; namespace MatterHackers.MatterControl.Library { @@ -79,9 +80,26 @@ namespace MatterHackers.MatterControl.Library } } - internal ILibraryItem NewPlatingItem(InteractiveScene scene) + internal ILibraryItem NewBedPlate(BedConfig bedConfig) + { + var name = bedConfig.Printer.Settings.GetValue(SettingsKey.printer_name); + string now = DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss"); + var filename = ApplicationController.Instance.SanitizeFileName($"{name} - {now}.mcx"); + string mcxPath = Path.Combine(this.FullPath, filename); + + File.WriteAllText(mcxPath, new Object3D().ToJson()); + + return new FileSystemFileItem(mcxPath); + } + + internal ILibraryItem NewDesign() { string mcxPath = Path.Combine(this.FullPath, "New Design.mcx"); + var count = 0; + while(File.Exists(mcxPath)) + { + mcxPath = Path.Combine(this.FullPath, $"New Design ({++count}).mcx"); + } File.WriteAllText(mcxPath, new Object3D().ToJson()); diff --git a/MatterControlLib/PartPreviewWindow/MainViewWidget.cs b/MatterControlLib/PartPreviewWindow/MainViewWidget.cs index 3b78a8042..46d2c8382 100644 --- a/MatterControlLib/PartPreviewWindow/MainViewWidget.cs +++ b/MatterControlLib/PartPreviewWindow/MainViewWidget.cs @@ -846,7 +846,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow new EditContext() { ContentStore = ApplicationController.Instance.Library.PlatingHistory, - SourceItem = history.NewPlatingItem(workspace.SceneContext.Scene) + SourceItem = history.NewDesign() }); ApplicationController.Instance.Workspaces.Add(workspace); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs index f1c653540..6c8045469 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/CombineObject3D_2.cs @@ -94,6 +94,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { } + if (!NameOverriden) + { + Name = NameFromChildren(); + NameOverriden = false; + } + this.cancellationToken = null; UiThread.RunOnIdle(() => { @@ -110,6 +116,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D Combine(CancellationToken.None, null); } + public override string NameFromChildren() + { + return CalculateName(SourceContainer.Children, " + "); + } + private void Combine(CancellationToken cancellationToken, IProgress reporter) { SourceContainer.Visible = true; diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs index 6cb03e3d1..24fa8bad2 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/IntersectionObject3D_2.cs @@ -104,6 +104,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D { } + if (!NameOverriden) + { + Name = NameFromChildren(); + NameOverriden = false; + } + this.cancellationToken = null; UiThread.RunOnIdle(() => { @@ -116,6 +122,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D }); } + public override string NameFromChildren() + { + return CalculateName(SourceContainer.Children, " & "); + } + public void Intersect() { Intersect(CancellationToken.None, null); diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs index d07dbad0c..34aa54712 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractObject3D_2.cs @@ -102,13 +102,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D } } - public override void WrapSelectedItemAndSelect(InteractiveScene scene) + public override async void WrapSelectedItemAndSelect(InteractiveScene scene) { base.WrapSelectedItemAndSelect(scene); if (SelectedChildren.Count == 0) { SelectedChildren.Add(SourceContainer.DescendantsAndSelfMultipleChildrenFirstOrSelf().Children.Last().ID); + await this.Rebuild(); } } @@ -376,54 +377,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons && MeshAnalysis == IplicitSurfaceMethod.Grid); } - private string NameFromChildren() + public override string NameFromChildren() { var (keepItems, removeItems) = GetSubtractItems(); - - var name = ""; - if (keepItems != null) - { - foreach (var item in keepItems) - { - if (name == "") - { - name = item.Name; - } - else - { - name += ", " + item.Name; - } - } - } - - if (removeItems != null) - { - var firstRemove = true; - foreach (var item in removeItems) - { - if (name == "") - { - name = item.Name; - } - else - { - if (firstRemove) - { - name += " - "; - } - else - { - name += ", "; - } - - name += item.Name; - } - - firstRemove = false; - } - } - - return name; + return CalculateName(keepItems, ", ", " - ", removeItems, ", "); } } } \ No newline at end of file diff --git a/MatterControlLib/RootSystemWindow.cs b/MatterControlLib/RootSystemWindow.cs index 9a8d78800..b4b879b48 100644 --- a/MatterControlLib/RootSystemWindow.cs +++ b/MatterControlLib/RootSystemWindow.cs @@ -300,6 +300,8 @@ namespace MatterHackers.MatterControl { var application = ApplicationController.Instance; + await ApplicationController.Instance.PersistUserWorkspaceTabs(true); + application.ApplicationExiting = true; // Make sure we tell the Application Controller to shut down. This will release the slicing thread if running. diff --git a/StaticData/Translations/Master.txt b/StaticData/Translations/Master.txt index 3e7206a2e..5c62df422 100644 --- a/StaticData/Translations/Master.txt +++ b/StaticData/Translations/Master.txt @@ -955,6 +955,9 @@ Translated:Edit Selected Setting English:Email Translated:Email +English:Empty +Translated:Empty + English:Empty Bed Translated:Empty Bed diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index e123dc767..940c157f7 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit e123dc76700b6e041069f5ad0e9f189dfeea3311 +Subproject commit 940c157f73bb807176c08e799bf0bb55ba08f2df