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> /// Return a deep copy of the object. /// </summary> /// <returns></returns> public GCodeCollector Duplicate() { GCodeCollector collector = new GCodeCollector(Codes.Count); foreach (var code in Codes) { collector.addCode(code.Duplicate()); } return(collector); }
/// <summary> /// Returns the minimum Z position of specified g-code collection. /// </summary> /// <param name="code">specified g-code collection</param> /// <returns></returns> public static double getZNull(GCodeCollector code) { double z = double.MaxValue; foreach (var c in code.Codes) { var l = c as GCodeLine; if (l != null && l.z != null && l.z.Value < z) { z = l.z.Value; } } return(z); }
/// <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)); }
/// <summary> /// Shifts specified g-code collection with the specified value. /// </summary> /// <param name="x">Added this value to all x positions.</param> /// <param name="y">Added this value to all y positions.</param> /// <param name="incode">specified g-code collection</param> /// <returns></returns> public static GCodeCollector codeShift(double x, double y, GCodeCollector incode) { GCodeCollector backcode = new GCodeCollector(incode.Codes.Count); foreach (var c in incode.Codes) { var l = c as GCodeLine; var els = c as GCodeEllipse; if (l != null) { if (l.x != null || l.y != null) { double?xk = l.x != null ? l.x + x : null; double?yk = l.y != null ? l.y + y : null; GCodeLine nl = new GCodeLine(xk, yk, l.z, l.f, l.e, l.SpeedMode); backcode.addCode(nl); continue; } backcode.addCode(l); } else if (els != null) { els = (GCodeEllipse)els.Duplicate(); foreach (var o in els.Code.Codes) { l = (GCodeLine)o; if (l.x != null || l.y != null) { double?xk = l.x != null ? l.x + x : null; double?yk = l.y != null ? l.y + y : null; l.x = xk; l.y = yk; } } backcode.addCode(els); } else { backcode.addCode(c); } } return(backcode); }
/// <summary> /// Constructs a new GCodeReader from a Stream object of g-code text file. /// </summary> /// <param name="file">Stream object of g-code text file</param> public GCodeReader(Stream file) { Code = new GCodeCollector(); string line; using (StreamReader text = new StreamReader(file)) { while ((line = text.ReadLine()) != null) { IGCode row = Read(line); if (row != null) { Code.addCode(row); } } } }
/// <summary> /// Shifts specified g-code collection with the specified value. /// </summary> /// <param name="z">Added this value to all z positions.</param> /// <param name="incode">specified g-code collection</param> /// <returns></returns> public static GCodeCollector codeShift(double z, GCodeCollector incode) { GCodeCollector backcode = new GCodeCollector(incode.Codes.Count); foreach (var c in incode.Codes) { var l = c as GCodeLine; if (l != null) { if (l.z != null) { GCodeLine nl = new GCodeLine(l.x, l.y, l.z + z, l.f, l.e, l.SpeedMode); backcode.addCode(nl); continue; } } backcode.addCode(c); } return(backcode); }
/// <summary> /// Appends all of the elements in the specified GCodeCollector to the end of this list. /// </summary> /// <param name="codes"> GCodeCollector containing elements to be added to this list</param> public void addCode(GCodeCollector codes) { Codes.AddRange(codes.Codes); }
public static void ArcByPoints(GCodeCollector code, double startX, double startY, double stopX, double stopY, double radius, double?speed = null, bool needExtrude = false, bool isClockwise = true) { }
/// <summary> /// Shifts the specified g-code collection to zero Z axis position. /// </summary> /// <param name="code">specified g-code collection</param> /// <returns></returns> public static GCodeCollector codeZnull(GCodeCollector code) { return(codeShift(-getZNull(code), code)); }
/// <summary> /// This function shifts to the equivalent position the codes of the object. /// </summary> /// <param name="code">Input code collection (byRef). The new codes are appended to this collection.</param> /// <param name="stagePositonX">X positon of center of the printing area</param> /// <param name="stagePositonY">Y positon of center of the printing area</param> /// <param name="zOff">z offset value</param> public void Print(GCodeCollector code, double stagePositonX, double stagePositonY, double zOff) { Print(code, new Point(stagePositonX, stagePositonY), zOff); }
/// <summary> /// Returns the center of bounding rectangle. /// </summary> /// <param name="code">specified g-code collection</param> /// <returns></returns> public static Point getCenter(GCodeCollector code) { return(bound(code).Center); }
/// <summary> /// Shifts the specified g-code collection into the center. /// </summary> /// <param name="code">specified g-code collection</param> /// <returns></returns> public static GCodeCollector setCorner(GCodeCollector code) { var b = bound(code); return(codeShift(-b.X1, -b.Y1, code)); }
/// <summary> /// Returns the bounding rectangle of the specified g-code collection. /// </summary> /// <param name="code">Specified g-code collection</param> /// <returns></returns> public static Rect bound(GCodeCollector code) { double minx = Double.MaxValue; double miny = Double.MaxValue; double maxx = Double.MinValue; double maxy = Double.MinValue; GCodeLine last = null; foreach (var c in code.Codes) { var l = c as GCodeLine; if (l != null) { last = l; if (l.x != null) { if (l.x.Value > maxx) { maxx = l.x.Value; } if (l.x.Value < minx) { minx = l.x.Value; } } if (l.y != null) { if (l.y.Value > maxy) { maxy = l.y.Value; } if (l.y.Value < miny) { miny = l.y.Value; } } } var els = c as GCodeEllipse; if (els != null) { foreach (var o in els.Code.Codes) { l = (GCodeLine)o; last = l; if (l.x != null) { if (l.x.Value > maxx) { maxx = l.x.Value; } if (l.x.Value < minx) { minx = l.x.Value; } } if (l.y != null) { if (l.y.Value > maxy) { maxy = l.y.Value; } if (l.y.Value < miny) { miny = l.y.Value; } } } } var e = c as GCodeCircle; if (last != null && e != null) { var rec = e.Bound; if (last.x != null) { if (last.x.Value + rec.MaxX > maxx) { maxx = last.x.Value + rec.MaxX; } if (last.x.Value + rec.MinX < minx) { minx = last.x.Value + rec.MinX; } } if (last.y != null) { if (last.y.Value + rec.MaxY > maxy) { maxy = last.y.Value + rec.MaxY; } if (last.y.Value + rec.MinY < miny) { miny = last.y.Value + rec.MinY; } } } } return(new Rect(minx, miny, maxx, maxy)); }
/// <summary> /// This function shifts to the equivalent position the codes of the object. /// </summary> /// <param name="code">Input code collection (byRef). The new codes are appended to this collection.</param> /// <param name="stagePositon">The position of center of the printing area</param> /// <param name="zOff">z offset value</param> public void Print(GCodeCollector code, Point stagePositon, double zOff) { var cc = GCodeTransforms.codeShift(zOff, Code.Duplicate()); code.addCode(GCodeTransforms.codeShift(stagePositon.X, stagePositon.Y, cc)); }
/// <summary> /// Shifts the center of specified g-code collection to the specified position. /// </summary> /// <param name="x">New X center of specified g-code collection</param> /// <param name="y">New Y center of specified g-code collection</param> /// <param name="code">specified g-code collection</param> /// <returns></returns> public static GCodeCollector codeShiftCenterTo(double x, double y, GCodeCollector code) { var center = getCenter(code); return(codeShift(x - center.X, y - center.Y, code)); }
public PetriTask(string name, string description, GCodeCollector mainCode) { this.Name = name; this.Description = description; Code = mainCode; }