Improving base object

Took out object list attribute
Refactoring
This commit is contained in:
Lars Brubaker 2018-06-07 14:20:39 -07:00
parent ecd8c8edba
commit 3150dc2c27
19 changed files with 166 additions and 112 deletions

View file

@ -108,41 +108,54 @@ namespace MatterHackers.MatterControl.DesignTools
public void DrawEditor(object sender, DrawEventArgs e)
{
bool first = true;
Vector2 lastPosition = Vector2.Zero;
var aabb = this.VisibleMeshes().FirstOrDefault().GetAxisAlignedBoundingBox();
foreach (var vertex in VertexSource.Vertices())
ImageToPath.DrawPath(this);
}
public static void DrawPath(IObject3D item)
{
if (item is IPathObject pathObject)
{
var position = vertex.position;
if (first)
if (pathObject.VertexSource == null)
{
first = false;
GL.PushMatrix();
GL.PushAttrib(AttribMask.EnableBit);
GL.MultMatrix(this.WorldMatrix().GetAsFloatArray());
GL.Disable(EnableCap.Texture2D);
GL.Disable(EnableCap.Blend);
GL.Begin(BeginMode.Lines);
GL.Color4(255, 0, 0, 255);
return;
}
if (vertex.IsLineTo)
bool first = true;
Vector2 lastPosition = Vector2.Zero;
var aabb = item.VisibleMeshes().FirstOrDefault().GetAxisAlignedBoundingBox();
foreach (var vertex in pathObject.VertexSource.Vertices())
{
GL.Vertex3(lastPosition.X, lastPosition.Y, aabb.maxXYZ.Z + 0.002);
GL.Vertex3(position.X, position.Y, aabb.maxXYZ.Z + 0.002);
var position = vertex.position;
if (first)
{
first = false;
GL.PushMatrix();
GL.PushAttrib(AttribMask.EnableBit);
GL.MultMatrix(item.WorldMatrix().GetAsFloatArray());
GL.Disable(EnableCap.Texture2D);
GL.Disable(EnableCap.Blend);
GL.Begin(BeginMode.Lines);
GL.Color4(255, 0, 0, 255);
}
if (vertex.IsLineTo)
{
GL.Vertex3(lastPosition.X, lastPosition.Y, aabb.maxXYZ.Z + 0.002);
GL.Vertex3(position.X, position.Y, aabb.maxXYZ.Z + 0.002);
}
lastPosition = position;
}
lastPosition = position;
}
// if we drew anything
if (!first)
{
GL.End();
GL.PopAttrib();
GL.PopMatrix();
// if we drew anything
if (!first)
{
GL.End();
GL.PopAttrib();
GL.PopMatrix();
}
}
}

View file

@ -44,6 +44,43 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
{
public static class Object3DExtensions
{
public static Color BlendHsl(string a, string b, int index, int count)
{
return PrimitiveColors[a].BlendHsl(PrimitiveColors[b], 1.0 / (count + 1.0) * index);
}
static Dictionary<string, Color> _primitiveColors;
public static Dictionary<string, Color> PrimitiveColors
{
get
{
if (_primitiveColors == null)
{
_primitiveColors = new Dictionary<string, Color>();
// put in all the constant things before blening them
_primitiveColors.Add("Cube", Color.FromHSL(.01, .98, .76)); // red
_primitiveColors.Add("Text", Color.FromHSL(.175, .98, .76)); // yellow
_primitiveColors.Add("HalfSphere", Color.FromHSL(.87, .98, .76)); // violet
// first color
_primitiveColors.Add("Pyramid", BlendHsl("Cube", "Text", 1, 3));
_primitiveColors.Add("Wedge", BlendHsl("Cube", "Text", 2, 3));
_primitiveColors.Add("HalfWedge", BlendHsl("Cube", "Text", 3, 3));
// mid color
_primitiveColors.Add("Cylinder", BlendHsl("Text", "HalfSphere", 1, 6));
_primitiveColors.Add("Cone", BlendHsl("Text", "HalfSphere", 2, 6));
_primitiveColors.Add("HalfCylinder", BlendHsl("Text", "HalfSphere", 3, 6));
_primitiveColors.Add("Torus", BlendHsl("Text", "HalfSphere", 4, 6));
_primitiveColors.Add("Ring", BlendHsl("Text", "HalfSphere", 5, 6));
_primitiveColors.Add("Sphere", BlendHsl("Text", "HalfSphere", 6, 6));
// end color
}
return _primitiveColors;
}
}
public static bool IsRoot(this IObject3D object3D)
{
return object3D.Parent == null;
@ -230,7 +267,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
list.Add(wrapper);
});
scene.SelectedItem = wrapper;
if (scene != null)
{
scene.SelectedItem = wrapper;
}
originalItem.ResumeRebuild();
}

