public GCodeLine(string l, GCodeLine prevLn) { inputLine = localLine = l; CNCPoint curPosition = prevLn.ToPosition; FromPosition = curPosition; if (inputLine.Trim().StartsWith("(")) { } //Remove line number, if presents RemoveLineNumber(); //Remove comments RemoveComments(); curX = curPosition.x.ToString(); curY = curPosition.y.ToString(); curZ = curPosition.z.ToString(); ParseCoord(); ProcessCommand(); this.prev = prevLn; prevLn.next = this; LineNum = GetCurrentLineNumber(); }
/// <summary> /// Start to run a GCode file, if the file has been loaded /// </summary> public void StartFile() { if (this.gcodeFile == null) { return; } gCodeArgs.FileStatus = FileStatus.Running; //GCodeLine startLine = gcodeFile.GetStartLine(); //Send(startLine.inputLine); GCodeLine startLn = gcodeFile.ReadNextGCodeLine(); gcodeFile.CurrentLine++; if (startLn == null) { string filePath = gcodeFile.FilePath; OpenGCode(filePath); startLn = gcodeFile.ReadNextGCodeLine(); } if (startLn.IsSupported) { Send(startLn.inputLine); } else { Send("?"); } }
public GCodeLine Head() { GCodeLine head = this; while (head.prev != null) { head = head.prev; } return(head); }
public GCodeLine NextSupportedLine() { GCodeLine ln = curLine.next; while (ln != null && !ln.IsSupported) { ln = ln.next; } curLine = ln; return(curLine); }
public GCodeLine GetStartLine() { GCodeLine line = curLine; while (!line.IsSupported) { line = line.next; } curLine = line; return(curLine); }
/// <summary> /// Get next line from GCode file and send it to grbl port /// </summary> private void SendNextLineNew() { // if (IsPause) { return; } //string line = gcodeFile.NextLine(); //GCodeLine gcodeline = gcodeFile.NextSupportedLine(); GCodeLine gcodeline = gcodeFile.ReadNextGCodeLine(); //GCodeLine gcodeline = gcodeFile.NextSupportedLine(); if (gcodeline != null) { Send(gcodeline.inputLine); gCodeArgs.gcode = gcodeline.inputLine; gCodeArgs.CurrentLine = gcodeFile.CurrentLineNum; gCodeArgs.TotalLine = gcodeFile.TotalLines; GCodeStatusUpdate(this, gCodeArgs); } else { gCodeArgs.FileStatus = FileStatus.Stopped; GCodeStatusUpdate(this, gCodeArgs); } //if (!line.Trim().Equals("EOF") && line.Trim().Length > 0) //{ // logger.Info("Sending Line ......."); // Send(line); // gcodeFile.CurrentLine++; // gCodeArgs.gcode = line; // gCodeArgs.CurrentLine = gcodeFile.CurrentLine; // gCodeArgs.TotalLine = gcodeFile.TotalLines; // GCodeStatusUpdate(this, gCodeArgs); //} //else if (line.Trim().Equals("EOF")) //{ // logger.Info("End of File ......"); // IsFile = false; //} //else if (line.Trim().Length == 0) //{ // logger.Info("Empty Line ...."); // return; //} //else //{ // logger.Info("Unknown ......................"); //} }
public int Count() { int cnt = 1; GCodeLine p = this.next; while (p != null) { cnt++; p = p.next; } return(cnt); }
public void PrintAll() { GCodeLine cur = this.Head(); Console.WriteLine(cur.Count()); //Console.WriteLine(cur.PrintPosition()); while (cur.next != null) { cur = cur.next; Console.WriteLine(cur.PrintPosition()); } }
/// <summary> /// Load GCode from a file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static List <GCodeLine> LoadFromFile(string fileName) { int idx = 0; List <GCodeLine> list = new List <GCodeLine>(); //Create a fist line to setup initial positions, in case no information from grbl GCodeLine firstLn = new GCodeLine() { Index = idx }; firstLn.SetInitialPosition(); list.Add(firstLn); //Start reading file string ln = string.Empty; StreamReader sr = new StreamReader(fileName); while ((ln = sr.ReadLine()) != null) { try { idx++; //Create a GCode Line GCodeLine gcodeLn = new GCodeLine(ln) { Index = idx }; if (gcodeLn.comment.Length > 0) { continue; } list.Add(gcodeLn); //Create linked list GCodeLine prevCode = list[list.Count - 2]; gcodeLn.prev = prevCode; prevCode.next = gcodeLn; if (gcodeLn.IsPositionCode) { //gcodeLn.CalculateTravelDist(); //gcodeLn.GetCurrentDir(); } } catch (Exception ex) { Console.WriteLine(ex.Message + "<<" + ln + ">>"); } } return(list); }
public int GetCurrentLineNumber() { int cnt = 1; GCodeLine tempL = this; while (tempL.prev != null) { tempL = tempL.prev; cnt++; } return(cnt); }
public int PositionCount() { int cnt = 0; GCodeLine p = this; while (p != null) { if (p.isPosition) { cnt++; } p = p.Next(); } return(cnt); }
public int CurrentZDir() { int dir = 0; GCodeLine p = this.prev; while (p != null) { if (p.zDir != 0) { dir = p.zDir; break; } p = p.prev; } return(dir); }
public GCodeLine ReadNextGCodeLine() { GCodeLine gcodeLn = null; string ln = stream_in.ReadLine(); if (ln == null) { ln = "EOF"; CurrentLineNum++; } else { gcodeLn = new GCodeLine(ln); CurrentLineNum++; } return(gcodeLn); }
/// <summary> /// Open the gcode file /// </summary> /// <param name="filename"></param> public void OpenFile(string filename) { //Set the file name _filename = filename; //If the file exist if (File.Exists(filename)) { gcodeLines = new List <GCodeLine>(); ///Open the file and get the line count using (StreamReader sr = new StreamReader(_filename)) { string content = sr.ReadToEnd(); sr.Close(); string[] lns = content.Split('\n'); lines = new List <string>(); foreach (string ln in lns) { AddGCodeLine(ln); lines.Add(ln.Trim()); } TotalLines = lns.Length; CurrentLineNum = 0; content = string.Empty; sr.Close(); } curLine = gcodeLines[0]; //stream = new FileStream(filename, FileMode.Open, FileAccess.Read); //fileLength = stream.Length; Status = GCodeFileStatusEnum.Loaded; } else { Status = GCodeFileStatusEnum.Error; } ResetSeek(); }
public BoundingBox GetBoundingBox() { BoundingBox bbox = new BoundingBox() { XMax = -1000, XMin = 1000, YMax = -1000, YMin = 1000, ZMax = -1000, ZMin = 1000 }; GCodeLine curLn = this; while (curLn != null) { CNCPoint toPt = curLn.ToPosition; if (toPt.x < bbox.XMin) { bbox.XMin = toPt.x; } if (toPt.x > bbox.XMax) { bbox.XMax = toPt.x; } if (toPt.y < bbox.YMin) { bbox.YMin = toPt.y; } if (toPt.y > bbox.YMax) { bbox.YMax = toPt.y; } if (toPt.z < bbox.ZMin) { bbox.ZMin = toPt.z; } if (toPt.z > bbox.ZMax) { bbox.ZMax = toPt.z; } curLn = curLn.next; } return(bbox); }
public void AddGCodeLine(string ln) { if (gcodeLines == null) { gcodeLines = new List <GCodeLine>(); } ln = ln.Trim(); if (gcodeLines.Count == 0) { GCodeLine newLn = new GCodeLine(ln.Trim()); newLn.SetInitialPosition(); gcodeLines.Add(newLn); } else { GCodeLine prevLn = gcodeLines[gcodeLines.Count - 1]; GCodeLine newLn = new GCodeLine(ln.Trim(), prevLn); gcodeLines.Add(newLn); } }
public void GetBoundaryBox(out float xmin, out float xmax, out float ymin, out float ymax) { xmin = 0; xmax = 0; ymin = 0; ymax = 0; GCodeLine head = this.Head(); while (head != null) { if (!head.isPosition) { head = head.next; continue; } float xd = float.Parse(head.x); float yd = float.Parse(head.y); float zd = float.Parse(head.z); if (xd < xmin) { xmin = xd; } if (xd > xmax) { xmax = xd; } if (yd < ymin) { ymin = yd; } if (yd > ymax) { ymax = yd; } head = head.next; } }
public GCodeLine AddGCodeLine(string l) { GCodeLine newLine = new GCodeLine(l, this); return(newLine); }
private double CalDistance(GCodeLine p1, GCodeLine p2) { return(Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2) + Math.Pow(p2.Z - p1.Z, 2))); }