2018-06-06 15:46:30 -07:00
|
|
|
|
/*
|
|
|
|
|
|
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;
|
2020-10-11 14:53:46 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
2018-06-06 15:46:30 -07:00
|
|
|
|
using MatterHackers.Agg.VertexSource;
|
|
|
|
|
|
using MatterHackers.DataConverters3D;
|
2020-10-11 14:53:46 -07:00
|
|
|
|
using MatterHackers.DataConverters3D.UndoCommands;
|
|
|
|
|
|
using MatterHackers.Localizations;
|
2021-07-07 21:07:30 -07:00
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
2020-10-11 14:53:46 -07:00
|
|
|
|
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;
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2020-10-13 22:24:30 -07:00
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools.Operations
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2021-09-02 17:12:32 -07:00
|
|
|
|
public class LinearExtrudeObject3D : Object3D
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#if DEBUG
|
|
|
|
|
|
, IPropertyGridModifier
|
|
|
|
|
|
#endif
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
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;
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#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;
|
2021-05-01 22:06:43 -07:00
|
|
|
|
|
2022-11-07 08:34:52 -08:00
|
|
|
|
[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;
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#endif
|
2020-10-13 22:24:30 -07:00
|
|
|
|
|
2022-11-10 12:43:06 -08:00
|
|
|
|
public override bool CanApply => true;
|
2018-06-08 12:08:48 -07:00
|
|
|
|
|
2022-08-09 08:48:05 -07:00
|
|
|
|
public override IVertexSource GetVertexSource()
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2022-08-09 08:48:05 -07:00
|
|
|
|
return this.CombinedVisibleChildrenPaths();
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 15:43:50 -08:00
|
|
|
|
public override void Apply(UndoBuffer undoBuffer)
|
2018-06-08 15:09:10 -07:00
|
|
|
|
{
|
2020-07-19 10:07:35 -07:00
|
|
|
|
if (Mesh == null)
|
2018-06-08 15:09:10 -07:00
|
|
|
|
{
|
2022-01-22 15:43:50 -08:00
|
|
|
|
Cancel(undoBuffer);
|
2020-07-19 10:07:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// only keep the mesh and get rid of everything else
|
|
|
|
|
|
using (RebuildLock())
|
2018-06-20 08:09:35 -07:00
|
|
|
|
{
|
2020-07-19 10:07:35 -07:00
|
|
|
|
var meshOnlyItem = new Object3D()
|
|
|
|
|
|
{
|
|
|
|
|
|
Mesh = this.Mesh.Copy(CancellationToken.None)
|
|
|
|
|
|
};
|
2018-06-08 15:09:10 -07:00
|
|
|
|
|
2020-07-19 10:07:35 -07:00
|
|
|
|
meshOnlyItem.CopyProperties(this, Object3DPropertyFlags.All);
|
2018-06-08 15:09:10 -07:00
|
|
|
|
|
2020-10-13 22:24:30 -07:00
|
|
|
|
// and replace us with the children
|
2020-07-19 10:07:35 -07:00
|
|
|
|
undoBuffer.AddAndDo(new ReplaceCommand(new[] { this }, new[] { meshOnlyItem }));
|
|
|
|
|
|
}
|
2018-06-08 15:09:10 -07:00
|
|
|
|
|
2020-07-19 10:07:35 -07:00
|
|
|
|
Invalidate(InvalidateType.Children);
|
|
|
|
|
|
}
|
2018-06-08 15:09:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-21 21:02:37 -07:00
|
|
|
|
public LinearExtrudeObject3D()
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Linear Extrude".Localize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-26 18:38:40 -08:00
|
|
|
|
public override async void OnInvalidate(InvalidateArgs eventArgs)
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2019-01-28 14:19:40 -08:00
|
|
|
|
if ((eventArgs.InvalidateType.HasFlag(InvalidateType.Path)
|
2020-10-11 14:53:46 -07:00
|
|
|
|
|| eventArgs.InvalidateType.HasFlag(InvalidateType.Children))
|
2019-01-26 18:38:40 -08:00
|
|
|
|
&& eventArgs.Source != this
|
2018-06-20 17:16:38 -07:00
|
|
|
|
&& !RebuildLocked)
|
|
|
|
|
|
{
|
2019-01-24 10:32:22 -08:00
|
|
|
|
await Rebuild();
|
2018-06-20 17:16:38 -07:00
|
|
|
|
}
|
2019-01-28 14:19:40 -08:00
|
|
|
|
else if (eventArgs.InvalidateType.HasFlag(InvalidateType.Properties)
|
2019-01-26 18:38:40 -08:00
|
|
|
|
&& eventArgs.Source == this)
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2019-01-24 10:32:22 -08:00
|
|
|
|
await Rebuild();
|
2019-01-28 14:19:40 -08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnInvalidate(eventArgs);
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-24 10:32:22 -08:00
|
|
|
|
public override Task Rebuild()
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2019-01-24 10:32:22 -08:00
|
|
|
|
this.DebugDepth("Rebuild");
|
2021-07-07 21:07:30 -07:00
|
|
|
|
var rebuildLock = RebuildLock();
|
2019-01-24 10:32:22 -08:00
|
|
|
|
|
2021-03-14 14:58:00 -07:00
|
|
|
|
bool valuesChanged = false;
|
|
|
|
|
|
|
2021-06-05 11:12:26 -07:00
|
|
|
|
var height = Height.Value(this);
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#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;
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#endif
|
2021-03-14 14:58:00 -07:00
|
|
|
|
|
2021-07-07 21:07:30 -07:00
|
|
|
|
// now create a long running task to do the extrusion
|
2019-01-24 10:32:22 -08:00
|
|
|
|
return ApplicationController.Instance.Tasks.Execute(
|
|
|
|
|
|
"Linear Extrude".Localize(),
|
|
|
|
|
|
null,
|
|
|
|
|
|
(reporter, cancellationToken) =>
|
2018-06-20 08:09:35 -07:00
|
|
|
|
{
|
2022-08-09 08:48:05 -07:00
|
|
|
|
var vertexSource = this.GetVertexSource();
|
2020-10-13 22:24:30 -07:00
|
|
|
|
List<(double height, double inset)> bevel = null;
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#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
|
|
|
|
}
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#endif
|
2022-11-10 12:43:06 -08:00
|
|
|
|
if (this.GetVertexSource() != null)
|
2019-01-24 10:32:22 -08:00
|
|
|
|
{
|
2022-11-07 08:34:52 -08:00
|
|
|
|
Mesh = VertexSourceToMesh.Extrude(this.GetVertexSource(), height, bevel, InflatePathObject3D.GetJoinType(Style));
|
2022-08-09 08:48:05 -07:00
|
|
|
|
if (Mesh.Vertices.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Mesh = null;
|
|
|
|
|
|
}
|
2019-01-24 10:32:22 -08:00
|
|
|
|
}
|
2022-08-09 08:48:05 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Mesh = null;
|
|
|
|
|
|
}
|
2019-01-24 10:32:22 -08:00
|
|
|
|
|
2021-05-03 17:58:03 -07:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
2021-03-14 14:58:00 -07:00
|
|
|
|
{
|
2021-05-03 17:58:03 -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();
|
2021-05-03 17:58:03 -07:00
|
|
|
|
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh));
|
|
|
|
|
|
});
|
2021-07-07 21:07:30 -07:00
|
|
|
|
|
2019-01-24 10:32:22 -08:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
2020-10-13 22:24:30 -07:00
|
|
|
|
|
2021-05-01 22:06:43 -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);
|
2022-11-07 08:34:52 -08:00
|
|
|
|
change.SetRowVisible(nameof(Style), () => BevelTop);
|
2020-10-13 22:24:30 -07:00
|
|
|
|
}
|
2021-05-01 22:06:43 -07:00
|
|
|
|
#endif
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|