mattercontrol/MatterControlLib/PartPreviewWindow/View3D/Actions/SubtractPathObject3D.cs

269 lines
9.8 KiB
C#
Raw Normal View History

2020-10-16 16:25:11 -07:00
/*
2023-03-10 17:15:55 -08:00
Copyright (c) 2023, Lars Brubaker, John Lewin
2020-10-16 16:25:11 -07:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
2023-12-01 11:36:08 -08:00
using MatterControlLib.DesignTools.Operations.Path;
2020-10-16 16:25:11 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DesignTools;
using MatterHackers.MatterControl.DesignTools.Operations;
2021-11-26 12:49:42 -08:00
using MatterHackers.PolygonMesh.Processors;
2020-10-16 16:25:11 -07:00
using MatterHackers.VectorMath;
2023-03-10 17:15:55 -08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2020-10-16 16:25:11 -07:00
namespace MatterHackers.MatterControl.PartPreviewWindow.View3D
{
2023-12-01 09:43:23 -08:00
public class SubtractPathObject3D : OperationSourceContainerObject3D, IEditorDraw, IObject3DControlsProvider, IPrimaryOperationsSpecifier, IPathObject3D
2023-03-10 17:15:55 -08:00
{
public SubtractPathObject3D()
{
Name = "Subtract";
}
[DisplayName("Part(s) to Subtract")]
public SelectedChildren SelectedChildren { get; set; } = new SelectedChildren();
public void DrawEditor(Object3DControlsLayer layer, DrawEventArgs e)
{
this.DrawPath();
}
public AxisAlignedBoundingBox GetEditorWorldspaceAABB(Object3DControlsLayer layer)
{
return this.GetWorldspaceAabbOfDrawPath();
}
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public override async void OnInvalidate(InvalidateArgs invalidateType)
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Children)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Matrix)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Mesh))
&& invalidateType.Source != this
&& !RebuildLocked)
{
await Rebuild();
}
else if (invalidateType.InvalidateType.HasFlag(InvalidateType.Properties)
&& invalidateType.Source == this)
{
await Rebuild();
}
else
{
base.OnInvalidate(invalidateType);
}
}
public override bool CanApply => true;
2023-12-01 09:43:23 -08:00
public bool MeshIsSolidObject => false;
public VertexStorage VertexStorage { get; set; }
2023-03-10 17:15:55 -08:00
public override void Apply(UndoBuffer undoBuffer)
{
this.FlattenToPathObject(undoBuffer);
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");
var rebuildLocks = this.RebuilLockAll();
return ApplicationController.Instance.Tasks.Execute(
"Subtract".Localize(),
null,
(reporter, cancellationTokenSource) =>
{
try
{
Subtract(cancellationTokenSource.Token, reporter);
}
catch
{
}
// set the mesh to show the path
if (this.GetVertexSource() != null)
{
var extrudeMesh = this.GetVertexSource().Extrude(Constants.PathPolygonsHeight);
if (extrudeMesh.Vertices.Count() > 5)
{
this.Mesh = extrudeMesh;
}
else
{
this.Mesh = null;
}
}
UiThread.RunOnIdle(() =>
{
rebuildLocks.Dispose();
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children));
});
return Task.CompletedTask;
});
}
public void Subtract()
{
Subtract(CancellationToken.None, null);
}
private void Subtract(CancellationToken cancellationToken, Action<double, string> reporter)
{
SourceContainer.Visible = true;
RemoveAllButSource();
var parentOfSubtractTargets = SourceContainer.FirstWithMultipleChildrenDescendantsAndSelf();
if (parentOfSubtractTargets.Children.Count() < 2)
{
if (parentOfSubtractTargets.Children.Count() == 1)
{
this.Children.Add(SourceContainer.Clone());
SourceContainer.Visible = false;
}
return;
}
CleanUpSelectedChildrenNames(this);
var removeVisibleItems = parentOfSubtractTargets.Children
.Where((i) => SelectedChildren
.Contains(i.ID))
.SelectMany(c => c.VisiblePaths())
.ToList();
var keepItems = parentOfSubtractTargets.Children
.Where((i) => !SelectedChildren
.Contains(i.ID));
var keepVisibleItems = keepItems.SelectMany(c => c.VisiblePaths()).ToList();
if (removeVisibleItems.Any()
&& keepVisibleItems.Any())
{
var totalOperations = removeVisibleItems.Count * keepVisibleItems.Count;
double amountPerOperation = 1.0 / totalOperations;
double ratioCompleted = 0;
bool first = true;
foreach (var keep in keepVisibleItems)
{
2023-12-01 09:43:23 -08:00
var keepObject3D = keep as Object3D;
var resultsVertexSource = keep.GetVertexSource().Transform(keepObject3D.Matrix);
2023-03-10 17:15:55 -08:00
foreach (var remove in removeVisibleItems)
{
2023-12-01 09:43:23 -08:00
var removeObject3D = remove as Object3D;
resultsVertexSource = resultsVertexSource.MergePaths(remove.GetVertexSource().Transform(removeObject3D.Matrix), ClipperLib.ClipType.ctDifference);
2023-03-10 17:15:55 -08:00
// report our progress
ratioCompleted += amountPerOperation;
reporter?.Invoke(ratioCompleted, "Do Subtract".Localize());
}
if (first)
{
this.VertexStorage = new VertexStorage(resultsVertexSource);
2023-12-01 09:43:23 -08:00
this.CopyProperties(keepObject3D, Object3DPropertyFlags.Color);
2023-03-10 17:15:55 -08:00
first = false;
}
else
{
this.GetVertexSource().MergePaths(resultsVertexSource, ClipperLib.ClipType.ctUnion);
}
}
// this.VertexSource = this.VertexSource.Transform(Matrix.Inverted);
first = true;
foreach (var child in Children)
{
if (first)
{
// hide the source item
child.Visible = false;
first = false;
}
else
{
child.Visible = true;
}
}
}
}
public static void CleanUpSelectedChildrenNames(OperationSourceContainerObject3D item)
{
if (item is ISelectableChildContainer selectableChildContainer)
{
var parentOfSubtractTargets = item.FirstWithMultipleChildrenDescendantsAndSelf();
var allVisibleNames = parentOfSubtractTargets.Children.Select(i => i.ID);
// remove any names from SelectedChildren that are not a child we can select
foreach (var name in selectableChildContainer.SelectedChildren.ToArray())
{
if (!allVisibleNames.Contains(name))
{
selectableChildContainer.SelectedChildren.Remove(name);
}
}
}
}
public IEnumerable<SceneOperation> GetOperations()
{
2023-12-01 11:36:08 -08:00
return PathObject3DAbstract.GetOperations(this.GetType());
2023-03-10 17:15:55 -08:00
}
2023-12-01 09:43:23 -08:00
public IVertexSource GetVertexSource()
{
return VertexStorage;
}
2023-03-10 17:15:55 -08:00
}
2020-10-16 16:25:11 -07:00
}