feat: add VariableDefinition type for G-code user variables

Adds immutable VariableDefinition record to OpenNest.CNC with name,
expression, resolved value, inline, and global flags. Fixes namespace
collision in PatternTilerTests and PolygonHelperTests caused by the new
OpenNest.Tests.CNC namespace.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 09:46:37 -04:00
parent 287023d802
commit 1040db414f
4 changed files with 66 additions and 11 deletions

View File

@@ -0,0 +1,21 @@
namespace OpenNest.CNC
{
public sealed class VariableDefinition
{
public string Name { get; }
public string Expression { get; }
public double Value { get; }
public bool Inline { get; }
public bool Global { get; }
public VariableDefinition(string name, string expression, double value,
bool inline = false, bool global = false)
{
Name = name;
Expression = expression;
Value = value;
Inline = inline;
Global = global;
}
}
}

View File

@@ -0,0 +1,33 @@
using OpenNest.CNC;
namespace OpenNest.Tests.CNC;
public class VariableDefinitionTests
{
[Fact]
public void Constructor_SetsAllProperties()
{
var v = new VariableDefinition("diameter", "0.3", 0.3);
Assert.Equal("diameter", v.Name);
Assert.Equal("0.3", v.Expression);
Assert.Equal(0.3, v.Value);
Assert.False(v.Inline);
Assert.False(v.Global);
}
[Fact]
public void Constructor_WithFlags_SetsFlags()
{
var v = new VariableDefinition("width", "48.0", 48.0, inline: true, global: true);
Assert.True(v.Inline);
Assert.True(v.Global);
}
[Fact]
public void DefaultFlags_AreFalse()
{
var v = new VariableDefinition("x", "1", 1.0);
Assert.False(v.Inline);
Assert.False(v.Global);
}
}

View File

@@ -1,3 +1,4 @@
using OpenNest.CNC;
using OpenNest.Engine.Fill;
using OpenNest.Geometry;
@@ -7,11 +8,11 @@ public class PatternTilerTests
{
private static Drawing MakeSquareDrawing(double size)
{
var pgm = new CNC.Program();
pgm.Codes.Add(new CNC.LinearMove(size, 0));
pgm.Codes.Add(new CNC.LinearMove(size, size));
pgm.Codes.Add(new CNC.LinearMove(0, size));
pgm.Codes.Add(new CNC.LinearMove(0, 0));
var pgm = new Program();
pgm.Codes.Add(new LinearMove(size, 0));
pgm.Codes.Add(new LinearMove(size, size));
pgm.Codes.Add(new LinearMove(0, size));
pgm.Codes.Add(new LinearMove(0, 0));
return new Drawing("square", pgm);
}

View File

@@ -57,12 +57,12 @@ public class PolygonHelperTests
public void ExtractPerimeterPolygon_InflatedPolygonIsLarger_ForCCWWinding()
{
// CCW winding: (0,0)→(10,0)→(10,10)→(0,10)→(0,0)
var pgm = new CNC.Program();
pgm.Codes.Add(new CNC.RapidMove(new Vector(0, 0)));
pgm.Codes.Add(new CNC.LinearMove(new Vector(10, 0)));
pgm.Codes.Add(new CNC.LinearMove(new Vector(10, 10)));
pgm.Codes.Add(new CNC.LinearMove(new Vector(0, 10)));
pgm.Codes.Add(new CNC.LinearMove(new Vector(0, 0)));
var pgm = new Program();
pgm.Codes.Add(new RapidMove(new Vector(0, 0)));
pgm.Codes.Add(new LinearMove(new Vector(10, 0)));
pgm.Codes.Add(new LinearMove(new Vector(10, 10)));
pgm.Codes.Add(new LinearMove(new Vector(0, 10)));
pgm.Codes.Add(new LinearMove(new Vector(0, 0)));
var drawing = new Drawing("ccw-square", pgm);
var noSpacing = PolygonHelper.ExtractPerimeterPolygon(drawing, 0);