View file

@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project.
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MatterHackers.MatterControl.DesignTools.Operations
@ -37,6 +38,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public class OperationSource : Object3D
{
public OperationSource()
: this(null)
{
}
public OperationSource(IEnumerable<IObject3D> children)
: base(children)
{
Name = "Source".Localize();
Visible = false;

View file

@ -43,33 +43,18 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
public class SmoothPath : Object3D, IPublicPropertyObject, IPathObject
public class SmoothPath : Object3D, IPublicPropertyObject, IPathObject, IEditorDraw
{
public Polygons PathData;
[JsonIgnore]
public IVertexSource VertexSource
{
get
{
var vertexSourc = this.Children.OfType<IPathObject>().FirstOrDefault();
return vertexSourc?.VertexSource;
}
set
{
var vertexSourc = this.Children.OfType<IPathObject>().FirstOrDefault();
if(vertexSourc != null)
{
vertexSourc.VertexSource = value;
}
}
}
public IVertexSource VertexSource { get; set; } = new VertexStorage();
public SmoothPath()
{
Name = "Smooth Path".Localize();
}
public override bool CanRemove => true;
public override void OnInvalidate(InvalidateArgs invalidateType)
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Content)
@ -98,7 +83,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public void DoSmoothing(long maxDist = 300, int interations = 3, bool closedPath = true)
{
var inputPolygons = VertexSource.CreatePolygons();
var sourceVertices = this.Children.OfType<IPathObject>().FirstOrDefault().VertexSource;
var inputPolygons = sourceVertices.CreatePolygons();
Polygons outputPolygons = new Polygons();
foreach (Polygon inputPolygon in inputPolygons)
@ -147,5 +134,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
VertexSource = outputPolygons.CreateVertexStorage();
}
public void DrawEditor(object sender, DrawEventArgs e)
{
ImageToPath.DrawPath(this);
}
}
}

View file

