mattercontrol/MatterControlLib/DesignTools/Operations/Path/LinearExtrudeObject3D.cs

202 lines
6.3 KiB
C#
Raw Normal View History

/*
Copyright (c) 2017, Lars Brubaker, John Lewin
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.
*/
2021-03-14 14:58:00 -07:00
using System;
2020-10-13 22:24:30 -07:00
using System.Collections.Generic;
2021-03-13 16:27:06 -08:00
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters3D;
using MatterHackers.DataConverters3D.UndoCommands;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.PolygonMesh;
2021-11-26 12:49:42 -08:00
using MatterHackers.PolygonMesh.Processors;
2021-03-13 16:27:06 -08:00
using MatterHackers.VectorMath;
2020-10-13 22:24:30 -07:00
namespace MatterHackers.MatterControl.DesignTools.Operations
{
2021-09-02 17:12:32 -07:00
public class LinearExtrudeObject3D : Object3D
#if DEBUG
, IPropertyGridModifier
#endif
{
2021-10-26 14:14:46 -07:00
[Description("The height of the extrusion")]
2021-10-01 16:59:58 -07:00
[Slider(.1, 50, Easing.EaseType.Quadratic, useSnappingGrid: true)]
2021-10-26 14:14:46 -07:00
[MaxDecimalPlaces(2)]
2021-06-05 11:12:26 -07:00
public DoubleOrExpression Height { get; set; } = 5;
#if DEBUG
2021-03-13 16:27:06 -08:00
[Description("Bevel the top of the extrusion")]
2020-10-13 22:24:30 -07:00
public bool BevelTop { get; set; } = false;
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ExpandStyles Style { get; set; } = ExpandStyles.Sharp;
2022-11-10 12:43:06 -08:00
[Slider(0, 20, Easing.EaseType.Quadratic, snapDistance: .1)]
public DoubleOrExpression Radius { get; set; } = 3;
2020-10-13 22:24:30 -07:00
2022-11-10 12:43:06 -08:00
[Slider(1, 20, Easing.EaseType.Quadratic, snapDistance: 1)]
public IntOrExpression Segments { get; set; } = 9;
#endif
2020-10-13 22:24:30 -07:00
2022-11-10 12:43:06 -08:00
public override bool CanApply => true;
public override IVertexSource GetVertexSource()
{
return this.CombinedVisibleChildrenPaths();
}
2022-01-22 15:43:50 -08:00
public override void Apply(UndoBuffer undoBuffer)
{
if (Mesh == null)
{
2022-01-22 15:43:50 -08:00
Cancel(undoBuffer);
}
else
{
// only keep the mesh and get rid of everything else
using (RebuildLock())
2018-06-20 08:09:35 -07:00
{
var meshOnlyItem = new Object3D()
{
Mesh = this.Mesh.Copy(CancellationToken.None)
};
meshOnlyItem.CopyProperties(this, Object3DPropertyFlags.All);
2020-10-13 22:24:30 -07:00
// and replace us with the children
undoBuffer.AddAndDo(new ReplaceCommand(new[] { this }, new[] { meshOnlyItem }));
}
Invalidate(InvalidateType.Children);
}
}
public LinearExtrudeObject3D()
{
Name = "Linear Extrude".Localize();
}
public override async void OnInvalidate(InvalidateArgs eventArgs)
{
if ((eventArgs.InvalidateType.HasFlag(InvalidateType.Path)
|| eventArgs.InvalidateType.HasFlag(InvalidateType.Children))
&& eventArgs.Source != this
&& !RebuildLocked)
{
await Rebuild();
}
else if (eventArgs.InvalidateType.HasFlag(InvalidateType.Properties)
&& eventArgs.Source == this)
{
await Rebuild();
}
else
{
base.OnInvalidate(eventArgs);
}
}
2022-11-10 12:43:06 -08:00
private (double x, double y) GetOffset(double radius, double xRatio, double yRatio)
{
return (radius * Math.Cos(xRatio * MathHelper.Tau / 4), radius * Math.Sin(yRatio * MathHelper.Tau / 4));
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");
var rebuildLock = RebuildLock();
2021-03-14 14:58:00 -07:00
bool valuesChanged = false;
2021-06-05 11:12:26 -07:00
var height = Height.Value(this);
#if DEBUG
2022-11-10 12:43:06 -08:00
var segments = Segments.ClampIfNotCalculated(this, 1, 32, ref valuesChanged);
var aabb = this.GetAxisAlignedBoundingBox();
var radius = Radius.ClampIfNotCalculated(this, 0, Math.Min(aabb.XSize, Math.Min(aabb.YSize, aabb.ZSize)) / 2, ref valuesChanged);
var bevelStart = height - radius;
#endif
2021-03-14 14:58:00 -07:00
// now create a long running task to do the extrusion
return ApplicationController.Instance.Tasks.Execute(
"Linear Extrude".Localize(),
null,
(reporter, cancellationToken) =>
2018-06-20 08:09:35 -07:00
{
var vertexSource = this.GetVertexSource();
2020-10-13 22:24:30 -07:00
List<(double height, double inset)> bevel = null;
#if DEBUG
2020-10-13 22:24:30 -07:00
if (BevelTop)
{
2022-11-10 12:43:06 -08:00
bevel = new List<(double, double)>();
for (int i = 0; i < segments; i++)
2020-10-13 22:24:30 -07:00
{
2022-11-10 12:43:06 -08:00
(double x, double y) = GetOffset(radius, (i + 1) / (double)segments, i / (double)segments);
bevel.Add((bevelStart + y, -radius+x));
2020-10-13 22:24:30 -07:00
}
2022-11-10 12:43:06 -08:00
}
#endif
2022-11-10 12:43:06 -08:00
if (this.GetVertexSource() != null)
{
Mesh = VertexSourceToMesh.Extrude(this.GetVertexSource(), height, bevel, InflatePathObject3D.GetJoinType(Style));
if (Mesh.Vertices.Count == 0)
{
Mesh = null;
}
}
else
{
Mesh = null;
}
UiThread.RunOnIdle(() =>
2021-03-14 14:58:00 -07:00
{
rebuildLock.Dispose();
2021-10-01 12:28:06 -07:00
Invalidate(InvalidateType.DisplayValues);
2021-12-05 22:01:50 -08:00
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh));
});
return Task.CompletedTask;
});
}
2020-10-13 22:24:30 -07:00
#if DEBUG
2020-10-13 22:24:30 -07:00
public void UpdateControls(PublicPropertyChange change)
{
2022-11-10 12:43:06 -08:00
change.SetRowVisible(nameof(Radius), () => BevelTop);
change.SetRowVisible(nameof(Segments), () => BevelTop);
change.SetRowVisible(nameof(Style), () => BevelTop);
2020-10-13 22:24:30 -07:00
}
#endif
}
}