Working on align tool

This commit is contained in:
LarsBrubaker 2018-02-11 13:52:19 -08:00
parent a3eca8e420
commit 9ce555a740
6 changed files with 125 additions and 138 deletions

View file

@ -429,6 +429,21 @@ namespace MatterHackers.MatterControl
IsEnabled = (scene) => scene.HasSelection && !(scene.SelectedItem is SelectionGroup),
},
#if DEBUG // keep this work in progress to the editor for now
new SceneSelectionSeparator(),
new SceneSelectionOperation()
{
TitleResolver = () => "Align".Localize(),
Action = (scene) =>
{
scene.AddSelectionAsChildren(new ArangeObject3D());
if(scene.SelectedItem is ArangeObject3D arange)
{
arange.Rebuild();
}
},
//Icon = AggContext.StaticData.LoadIcon("array_linear.png").SetPreMultiply(),
IsEnabled = (scene) => scene.SelectedItem is SelectionGroup,
},
new SceneSelectionSeparator(),
new SceneSelectionOperation()
{
@ -440,16 +455,6 @@ namespace MatterHackers.MatterControl
IsEnabled = (scene) => scene.HasSelection,
},
new SceneSelectionOperation()
{
TitleResolver = () => "Proportional Scale".Localize(),
Action = (scene) =>
{
scene.AddSelectionAsChildren(new HoldChildProportional());
},
//Icon = AggContext.StaticData.LoadIcon("subtract.png").SetPreMultiply(),
IsEnabled = (scene) => scene.HasSelection,
},
new SceneSelectionOperation()
{
TitleResolver = () => "Bend".Localize(),
Action = (scene) => new BendOperation(scene.SelectedItem),

View file

@ -28,6 +28,7 @@ either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using MatterHackers.DataConverters3D;
@ -37,6 +38,8 @@ using Newtonsoft.Json.Converters;
namespace MatterHackers.MatterControl.DesignTools.Operations
{
using Aabb = AxisAlignedBoundingBox;
public class DirectionAxis
{
public Vector3 Origin { get; set; }
@ -48,6 +51,108 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public Vector3 Normal { get; set; }
}
public class ArangeObject3D : Object3D, IRebuildable
{
[JsonIgnoreAttribute]
Aabb startingBounds = Aabb.Empty;
[JsonIgnoreAttribute]
List<Aabb> childrenBounds = new List<Aabb>();
public enum AlignTo { First, Last, All_Bounds }
public enum Align { None, Min, Center, Max, Flow }
public Align AlignmentX { get; set; } = Align.None;
public AlignTo AlignToX { get; set; } = AlignTo.First;
public double OffsetX { get; set; } = 0;
public Align AlignmentY { get; set; } = Align.None;
public AlignTo AlignToY { get; set; } = AlignTo.First;
public double OffsetY { get; set; } = 0;
public Align AlignmentZ { get; set; } = Align.None;
public AlignTo AlignToZ { get; set; } = AlignTo.First;
public double OffsetZ { get; set; } = 0;
public override string ActiveEditor => "PublicPropertyEditor";
public ArangeObject3D()
{
}
public void Rebuild()
{
var aabb = this.GetAxisAlignedBoundingBox();
if (startingBounds == Aabb.Empty)
{
startingBounds = aabb;
this.Children.Modify(list =>
{
foreach (var child in list)
{
childrenBounds.Add(child.GetAxisAlignedBoundingBox());
}
});
}
this.Children.Modify(list =>
{
int i = 0;
foreach (var child in list)
{
var originalBounds = childrenBounds[i++];
AlignAxis(0, AlignmentX, GetCorrectAabb(AlignToX), OffsetX, child, originalBounds);
AlignAxis(1, AlignmentY, GetCorrectAabb(AlignToY), OffsetY, child, originalBounds);
AlignAxis(2, AlignmentZ, GetCorrectAabb(AlignToZ), OffsetZ, child, originalBounds);
}
});
}
private Aabb GetCorrectAabb(AlignTo alignTo)
{
switch (alignTo)
{
case AlignTo.First:
return childrenBounds.First();
case AlignTo.Last:
return childrenBounds.Last();
default:
return startingBounds;
}
}
private void AlignAxis(int axis, Align align, Aabb bounds, double offset,
IObject3D item, Aabb originalBounds)
{
var aabb = item.GetAxisAlignedBoundingBox();
var translate = Vector3.Zero;
switch (align)
{
case Align.None:
translate[axis] = originalBounds.minXYZ[axis] - aabb.minXYZ[axis];
break;
case Align.Min:
translate[axis] = bounds.minXYZ[axis] - aabb.minXYZ[axis] + offset;
break;
case Align.Center:
translate[axis] = bounds.Center[axis] - aabb.Center[axis] + offset;
break;
case Align.Max:
translate[axis] = bounds.maxXYZ[axis] - aabb.maxXYZ[axis] + offset;
break;
case Align.Flow:
break;
}
item.Translate(translate);
}
}
public class ArrayLinearObject3D : Object3D, IRebuildable
{
public int Count { get; set; } = 3;
@ -68,7 +173,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
list.Clear();
list.Add(lastChild);
var offset = Vector3.Zero;
for (int i=1; i<Count; i++)
for (int i = 1; i < Count; i++)
{
var next = lastChild.Clone();
next.Matrix *= Matrix4X4.CreateTranslation(Direction.Normal.GetNormal() * Distance);

View file

@ -68,30 +68,24 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
{
if (scene.HasSelection)
{
IObject3D item;
List<IObject3D> itemsToReplace;
if (scene.SelectedItem is SelectionGroup)
{
Object3D container = new Object3D();
itemsToReplace = scene.SelectedItem.Children.ToList();
foreach (var child in itemsToReplace)
{
container.Children.Add(child.Clone());
newParent.Children.Add(child.Clone());
}
item = container;
}
else
{
itemsToReplace = new List<IObject3D> { scene.SelectedItem };
item = scene.SelectedItem.Clone();
newParent.Children.Add(scene.SelectedItem.Clone());
}
scene.SelectedItem = null;
newParent.Children.Add(item);
newParent.MakeNameNonColliding();
scene.UndoBuffer.AddAndDo(
@ -99,15 +93,6 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
itemsToReplace,
new List<IObject3D> { newParent }));
if (newParent is HoldChildProportional pe)
{
item.Matrix = Matrix4X4.Identity;
// Make the object have an identity matrix and keep its position in our new object
newParent.Matrix = item.Matrix;
pe.InitialChildBounds = item.GetAxisAlignedBoundingBox();
}
scene.SelectedItem = newParent;
}
}

View file

@ -125,7 +125,6 @@
<Compile Include="PartPreviewWindow\MaterialControls.cs" />
<Compile Include="PartPreviewWindow\MoveItemPage.cs" />
<Compile Include="PartPreviewWindow\View3D\Actions\CombineEditor.cs" />
<Compile Include="PartPreviewWindow\View3D\Actions\ProportionalEditor.cs" />
<Compile Include="PartPreviewWindow\View3D\Actions\SubtractAndReplace.cs" />
<Compile Include="PartPreviewWindow\View3D\PrinterBar\OverflowBar.cs" />
<Compile Include="PartPreviewWindow\View3D\PrinterBar\PrintPopupMenu.cs" />

View file

@ -116,8 +116,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
var transformDatas = GetTransforms(axisIndex, alignment);
scene.UndoBuffer.AddAndDo(new TransformCommand(transformDatas));
//scene.SelectedItem.MaterialIndex = extruderIndexCanPassToClick;
scene.Invalidate();
//scene.SelectedItem.MaterialIndex = extruderIndexCanPassToClick;
scene.Invalidate();
}
};

View file

@ -1,107 +0,0 @@
/*
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;
using System.Collections.Generic;
using System.Linq;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.DataConverters3D.UndoCommands;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow.View3D
{
public class HoldChildProportional : Object3D
{
public AxisAlignedBoundingBox InitialChildBounds = AxisAlignedBoundingBox.Zero;
public HoldChildProportional()
{
}
public override AxisAlignedBoundingBox GetAxisAlignedBoundingBox(Matrix4X4 matrix, bool requirePrecision = false)
{
// We return our calculated bounds not those of our children
return InitialChildBounds.NewTransformed(this.Matrix * matrix);
}
bool initiatingChange = false;
protected override void OnInvalidate()
{
if (!initiatingChange
&& InitialChildBounds.XSize != 0)
{
initiatingChange = true;
var currentBounds = this.GetAxisAlignedBoundingBox();
// keep the bounds proportional
double minScale = double.MaxValue;
for (int i = 0; i < 3; i++)
{
minScale = Math.Min(minScale, currentBounds.Size[i] / InitialChildBounds.Size[i]);
}
Vector3 innerScale = Vector3.One;
for (int i = 0; i < 3; i++)
{
innerScale[i] = InitialChildBounds.Size[i] / currentBounds.Size[i] * minScale;
}
Children.First().Matrix = Matrix4X4.CreateScale(innerScale);
initiatingChange = false;
}
base.OnInvalidate();
}
}
public class ProportionalEditor : IObject3DEditor
{
private MeshWrapperOperation group;
private View3DWidget view3DWidget;
public string Name => "Proportional Scale";
public bool Unlocked { get; } = true;
public GuiWidget Create(IObject3D group, View3DWidget view3DWidget, ThemeConfig theme)
{
this.view3DWidget = view3DWidget;
this.group = group as MeshWrapperOperation;
var mainContainer = new FlowLayoutWidget(FlowDirection.TopToBottom);
return mainContainer;
}
public IEnumerable<Type> SupportedTypes() => new Type[]
{
typeof(HoldChildProportional),
};
}
}