@ -37,6 +37,8 @@ using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters2D;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DesignTools.Operations;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
namespace MatterHackers.MatterControl.DesignTools
@ -54,25 +56,42 @@ namespace MatterHackers.MatterControl.DesignTools
Name = "Base".Localize();
}
public override bool CanRemove => true;
public override bool CanApply => true;
public BaseTypes CurrentBaseType { get; set; }
public double BaseSize { get; set; } = 3;
public double InfillAmount { get; set; } = 3;
public double ExtrusionHeight { get; set; } = 5;
public override void Apply(UndoBuffer undoBuffer)
{
OperationSource.Apply(this);
base.Apply(undoBuffer);
}
public override void Remove(UndoBuffer undoBuffer)
{
OperationSource.Remove(this);
base.Remove(undoBuffer);
}
[JsonIgnore]
public IVertexSource VertexSource
{
get
{
var vertexSourc = this.Children.OfType<IPathObject>().FirstOrDefault();
return vertexSourc?.VertexSource;
var vertexSource = (IPathObject)this.Descendants<IObject3D>().FirstOrDefault((i) => i is IPathObject);
return vertexSource?.VertexSource;
}
set
{
var vertexSourc = this.Children.OfType<IPathObject>().FirstOrDefault();
if (vertexSourc != null)
var vertexSource = this.Children.OfType<IPathObject>().FirstOrDefault();
if (vertexSource != null)
{
vertexSourc.VertexSource = value;
vertexSource.VertexSource = value;
}
}
}
@ -88,7 +107,8 @@ namespace MatterHackers.MatterControl.DesignTools
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Content)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Matrix)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Path))
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Path)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Mesh))
&& invalidateType.Source != this
&& !RebuildSuspended)
{
@ -105,8 +125,14 @@ namespace MatterHackers.MatterControl.DesignTools
this.DebugDepth("Rebuild");
SuspendRebuild();
var aabb = this.GetAxisAlignedBoundingBox();
Children.Modify((list) =>
{
list.RemoveAll((i) => !(i is OperationSource));
});
// Fall back to sibling content if VertexSource is unset
var vertexSource = this.VertexSource;
@ -211,8 +237,7 @@ namespace MatterHackers.MatterControl.DesignTools
public void GenerateBase(Polygons polygonShape)
{
// Add/remove base mesh
if (polygonShape?.Count > 0)
if (polygonShape.Select(p => p.Count).Sum() > 3)
{
Polygons polysToOffset = new Polygons();
@ -235,7 +260,7 @@ namespace MatterHackers.MatterControl.DesignTools
break;
}
if (polysToOffset.Count > 3)
if (polysToOffset.Count > 0)
{
Polygons basePolygons;
@ -253,7 +278,12 @@ namespace MatterHackers.MatterControl.DesignTools
VertexStorage rawVectorShape = basePolygons.PolygonToPathStorage();
var vectorShape = new VertexSourceApplyTransform(rawVectorShape, Affine.NewScaling(1.0 / scalingForClipper));
Mesh = VertexSourceToMesh.Extrude(vectorShape, zHeight: ExtrusionHeight);
var baseObject = new Object3D()
{
Mesh = VertexSourceToMesh.Extrude(vectorShape, zHeight: ExtrusionHeight)
};
Children.Add(baseObject);
baseObject.Mesh.Translate(new Vector3(0, 0, -ExtrusionHeight));
}
else
{

View file

@ -45,7 +45,7 @@ namespace MatterHackers.MatterControl.DesignTools
public ConeObject3D()
{
Name = "Cone".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Cone"];
Color = Operations.Object3DExtensions.PrimitiveColors["Cone"];
}
public static ConeObject3D Create()

View file

@ -41,7 +41,7 @@ namespace MatterHackers.MatterControl.DesignTools
public CubeObject3D()
{
Name = "Cube".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Cube"];
Color = Operations.Object3DExtensions.PrimitiveColors["Cube"];
}
public CubeObject3D(double width, double depth, double height)

View file

@ -45,7 +45,7 @@ namespace MatterHackers.MatterControl.DesignTools
public CylinderObject3D()
{
Name = "Cylinder".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Cylinder"];
Color = Operations.Object3DExtensions.PrimitiveColors["Cylinder"];
}
public CylinderObject3D(double diameter, double height, int sides)

View file

@ -44,7 +44,7 @@ namespace MatterHackers.MatterControl.DesignTools
public HalfCylinderObject3D()
{
Name = "Half Cylinder".Localize();
Color = ApplicationController.Instance.PrimitiveColors["HalfCylinder"];
Color = Operations.Object3DExtensions.PrimitiveColors["HalfCylinder"];
}
public static HalfCylinderObject3D Create()

View file

@ -44,7 +44,7 @@ namespace MatterHackers.MatterControl.DesignTools
public HalfSphereObject3D()
{
Name = "Half Sphere".Localize();
Color = ApplicationController.Instance.PrimitiveColors["HalfSphere"];
Color = Operations.Object3DExtensions.PrimitiveColors["HalfSphere"];
}
public HalfSphereObject3D(double diametar, int sides)

View file

@ -44,7 +44,7 @@ namespace MatterHackers.MatterControl.DesignTools
public HalfWedgeObject3D()
{
Name = "Half Wedge".Localize();
Color = ApplicationController.Instance.PrimitiveColors["HalfWedge"];
Color = Operations.Object3DExtensions.PrimitiveColors["HalfWedge"];
}
public static HalfWedgeObject3D Create()

View file

@ -43,7 +43,7 @@ namespace MatterHackers.MatterControl.DesignTools
public PyramidObject3D()
{
Name = "Pyriamid".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Pyramid"];
Color = Operations.Object3DExtensions.PrimitiveColors["Pyramid"];
}
public static PyramidObject3D Create()

View file

@ -46,7 +46,7 @@ namespace MatterHackers.MatterControl.DesignTools
public RingObject3D()
{
Name = "Ring".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Ring"];
Color = Operations.Object3DExtensions.PrimitiveColors["Ring"];
}
public RingObject3D(double outerDiameter, double innerDiameter, double height, int sides)

View file

@ -44,7 +44,7 @@ namespace MatterHackers.MatterControl.DesignTools
public SphereObject3D()
{
Name = "Sphere".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Sphere"];
Color = Operations.Object3DExtensions.PrimitiveColors["Sphere"];
}
public SphereObject3D(double diameter, int sides)

View file

@ -50,7 +50,7 @@ namespace MatterHackers.MatterControl.DesignTools
public TextObject3D()
{
Name = "Text".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Text"];
Color = Operations.Object3DExtensions.PrimitiveColors["Text"];
}
public static TextObject3D Create()

View file

@ -47,7 +47,7 @@ namespace MatterHackers.MatterControl.DesignTools
public TorusObject3D()
{
Name = "Torus".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Torus"];
Color = Operations.Object3DExtensions.PrimitiveColors["Torus"];
}
public static TorusObject3D Create()

View file

@ -45,7 +45,7 @@ namespace MatterHackers.MatterControl.DesignTools
public WedgeObject3D()
{
Name = "Wedge".Localize();
Color = ApplicationController.Instance.PrimitiveColors["Wedge"];
Color = Operations.Object3DExtensions.PrimitiveColors["Wedge"];
}
public static WedgeObject3D Create()