Files
OpenNest/OpenNest.Core/CNC/SubProgramCall.cs
AJ Isaacs 24babe353e fix: show both offset and rotation in SubProgramCall.ToString
The either/or format meant a SubProgramCall with both a non-zero
Offset and non-zero Rotation would only show the Offset, hiding the
rotation metadata. The data model supports both independently, so the
display should too.

Also fixes a zero-field leak where the old fallback emitted
`G65 P_ R0` for calls with no rotation. Now each field is only shown
when non-zero, and `G65 P_` with no arguments is emitted when
neither is set.

Note: SubProgramCall.ToString is purely a debug/display aid. The
Cincinnati post emits sub-calls via the G52 + M98 bracket, not via
G65, so this format doesn't correspond to real machine output.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 08:37:46 -04:00

104 lines
2.6 KiB
C#

using System.Text;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.CNC
{
public class SubProgramCall : ICode
{
private double rotation;
private Program program;
public SubProgramCall()
{
}
public SubProgramCall(Program program, double rotation)
{
this.program = program;
this.Rotation = rotation;
}
/// <summary>
/// The program ID.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Gets or sets the program.
/// </summary>
public Program Program
{
get { return program; }
set
{
program = value;
UpdateProgramRotation();
}
}
/// <summary>
/// Gets or sets the offset (position) at which the sub-program is executed.
/// For hole sub-programs, this is the hole center.
/// </summary>
public Vector Offset { get; set; }
/// <summary>
/// Gets or sets the rotation of the program in degrees.
/// </summary>
public double Rotation
{
get { return rotation; }
set
{
rotation = value;
UpdateProgramRotation();
}
}
/// <summary>
/// Rotates the program by the difference of the current
/// rotation set in the sub program call and the program.
/// </summary>
private void UpdateProgramRotation()
{
if (program != null)
{
var diffAngle = Angle.ToRadians(rotation) - program.Rotation;
if (!diffAngle.IsEqualTo(0.0))
program.Rotate(diffAngle);
}
}
/// <summary>
/// Gets the code type.
/// </summary>
/// <returns></returns>
public CodeType Type
{
get { return CodeType.SubProgramCall; }
}
/// <summary>
/// Gets a shallow copy.
/// </summary>
/// <returns></returns>
public ICode Clone()
{
return new SubProgramCall(program, Rotation) { Id = Id, Offset = Offset };
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append($"G65 P{Id}");
if (Offset.X != 0 || Offset.Y != 0)
sb.Append($" X{Offset.X} Y{Offset.Y}");
if (Rotation != 0)
sb.Append($" R{Rotation}");
return sb.ToString();
}
}
}