fix(geometry): add Entity.Clone() and stop NormalizeEntities from mutating originals
ShapeProfile.NormalizeEntities called Shape.Reverse() which flipped arc directions on the original entity objects shared with the CAD view. Switching to the Program tab and back would leave arcs reversed. Clone entities before normalizing so the originals stay untouched. Adds abstract Entity.Clone() with implementations on Line, Arc, Circle, Polygon, and Shape (deep-clones children). Also adds CloneAll() extension and replaces manual duplication in PartGeometry.CopyEntitiesAtLocation and ProgramEditorControl.CloneEntity. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -267,6 +267,13 @@ namespace OpenNest.Geometry
|
||||
get { return Diameter * System.Math.PI * SweepAngle() / Angle.TwoPI; }
|
||||
}
|
||||
|
||||
public override Entity Clone()
|
||||
{
|
||||
var copy = new Arc(center, radius, startAngle, endAngle, reversed);
|
||||
CopyBaseTo(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses the rotation direction.
|
||||
/// </summary>
|
||||
|
||||
@@ -165,6 +165,13 @@ namespace OpenNest.Geometry
|
||||
get { return Circumference(); }
|
||||
}
|
||||
|
||||
public override Entity Clone()
|
||||
{
|
||||
var copy = new Circle(center, radius) { Rotation = Rotation };
|
||||
CopyBaseTo(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses the rotation direction.
|
||||
/// </summary>
|
||||
|
||||
@@ -251,6 +251,23 @@ namespace OpenNest.Geometry
|
||||
/// <returns></returns>
|
||||
public abstract bool Intersects(Shape shape, out List<Vector> pts);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep copy of the entity with a new Id.
|
||||
/// </summary>
|
||||
public abstract Entity Clone();
|
||||
|
||||
/// <summary>
|
||||
/// Copies common Entity properties from this instance to the target.
|
||||
/// </summary>
|
||||
protected void CopyBaseTo(Entity target)
|
||||
{
|
||||
target.Color = Color;
|
||||
target.Layer = Layer;
|
||||
target.LineTypeName = LineTypeName;
|
||||
target.IsVisible = IsVisible;
|
||||
target.Tag = Tag;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Type of entity.
|
||||
/// </summary>
|
||||
@@ -259,6 +276,14 @@ namespace OpenNest.Geometry
|
||||
|
||||
public static class EntityExtensions
|
||||
{
|
||||
public static List<Entity> CloneAll(this IEnumerable<Entity> entities)
|
||||
{
|
||||
var result = new List<Entity>();
|
||||
foreach (var e in entities)
|
||||
result.Add(e.Clone());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<Vector> CollectPoints(this IEnumerable<Entity> entities)
|
||||
{
|
||||
var points = new List<Vector>();
|
||||
|
||||
@@ -257,6 +257,13 @@ namespace OpenNest.Geometry
|
||||
}
|
||||
}
|
||||
|
||||
public override Entity Clone()
|
||||
{
|
||||
var copy = new Line(pt1, pt2);
|
||||
CopyBaseTo(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reversed the line.
|
||||
/// </summary>
|
||||
|
||||
@@ -168,6 +168,13 @@ namespace OpenNest.Geometry
|
||||
get { return Perimeter(); }
|
||||
}
|
||||
|
||||
public override Entity Clone()
|
||||
{
|
||||
var copy = new Polygon { Vertices = new List<Vector>(Vertices) };
|
||||
CopyBaseTo(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses the rotation direction of the polygon.
|
||||
/// </summary>
|
||||
|
||||
@@ -349,6 +349,15 @@ namespace OpenNest.Geometry
|
||||
return polygon;
|
||||
}
|
||||
|
||||
public override Entity Clone()
|
||||
{
|
||||
var copy = new Shape();
|
||||
foreach (var e in Entities)
|
||||
copy.Entities.Add(e.Clone());
|
||||
CopyBaseTo(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses the rotation direction of the shape.
|
||||
/// </summary>
|
||||
|
||||
@@ -75,7 +75,8 @@ namespace OpenNest.Geometry
|
||||
/// </summary>
|
||||
public static List<Entity> NormalizeEntities(IEnumerable<Entity> entities)
|
||||
{
|
||||
var profile = new ShapeProfile(entities.ToList());
|
||||
var cloned = entities.CloneAll();
|
||||
var profile = new ShapeProfile(cloned);
|
||||
return profile.ToNormalizedEntities();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user