2020-05-23 22:54:41 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2018, 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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2020-05-24 12:06:05 -07:00
|
|
|
|
using System;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using ClipperLib;
|
2023-12-01 11:36:08 -08:00
|
|
|
|
using MatterControlLib.DesignTools.Operations.Path;
|
2020-05-24 12:06:05 -07:00
|
|
|
|
using MatterHackers.Agg;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
|
|
|
|
|
using MatterHackers.Agg.VertexSource;
|
2020-05-24 12:06:05 -07:00
|
|
|
|
using MatterHackers.DataConverters2D;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
using MatterHackers.DataConverters3D;
|
|
|
|
|
|
using MatterHackers.Localizations;
|
|
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
2021-11-26 12:49:42 -08:00
|
|
|
|
using MatterHackers.PolygonMesh.Processors;
|
2022-03-02 00:52:04 +00:00
|
|
|
|
using MatterHackers.VectorMath;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools.Operations
|
|
|
|
|
|
{
|
2023-12-01 11:36:08 -08:00
|
|
|
|
public class OutlinePathObject3D : PathObject3DAbstract, IEditorDraw, IObject3DControlsProvider
|
2023-03-11 10:56:32 -08:00
|
|
|
|
{
|
2020-05-23 22:54:41 -07:00
|
|
|
|
public OutlinePathObject3D()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "Outline Path".Localize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Description("The with of the outline.")]
|
2022-08-11 18:37:06 -07:00
|
|
|
|
public DoubleOrExpression OutlineWidth { get; set; } = .5;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
2022-08-09 08:48:05 -07:00
|
|
|
|
[Description("The offset of the outside of the outline as a ratio of the width.")]
|
|
|
|
|
|
public DoubleOrExpression Offset { get; set; } = .5;
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
|
|
|
|
|
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
|
|
|
|
|
|
public ExpandStyles InnerStyle { get; set; } = ExpandStyles.Sharp;
|
|
|
|
|
|
|
|
|
|
|
|
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
|
|
|
|
|
|
public ExpandStyles OuterStyle { get; set; } = ExpandStyles.Sharp;
|
|
|
|
|
|
|
2021-06-19 21:28:16 -07:00
|
|
|
|
public override async void OnInvalidate(InvalidateArgs invalidateArgs)
|
2020-05-23 22:54:41 -07:00
|
|
|
|
{
|
2021-06-19 21:28:16 -07:00
|
|
|
|
if ((invalidateArgs.InvalidateType.HasFlag(InvalidateType.Children)
|
|
|
|
|
|
|| invalidateArgs.InvalidateType.HasFlag(InvalidateType.Matrix)
|
|
|
|
|
|
|| invalidateArgs.InvalidateType.HasFlag(InvalidateType.Path))
|
|
|
|
|
|
&& invalidateArgs.Source != this
|
2020-05-23 22:54:41 -07:00
|
|
|
|
&& !RebuildLocked)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Rebuild();
|
|
|
|
|
|
}
|
2021-06-19 21:28:16 -07:00
|
|
|
|
else if ((invalidateArgs.InvalidateType.HasFlag(InvalidateType.Properties) && invalidateArgs.Source == this))
|
|
|
|
|
|
{
|
|
|
|
|
|
await Rebuild();
|
|
|
|
|
|
}
|
2023-03-10 17:15:55 -08:00
|
|
|
|
else if (Expressions.NeedRebuild(this, invalidateArgs))
|
2020-05-23 22:54:41 -07:00
|
|
|
|
{
|
|
|
|
|
|
await Rebuild();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-06-19 21:28:16 -07:00
|
|
|
|
base.OnInvalidate(invalidateArgs);
|
2020-05-23 22:54:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-28 18:16:20 -08:00
|
|
|
|
public override bool MeshIsSolidObject => false;
|
|
|
|
|
|
|
2020-10-11 14:53:46 -07:00
|
|
|
|
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
|
|
|
|
|
|
{
|
|
|
|
|
|
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-23 22:54:41 -07:00
|
|
|
|
public override Task Rebuild()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.DebugDepth("Rebuild");
|
2020-05-24 12:06:05 -07:00
|
|
|
|
|
2021-06-05 11:12:26 -07:00
|
|
|
|
var outlineWidth = OutlineWidth.Value(this);
|
|
|
|
|
|
if (outlineWidth < .01 || outlineWidth > 1000)
|
2020-05-24 12:06:05 -07:00
|
|
|
|
{
|
2021-06-05 11:12:26 -07:00
|
|
|
|
OutlineWidth = Math.Min(1000, Math.Max(.01, outlineWidth));
|
2020-05-24 12:06:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-23 22:54:41 -07:00
|
|
|
|
using (RebuildLock())
|
|
|
|
|
|
{
|
|
|
|
|
|
InsetPath();
|
2020-10-13 16:05:04 -07:00
|
|
|
|
// set the mesh to show the path
|
2022-08-09 08:48:05 -07:00
|
|
|
|
this.Mesh = VertexStorage.Extrude(Constants.PathPolygonsHeight);
|
2020-05-23 22:54:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-01 12:28:06 -07:00
|
|
|
|
Invalidate(InvalidateType.DisplayValues);
|
2020-05-24 12:06:05 -07:00
|
|
|
|
|
2021-12-05 22:01:50 -08:00
|
|
|
|
this.CancelAllParentBuilding();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path));
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InsetPath()
|
|
|
|
|
|
{
|
2022-08-09 08:48:05 -07:00
|
|
|
|
var path = this.CombinedVisibleChildrenPaths();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
if (path == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// clear our existing data
|
2022-08-09 08:48:05 -07:00
|
|
|
|
VertexStorage = new VertexStorage();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-09 08:48:05 -07:00
|
|
|
|
var aPolys = path.CreatePolygons();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
2023-04-11 17:36:14 -07:00
|
|
|
|
aPolys = aPolys.GetCorrectedWinding();
|
|
|
|
|
|
|
2020-10-10 16:18:14 -07:00
|
|
|
|
var offseter = new ClipperOffset();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
2021-06-05 11:12:26 -07:00
|
|
|
|
var outlineWidth = OutlineWidth.Value(this);
|
2022-08-09 08:48:05 -07:00
|
|
|
|
var ratio = Offset.Value(this);
|
2021-06-05 11:12:26 -07:00
|
|
|
|
|
2020-05-23 22:54:41 -07:00
|
|
|
|
offseter.AddPaths(aPolys, InflatePathObject3D.GetJoinType(OuterStyle), EndType.etClosedPolygon);
|
|
|
|
|
|
var outerLoops = new List<List<IntPoint>>();
|
2021-06-05 11:12:26 -07:00
|
|
|
|
offseter.Execute(ref outerLoops, outlineWidth * ratio * 1000);
|
2020-05-23 22:54:41 -07:00
|
|
|
|
Clipper.CleanPolygons(outerLoops);
|
|
|
|
|
|
|
|
|
|
|
|
offseter.AddPaths(aPolys, InflatePathObject3D.GetJoinType(InnerStyle), EndType.etClosedPolygon);
|
|
|
|
|
|
var innerLoops = new List<List<IntPoint>>();
|
2021-06-05 11:12:26 -07:00
|
|
|
|
offseter.Execute(ref innerLoops, -outlineWidth * (1 - ratio) * 1000);
|
2020-05-23 22:54:41 -07:00
|
|
|
|
Clipper.CleanPolygons(innerLoops);
|
|
|
|
|
|
|
|
|
|
|
|
var allLoops = outerLoops;
|
|
|
|
|
|
allLoops.AddRange(innerLoops);
|
|
|
|
|
|
|
2022-08-09 08:48:05 -07:00
|
|
|
|
VertexStorage = allLoops.CreateVertexStorage();
|
2020-05-23 22:54:41 -07:00
|
|
|
|
|
2023-10-31 15:13:04 -07:00
|
|
|
|
VertexStorage.Add(0, 0, FlagsAndCommand.Stop);
|
2020-05-23 22:54:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|