feat(core): add FlangeShape with JSON preset loading

FlangeShape generates an outer circle with evenly-spaced bolt holes
on a bolt circle pattern. ShapeDefinition.LoadFromJson<T>() provides
generic JSON loading for any shape — no separate preset classes needed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-17 11:36:26 -04:00
parent d4222db0e8
commit bfd740c81e
3 changed files with 156 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using OpenNest.Geometry;
namespace OpenNest.Shapes
{
public class FlangeShape : ShapeDefinition
{
public double NominalPipeSize { get; set; }
public double OD { get; set; }
public double HoleDiameter { get; set; }
public double HolePatternDiameter { get; set; }
public int HoleCount { get; set; }
public override Drawing GetDrawing()
{
var entities = new List<Entity>();
// Outer circle
entities.Add(new Circle(0, 0, OD / 2.0));
// Bolt holes evenly spaced on the bolt circle
var boltCircleRadius = HolePatternDiameter / 2.0;
var holeRadius = HoleDiameter / 2.0;
var angleStep = 2.0 * System.Math.PI / HoleCount;
for (var i = 0; i < HoleCount; i++)
{
var angle = i * angleStep;
var cx = boltCircleRadius * System.Math.Cos(angle);
var cy = boltCircleRadius * System.Math.Sin(angle);
entities.Add(new Circle(cx, cy, holeRadius));
}
return CreateDrawing(entities);
}
}
}

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using OpenNest.Converters;
using OpenNest.Geometry;
@@ -7,6 +9,11 @@ namespace OpenNest.Shapes
{
public abstract class ShapeDefinition
{
private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
public string Name { get; set; }
protected ShapeDefinition()
@@ -19,6 +26,12 @@ namespace OpenNest.Shapes
public abstract Drawing GetDrawing();
public static List<T> LoadFromJson<T>(string path) where T : ShapeDefinition
{
var json = File.ReadAllText(path);
return JsonSerializer.Deserialize<List<T>>(json, JsonOptions);
}
protected Drawing CreateDrawing(List<Entity> entities)
{
var pgm = ConvertGeometry.ToProgram(entities);