public static string Generate(MachinePath machinePath, double maxSpindleRate, double maxFeedRate) { StringBuilder gcode = new StringBuilder(); gcode.AppendLine("G21"); gcode.AppendLine("G90"); gcode.AppendLine("M4 S0"); double currentPower = double.NaN; double currentSpeed = double.NaN; foreach (MachinePath.Travel travel in machinePath.Travels) { if (!travel.Rapid) { if (travel.Power == currentPower && travel.Speed == currentSpeed) { gcode.AppendFormat(CultureInfo.InvariantCulture, "G1 X{0:F2} Y{1:F2}", travel.Destination.X, travel.Destination.Y); } else { gcode.AppendFormat(CultureInfo.InvariantCulture, "G1 X{0:F2} Y{1:F2} S{2:F2} F{3:F2}", travel.Destination.X, travel.Destination.Y, travel.Power * maxSpindleRate, travel.Speed * maxFeedRate); currentPower = travel.Power; currentSpeed = travel.Speed; } } else { gcode.AppendFormat(CultureInfo.InvariantCulture, "G0 X{0:F2} Y{1:F2}", travel.Destination.X, travel.Destination.Y); currentPower = double.NaN; currentSpeed = double.NaN; } gcode.AppendLine(); } gcode.AppendLine("M5"); return(gcode.ToString()); }
private static MachinePath toMachinePath(IReadOnlyList <PathNode> schedule, double power, double speed) { MachinePath machinePath = new MachinePath(); machinePath.SetPowerAndSpeed(power, speed); Point lastEndPoint = new Point(0.0, 0.0); foreach (PathNode node in schedule) { node.LocateNearestStartPoint(lastEndPoint); if (node.Path.Closed) { for (int i = 0; i <= node.Path.Points.Count; i++) { machinePath.TravelTo(node.Path.Points[(node.StartIndex + i) % node.Path.Points.Count]); } } else if (node.StartIndex == 0) { for (int i = 0; i < node.Path.Points.Count; i++) { machinePath.TravelTo(node.Path.Points[i]); } } else { for (int i = node.Path.Points.Count - 1; i >= 0; i--) { machinePath.TravelTo(node.Path.Points[i]); } } machinePath.EndCut(); lastEndPoint = node.EndPoint; } return(machinePath); }