/// <summary> /// Add a curve to the input GCodeCollector; /// </summary> /// <param name="code">input GCodeCollector</param> /// <param name="centerX">center X of curve</param> /// <param name="centerY">center y of curve</param> /// <param name="radius">radius of curve</param> /// <param name="startDegree">start degree of curve</param> /// <param name="stopDegree">start degree of curve</param> /// <param name="speed">The feed-rate per minute of the move between the starting point and ending point</param> /// <param name="needExtrude">set true for extruding</param> public static void Arc(GCodeCollector code, double centerX, double centerY, double radius, double startDegree, double stopDegree, double?speed = null, bool needExtrude = false, bool isClockwise = true) { double dist = 0; if (isClockwise) { if (stopDegree < startDegree) { dist = startDegree - stopDegree; } else { dist = 360 - stopDegree + startDegree; } } else { if (stopDegree < startDegree) { dist = 360 - startDegree + stopDegree; } else { dist = stopDegree - startDegree; } } //double dist = Math.Abs(stopDegree - startDegree); double sign = isClockwise ? -1 : 1; var p = Point.PolarToCartesian(radius, startDegree, centerX, centerY); code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, code.LastPoint) : (double?)null)); for (int i = 1; i < dist; i += 5) { p = Point.PolarToCartesian(radius, startDegree + sign * i, centerX, centerY); code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, code.LastPoint) : (double?)null)); } p = Point.PolarToCartesian(radius, stopDegree, centerX, centerY); code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, code.LastPoint) : (double?)null)); }
private void Generate() { code = new GCodeCollector(); double dist = 0; if (isClockwise) { if (stopDeg < startDeg) { dist = startDeg - stopDeg; } else { dist = 360 - (stopDeg - startDeg); } } else { if (stopDeg < startDeg) { dist = 360 - startDeg + stopDeg; } else { dist = stopDeg - startDeg; } } if (dist == 0) { dist = 360; } var startRad = MathExtension.DegreeToRadian(startDeg); var stopRad = MathExtension.DegreeToRadian(stopDeg); double sign = isClockwise ? -1 : 1; Point p = new Point(center.X + AxisA / 2d * Math.Cos(startRad), center.Y + AxisB / 2d * Math.Sin(startRad)); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: null)); double a = AxisA / 2; double b = AxisB / 2; double c = Math.PI * (3 * (a + b) - Math.Sqrt((3 * a + b) * (a + 3 * b))); double degStep = Math.Min(360d / 8d, dist / (c * dist / 360 / MinArc)); for (double i = degStep; i < dist; i += degStep) { p = new Point(center.X + AxisA / 2d * Math.Cos(MathExtension.DegreeToRadian(startDeg + i * sign)), center.Y + AxisB / 2d * Math.Sin(MathExtension.DegreeToRadian(startDeg + i * sign))); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, Code.LastPoint) : (double?)null)); } p = new Point(center.X + AxisA / 2d * Math.Cos(stopRad), center.Y + AxisB / 2d * Math.Sin(stopRad)); Code.addCode(new GCodeLine(x: p.X, y: p.Y, speed: Speed, extrude: needExtrude ? GCodeSpecial.ExtrudeLine(p, Code.LastPoint) : (double?)null)); }
/// <summary> /// Translates and returns an IGCode object of specified string. /// </summary> /// <param name="line">String to be translated</param> /// <returns></returns> public static IGCode Read(string line) { string org = line; string[] part = line.Split(';'); line = part[0].Trim(); if (line.Length == 0) { return(null); } part = part[0].Split(' '); Regex regex = new Regex(@"M\d+"); Match match = regex.Match(part[0]); if (match.Success) { switch (match.Value) { case "M82": return(GCodeSpecial.setExtruderAbsoluteMode()); case "M83": return(GCodeSpecial.setExtruderRelativeMode()); case "M84": return(GCodeSpecial.StepperOff()); case "M104": return(GCodeSpecial.HeaterOff()); case "M106": double?s = getValue(part, "S"); if (s != null) { return(GCodeSpecial.FanOn(s.Value)); } break; case "M107": return(GCodeSpecial.FanOff()); case "M109": regex = new Regex(@"T\d+"); double?s1 = getValue(part, "S"); if (s1 != null) { return(GCodeSpecial.HeaterAndWait(s1.Value)); } break; case "M117": return(null); case "M140": return(null); } } regex = new Regex(@"G\d+"); match = regex.Match(part[0]); if (match.Success) { var xg0 = getValue(part, "X"); var yg0 = getValue(part, "Y"); var zg0 = getValue(part, "Z"); var fg0 = getValue(part, "F"); var eg0 = getValue(part, "E"); switch (match.Value) { case "G0": return(new GCodeLine(xg0, yg0, zg0, fg0, eg0, GCodeLine.SpeedModes.Fast)); case "G1": return(new GCodeLine(xg0, yg0, zg0, fg0, eg0, GCodeLine.SpeedModes.Slow)); case "G21": return(GCodeSpecial.setMetricValues()); case "G28": var x1 = getValue(part, "X"); var y1 = getValue(part, "Y"); var z1 = getValue(part, "Z"); //if (x1 != null || y1 != null || z1 != null) return(GCodeSpecial.GoToHome(x1 != null, y1 != null, z1 != null)); case "G90": return(GCodeSpecial.setAbsolutePositionMode()); case "G91": return(GCodeSpecial.setRelavitvePositionMode()); case "G92": var v1 = getValue(part, "Z"); var v2 = getValue(part, "E"); if (v1 != null) { return(GCodeSpecial.ResetToZ(v1.Value)); } if (v2 != null) { return(GCodeSpecial.ResetToExtrude(v2.Value)); } break; } } if (part[0].Equals("T0")) { return(null); } return(new UnknownGCode(line)); }