Adding revolve

cleaning up 2D editor controls
This commit is contained in:
LarsBrubaker 2020-10-11 14:53:46 -07:00
parent a71b54a7eb
commit dcccf5fd9c
14 changed files with 320 additions and 34 deletions

View file

@ -409,7 +409,7 @@ namespace MatterHackers.MatterControl
{
var scene = sceneContext.Scene;
var sceneItem = scene.SelectedItem;
if (sceneItem is IPathObject imageObject)
if (sceneItem is IPathObject pathObject)
{
var extrude = new LinearExtrudeObject3D();
@ -432,6 +432,40 @@ namespace MatterHackers.MatterControl
};
}
public static SceneOperation RevolveOperation()
{
return new SceneOperation("Revolve")
{
OperationType = typeof(IPathObject),
TitleResolver = () => "Revolve".Localize(),
ResultType = typeof(RevolveObject3D),
Action = (sceneContext) =>
{
var scene = sceneContext.Scene;
var sceneItem = scene.SelectedItem;
if (sceneItem is IPathObject pathObject)
{
var revolve = new RevolveObject3D();
var itemClone = sceneItem.Clone();
revolve.Children.Add(itemClone);
revolve.Matrix = itemClone.Matrix;
itemClone.Matrix = Matrix4X4.Identity;
using (new SelectionMaintainer(scene))
{
scene.UndoBuffer.AddAndDo(new ReplaceCommand(new[] { sceneItem }, new[] { revolve }));
}
revolve.Invalidate(InvalidateType.Properties);
}
},
Icon = (invertIcon) => AggContext.StaticData.LoadIcon("revolve.png", 16, 16, invertIcon).SetPreMultiply(),
HelpTextResolver = () => "*A path must be selected*".Localize(),
IsEnabled = (sceneContext) => sceneContext.Scene.SelectedItem != null && sceneContext.Scene.SelectedItem is IPathObject,
};
}
public static SceneOperation MakeComponentOperation()
{
return new SceneOperation("Make Component")
@ -754,6 +788,7 @@ namespace MatterHackers.MatterControl
Operations = new List<SceneOperation>()
{
LinearExtrudeOperation(),
RevolveOperation(),
SmoothPathOperation(),
InflatePathOperation(),
OutlinePathOperation(),

View file

@ -51,7 +51,7 @@ using Polygons = System.Collections.Generic.List<System.Collections.Generic.List
namespace MatterHackers.MatterControl.DesignTools
{
public class ImageToPathObject3D : Object3D, IPathObject, IEditorDraw
public class ImageToPathObject3D : Object3D, IPathObject, IEditorDraw, IObject3DControlsProvider
{
private ThresholdFunctions _featureDetector = ThresholdFunctions.Silhouette;
@ -194,6 +194,12 @@ namespace MatterHackers.MatterControl.DesignTools
}
}
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public static void DrawPath(IObject3D item)
{
if (item is IPathObject pathObject)

View file

@ -27,22 +27,19 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System.Linq;
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.PolygonMesh;
using Newtonsoft.Json;
using System.Linq;
namespace MatterHackers.MatterControl.DesignTools
{
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D.UndoCommands;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DesignTools.Operations;
using MatterHackers.PolygonMesh;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public class LinearExtrudeObject3D : Object3D
{
public double Height { get; set; } = 5;
@ -98,7 +95,7 @@ namespace MatterHackers.MatterControl.DesignTools
public override async void OnInvalidate(InvalidateArgs eventArgs)
{
if ((eventArgs.InvalidateType.HasFlag(InvalidateType.Path)
|| eventArgs.InvalidateType.HasFlag(InvalidateType.Children))
|| eventArgs.InvalidateType.HasFlag(InvalidateType.Children))
&& eventArgs.Source != this
&& !RebuildLocked)
{

View file

@ -0,0 +1,187 @@
/*
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.
*/
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
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;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
namespace MatterHackers.MatterControl.DesignTools
{
public class RevolveObject3D : Object3D, IObject3DControlsProvider
{
public double AxisPosition { get; set; } = 0;
public int Sides { get; set; } = 30;
public bool Advanced { get; set; } = false;
public double StartingAngle { get; set; } = 0;
public double EndingAngle { get; set; } = 360;
public override bool CanFlatten => true;
[JsonIgnore]
private IVertexSource VertexSource
{
get
{
var item = this.Descendants().Where((d) => d is IPathObject).FirstOrDefault();
if (item is IPathObject pathItem)
{
return pathItem.VertexSource;
}
return null;
}
}
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public override void Flatten(UndoBuffer undoBuffer)
{
if (Mesh == null)
{
Remove(undoBuffer);
}
else
{
// only keep the mesh and get rid of everything else
using (RebuildLock())
{
var meshOnlyItem = new Object3D()
{
Mesh = this.Mesh.Copy(CancellationToken.None)
};
meshOnlyItem.CopyProperties(this, Object3DPropertyFlags.All);
// and replace us with the children
undoBuffer.AddAndDo(new ReplaceCommand(new[] { this }, new[] { meshOnlyItem }));
}
Invalidate(InvalidateType.Children);
}
}
public RevolveObject3D()
{
Name = "Revolve".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);
}
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");
bool valuesChanged = false;
if (Advanced && (StartingAngle > 0 || EndingAngle < 360))
{
Sides = agg_basics.Clamp(Sides, 1, 360, ref valuesChanged);
}
else
{
Sides = agg_basics.Clamp(Sides, 3, 360, ref valuesChanged);
}
StartingAngle = agg_basics.Clamp(StartingAngle, 0, 360 - .01, ref valuesChanged);
EndingAngle = agg_basics.Clamp(EndingAngle, StartingAngle + .01, 360, ref valuesChanged);
if (valuesChanged)
{
Invalidate(InvalidateType.DisplayValues);
}
var rebuildLock = RebuildLock();
// now create a long running task to process the image
return ApplicationController.Instance.Tasks.Execute(
"Revolve".Localize(),
null,
(reporter, cancellationToken) =>
{
var vertexSource = this.VertexSource;
var bounds = vertexSource.GetBounds();
vertexSource = vertexSource.Translate(-bounds.Left - AxisPosition, 0);
if (!Advanced)
{
Mesh = VertexSourceToMesh.Revolve(vertexSource, Sides);
}
else
{
Mesh = VertexSourceToMesh.Revolve(vertexSource,
Sides,
MathHelper.DegreesToRadians(StartingAngle),
MathHelper.DegreesToRadians(EndingAngle));
}
if (Mesh.Vertices.Count == 0)
{
Mesh = null;
}
rebuildLock.Dispose();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh));
return Task.CompletedTask;
});
}
}
}

View file

@ -48,7 +48,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
Sharp,
}
public class InflatePathObject3D : Object3D, IPathObject, IEditorDraw
public class InflatePathObject3D : Object3D, IPathObject, IEditorDraw, IObject3DControlsProvider
{
public IVertexSource VertexSource { get; set; } = new VertexStorage();
@ -63,6 +63,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ExpandStyles Style { get; set; } = ExpandStyles.Sharp;
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public override async void OnInvalidate(InvalidateArgs invalidateType)
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Children)

View file

@ -44,7 +44,7 @@ using MatterHackers.MatterControl.PartPreviewWindow;
namespace MatterHackers.MatterControl.DesignTools.Operations
{
public class OutlinePathObject3D : Object3D, IPathObject, IEditorDraw
public class OutlinePathObject3D : Object3D, IPathObject, IEditorDraw, IObject3DControlsProvider
{
public IVertexSource VertexSource { get; set; } = new VertexStorage();
@ -85,6 +85,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
}
}
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");

View file

@ -43,7 +43,7 @@ using Polygons = System.Collections.Generic.List<System.Collections.Generic.List
namespace MatterHackers.MatterControl.DesignTools.Operations
{
public class SmoothPathObject3D : Object3D, IPathObject, IEditorDraw
public class SmoothPathObject3D : Object3D, IPathObject, IEditorDraw, IObject3DControlsProvider
{
public IVertexSource VertexSource { get; set; } = new VertexStorage();
@ -56,6 +56,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public int Iterations { get; set; } = 3;
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
public override async void OnInvalidate(InvalidateArgs invalidateType)
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Children)

View file

@ -151,6 +151,8 @@ namespace MatterHackers.MatterControl.DesignTools
Sides = agg_basics.Clamp(Sides, 3, 360, ref valuesChanged);
Height = agg_basics.Clamp(Height, .01, 1000000, ref valuesChanged);
Diameter = agg_basics.Clamp(Diameter, .01, 1000000, ref valuesChanged);
StartingAngle = agg_basics.Clamp(StartingAngle, 0, 360 - .01, ref valuesChanged);
EndingAngle = agg_basics.Clamp(EndingAngle, StartingAngle + .01, 360, ref valuesChanged);
if (valuesChanged)
{

View file

@ -131,7 +131,7 @@ namespace MatterHackers.MatterControl.DesignTools
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
{
object3DControlsLayer.AddDefaultControls();
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
}
private ImageBuffer LoadImage()

View file

@ -96,6 +96,8 @@ namespace MatterHackers.MatterControl.DesignTools
{
InnerDiameter = agg_basics.Clamp(InnerDiameter, 0, OuterDiameter - .1, ref valuesChanged);
Sides = agg_basics.Clamp(Sides, 3, 360, ref valuesChanged);
StartingAngle = agg_basics.Clamp(StartingAngle, 0, 360 - .01, ref valuesChanged);
EndingAngle = agg_basics.Clamp(EndingAngle, StartingAngle + .01, 360, ref valuesChanged);
using (new CenterAndHeightMaintainer(this))
{

View file

@ -98,6 +98,8 @@ namespace MatterHackers.MatterControl.DesignTools
{
Sides = agg_basics.Clamp(Sides, 3, 360, ref valuesChanged);
LatitudeSides = agg_basics.Clamp(LatitudeSides, 3, 360, ref valuesChanged);
StartingAngle = agg_basics.Clamp(StartingAngle, 0, 360 - .01, ref valuesChanged);
EndingAngle = agg_basics.Clamp(EndingAngle, StartingAngle + .01, 360, ref valuesChanged);
using (new CenterAndHeightMaintainer(this))
{

View file

@ -93,6 +93,9 @@ namespace MatterHackers.MatterControl.DesignTools
Sides = agg_basics.Clamp(Sides, 3, 360, ref valuesChanged);
RingSides = agg_basics.Clamp(RingSides, 3, 360, ref valuesChanged);
StartingAngle = agg_basics.Clamp(StartingAngle, 0, 360 - .01, ref valuesChanged);
EndingAngle = agg_basics.Clamp(EndingAngle, StartingAngle + .01, 360, ref valuesChanged);
var ringSides = RingSides;
var startingAngle = StartingAngle;
var endingAngle = EndingAngle;

View file

@ -49,6 +49,20 @@ using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
[Flags]
public enum ControlTypes
{
MoveInZ = 1,
RotateXYZ = 2,
RotateZ = 4,
ScaleCornersXY = 8,
ScaleMatrixXY = 16,
Shadow = 32,
SnappingIndicators = 64,
Standard2D = MoveInZ | Shadow | SnappingIndicators | RotateZ | ScaleMatrixXY
}
public class Object3DControlsLayer : GuiWidget, IObject3DControlContext
{
private IObject3DControl mouseDownObject3DControl = null;
@ -172,32 +186,55 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
Object3DControls.Add(new ScaleMatrixTopControl(this));
}
Object3DControls.Add(new ScaleCornerControl(this, 0));
Object3DControls.Add(new ScaleCornerControl(this, 1));
Object3DControls.Add(new ScaleCornerControl(this, 2));
Object3DControls.Add(new ScaleCornerControl(this, 3));
AddWorldRotateControls();
AddDefaultControls();
AddControls(ControlTypes.RotateXYZ
| ControlTypes.MoveInZ
| ControlTypes.ScaleMatrixXY
| ControlTypes.Shadow
| ControlTypes.SnappingIndicators);
}
});
}
/// <summary>
/// Add in MoveInZ, SelectionShadow & SnappingIndicators
/// </summary>
public void AddDefaultControls()
public void AddControls(ControlTypes controls)
{
Object3DControls.Add(new MoveInZControl(this));
Object3DControls.Add(new SelectionShadow(this));
Object3DControls.Add(new SnappingIndicators(this));
if (controls.HasFlag(ControlTypes.RotateXYZ))
{
Object3DControls.Add(new RotateCornerControl(this, 0));
Object3DControls.Add(new RotateCornerControl(this, 1));
Object3DControls.Add(new RotateCornerControl(this, 2));
}
if (controls.HasFlag(ControlTypes.RotateZ))
{
Object3DControls.Add(new RotateCornerControl(this, 2));
}
if (controls.HasFlag(ControlTypes.MoveInZ))
{
Object3DControls.Add(new MoveInZControl(this));
}
if (controls.HasFlag(ControlTypes.ScaleMatrixXY))
{
Object3DControls.Add(new ScaleCornerControl(this, 0));
Object3DControls.Add(new ScaleCornerControl(this, 1));
Object3DControls.Add(new ScaleCornerControl(this, 2));
Object3DControls.Add(new ScaleCornerControl(this, 3));
}
if (controls.HasFlag(ControlTypes.Shadow))
{
Object3DControls.Add(new SelectionShadow(this));
}
if (controls.HasFlag(ControlTypes.SnappingIndicators))
{
Object3DControls.Add(new SnappingIndicators(this));
}
}
public void AddWorldRotateControls()
{
Object3DControls.Add(new RotateCornerControl(this, 0));
Object3DControls.Add(new RotateCornerControl(this, 1));
Object3DControls.Add(new RotateCornerControl(this, 2));
}
private void DisposeCurrentSelectionObject3DControls()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB