mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-16 11:25:36 -05:00
fix(shapes): cut shape library parts in the CAD Converter's directions
The CAD Converter winds every drawing's outside clockwise and its cutouts counter-clockwise (ShapeProfile.NormalizeEntities) before building the program, but the Shape Library built its programs straight from the generated entities, which run counter-clockwise. A shape part therefore cut its outside the opposite way from an imported part, and opening it in the converter showed (and on save, wrote) the reverse of what was nested. ShapeDefinition.CreateDrawing now normalizes the entities the same way, so every shape cuts outside clockwise and cutouts counter-clockwise. Tests cover each shape type, including the pipe flange's holes, and that rebuilding a shape drawing through the converter's import path keeps its directions. Drawings already made from shapes keep their stored direction until they are recreated or saved from the converter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
618996d2e0
commit
7894b74cb7
2 files changed
+108
-1
No files matched your search
@@ -51,9 +51,13 @@ namespace OpenNest.Shapes
|
||||
|
||||
protected static string Dim(double value) => value.ToString("0.###");
|
||||
|
||||
/// <summary>
|
||||
/// Builds the shape's drawing with the CAD converter's cut directions: the outside
|
||||
/// clockwise and every cutout counter-clockwise (see <see cref="ShapeProfile.NormalizeEntities"/>).
|
||||
/// </summary>
|
||||
protected Drawing CreateDrawing(List<Entity> entities)
|
||||
{
|
||||
var pgm = ConvertGeometry.ToProgram(entities);
|
||||
var pgm = ConvertGeometry.ToProgram(ShapeProfile.NormalizeEntities(entities));
|
||||
|
||||
if (pgm == null)
|
||||
throw new InvalidOperationException(
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
using OpenNest.Bending;
|
||||
using OpenNest.CNC;
|
||||
using OpenNest.CNC.Posting;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.IO;
|
||||
using OpenNest.Shapes;
|
||||
|
||||
namespace OpenNest.Tests.Shapes;
|
||||
|
||||
/// <summary>
|
||||
/// Shape library drawings cut in the CAD converter's directions: the outside clockwise and
|
||||
/// every cutout counter-clockwise.
|
||||
/// </summary>
|
||||
public class ShapeCutDirectionTests
|
||||
{
|
||||
public static TheoryData<ShapeDefinition> Shapes()
|
||||
{
|
||||
var shapes = new ShapeDefinition[]
|
||||
{
|
||||
new CircleShape(),
|
||||
new IsoscelesTriangleShape(),
|
||||
new LShape(),
|
||||
new NgonShape(),
|
||||
new PipeFlangeShape(),
|
||||
new PipeFlangeShape { Blind = true },
|
||||
new RectangleShape(),
|
||||
new RightTriangleShape(),
|
||||
new RingShape(),
|
||||
new RoundedRectangleShape(),
|
||||
new TShape(),
|
||||
new TrapezoidShape(),
|
||||
};
|
||||
|
||||
var data = new TheoryData<ShapeDefinition>();
|
||||
foreach (var shape in shapes)
|
||||
{
|
||||
shape.SetPreviewDefaults();
|
||||
data.Add(shape);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Shapes))]
|
||||
public void GetDrawing_CutsOutsideClockwiseAndCutoutsCounterClockwise(ShapeDefinition shape)
|
||||
{
|
||||
AssertConverterWinding(shape.GetDrawing().Program, shape.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opening a shape drawing in the CAD Converter and saving it rebuilds it from its program
|
||||
/// geometry; the cut directions must come back unchanged.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(Shapes))]
|
||||
public void OpenInConverter_KeepsTheShapesCutDirections(ShapeDefinition shape)
|
||||
{
|
||||
var drawing = shape.GetDrawing();
|
||||
var editable = CadImporter.EditableEntities(drawing);
|
||||
|
||||
var rebuilt = CadImporter.BuildDrawing(
|
||||
new CadImportResult { Entities = editable, Name = drawing.Name }, editable,
|
||||
new List<Bend>(), 1, null, null);
|
||||
|
||||
Assert.Equal(Directions(drawing.Program), Directions(rebuilt.Program));
|
||||
AssertConverterWinding(rebuilt.Program, shape.Name);
|
||||
}
|
||||
|
||||
private static void AssertConverterWinding(Program program, string name)
|
||||
{
|
||||
var contours = PostContours.Extract(program, Vector.Zero);
|
||||
var perimeter = ContourWinding.PerimeterIndex(contours);
|
||||
|
||||
Assert.True(perimeter >= 0, $"{name}: no perimeter");
|
||||
for (var i = 0; i < contours.Count; i++)
|
||||
{
|
||||
var area = ContourWinding.SignedArea(contours[i]);
|
||||
if (i == perimeter)
|
||||
Assert.True(area < 0, $"{name}: outside is not clockwise");
|
||||
else
|
||||
Assert.True(area > 0, $"{name}: cutout {i} is not counter-clockwise");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Each contour's winding sign, in a stable order so reordering does not matter.</summary>
|
||||
private static List<int> Directions(Program program) =>
|
||||
PostContours.Extract(program, Vector.Zero)
|
||||
.Select(c => System.Math.Sign(ContourWinding.SignedArea(c)))
|
||||
.OrderBy(s => s)
|
||||
.ToList();
|
||||
|
||||
[Fact]
|
||||
public void PipeFlange_HasCutouts()
|
||||
{
|
||||
var shape = new PipeFlangeShape();
|
||||
shape.SetPreviewDefaults();
|
||||
|
||||
var contours = PostContours.Extract(shape.GetDrawing().Program, Vector.Zero);
|
||||
|
||||
Assert.True(contours.Count > 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user