mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-16 12:35:34 -05:00
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>
71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using OpenNest.Converters;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Shapes
|
|
{
|
|
public abstract class ShapeDefinition
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
};
|
|
|
|
public string Name { get; set; }
|
|
|
|
protected ShapeDefinition()
|
|
{
|
|
var typeName = GetType().Name;
|
|
Name = typeName.EndsWith("Shape")
|
|
? typeName.Substring(0, typeName.Length - 5)
|
|
: typeName;
|
|
}
|
|
|
|
public abstract Drawing GetDrawing();
|
|
|
|
public virtual string GenerateName()
|
|
{
|
|
var typeName = GetType().Name;
|
|
return typeName.EndsWith("Shape")
|
|
? typeName.Substring(0, typeName.Length - 5)
|
|
: typeName;
|
|
}
|
|
|
|
public virtual void SetPreviewDefaults() { }
|
|
|
|
/// <summary>
|
|
/// Where the Shape Library draws each parameter's dimension, in the coordinates of the
|
|
/// entities <see cref="GetDrawing"/> builds. Parameters without one are shown as notes.
|
|
/// </summary>
|
|
public virtual IReadOnlyList<ShapeDimension> GetDimensions() => Array.Empty<ShapeDimension>();
|
|
|
|
public static List<T> LoadFromJson<T>(string path)
|
|
where T : ShapeDefinition
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
return JsonSerializer.Deserialize<List<T>>(json, JsonOptions);
|
|
}
|
|
|
|
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(ShapeProfile.NormalizeEntities(entities));
|
|
|
|
if (pgm == null)
|
|
throw new InvalidOperationException(
|
|
$"Failed to create program for shape '{Name}'. Check that parameters produce valid geometry."
|
|
);
|
|
|
|
return new Drawing(Name, pgm);
|
|
}
|
|
}
|
|
}
|