Program.Clone deep-copied the SubPrograms dictionary but left every SubProgramCall pointing at the source's sub-program, and SubProgramCall.Clone went through the Rotation setter, which re-rotated that shared program to the call's stale angle. Copying a program with hole lead-ins therefore rotated the source's holes, and rotating the copy rotated the source again. Program.Rotate also rotated a shared sub-program once per call, so two identical holes (one deduplicated sub-program) turned twice. Clone now binds calls to one private copy per shared sub-program without re-aligning it, and Rotate turns each distinct sub-program once.
118 lines
3.2 KiB
C#
118 lines
3.2 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 that references the same program. Copies the fields
|
|
/// directly: going through the setters would re-align (rotate) the shared program.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public ICode Clone()
|
|
{
|
|
return new SubProgramCall
|
|
{
|
|
program = program,
|
|
rotation = rotation,
|
|
Id = Id,
|
|
Offset = Offset,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Points the call at <paramref name="copy"/>, a copy of its current program, without
|
|
/// re-aligning its rotation: the copy already has the geometry the call executes.
|
|
/// </summary>
|
|
internal void BindProgram(Program copy)
|
|
{
|
|
program = copy;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|