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;
}
///
/// The program ID.
///
public int Id { get; set; }
///
/// Gets or sets the program.
///
public Program Program
{
get { return program; }
set
{
program = value;
UpdateProgramRotation();
}
}
///
/// Gets or sets the offset (position) at which the sub-program is executed.
/// For hole sub-programs, this is the hole center.
///
public Vector Offset { get; set; }
///
/// Gets or sets the rotation of the program in degrees.
///
public double Rotation
{
get { return rotation; }
set
{
rotation = value;
UpdateProgramRotation();
}
}
///
/// Rotates the program by the difference of the current
/// rotation set in the sub program call and the program.
///
private void UpdateProgramRotation()
{
if (program != null)
{
var diffAngle = Angle.ToRadians(rotation) - program.Rotation;
if (!diffAngle.IsEqualTo(0.0))
program.Rotate(diffAngle);
}
}
///
/// Gets the code type.
///
///
public CodeType Type
{
get { return CodeType.SubProgramCall; }
}
///
/// Gets a shallow copy.
///
///
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();
}
}
}