/// <summary> /// mirror gcode /// </summary> public static string transformGCodeMirror(translate shiftToZero = translate.MirrorX) { Logger.Debug("Mirror {0}", shiftToZero); #if (debuginfo) log.Add(" GCodeVisu transform Mirror"); #endif if (gcodeList == null) { return(""); } xyPoint centerOfFigure = xyzSize.getCenter(); if (lastFigureNumber > 0) { centerOfFigure = getCenterOfMarkedFigure(); } oldLine.resetAll(grbl.posWork); // reset coordinates and parser modes clearDrawingnPath(); // reset path, dimensions foreach (gcodeByLine gcline in gcodeList) { if ((lastFigureNumber > 0) && (gcline.figureNumber != lastFigureNumber)) { continue; } if (!gcline.ismachineCoordG53) { // switch circle direction if ((shiftToZero == translate.MirrorX) || (shiftToZero == translate.MirrorY)) // mirror xy { if (gcline.motionMode == 2) { gcline.motionMode = 3; } else if (gcline.motionMode == 3) { gcline.motionMode = 2; } } if (shiftToZero == translate.MirrorX) // mirror x { if (gcline.x != null) { // if (gcline.isdistanceModeG90) // gcline.x = oldmaxx - gcline.x; // else gcline.x = -gcline.x + 2 * centerOfFigure.X; } gcline.i = -gcline.i; } if (shiftToZero == translate.MirrorY) // mirror y { if (gcline.y != null) { // if (gcline.isdistanceModeG90) // gcline.y = oldmaxy - gcline.y; // else gcline.y = -gcline.y + 2 * centerOfFigure.Y; } gcline.j = -gcline.j; } if (shiftToZero == translate.MirrorRotary) // mirror rotary { string rotary = Properties.Settings.Default.ctrl4thName; if ((rotary == "A") && (gcline.a != null)) { gcline.a = -gcline.a; } else if ((rotary == "B") && (gcline.b != null)) { gcline.b = -gcline.b; } else if ((rotary == "C") && (gcline.c != null)) { gcline.c = -gcline.c; } } calcAbsPosition(gcline, oldLine); oldLine = new gcodeByLine(gcline); // get copy of newLine } } return(createGCodeProg()); }
public string transformGCode(IList <string> oldCode, double scale, double angle, translate shiftToZero = translate.None) { string[] GCode = oldCode.ToArray <string>(); StringBuilder newCode = new StringBuilder(); StringBuilder newtext = new StringBuilder(); double offsetX = 0; double offsetY = 0; double oldmaxx = xyzSize.maxx; double oldmaxy = xyzSize.maxy; if (shiftToZero == translate.Offset0) { offsetX = xyzSize.minx; offsetY = xyzSize.miny; } clear(); // clear old drawing-paths // showChangedMessage = true; bool receivedX = false; // remember if X,Y word was given bool receivedY = false; // remember if X,Y word was given bool receivedZ = false; bool receivedI = false; // remember if I,J word was given bool receivedJ = false; // remember if I,J word was given bool applyscale = false; // only change code if scale is given bool applyrotation = false; // only change code if scale is given double factor = scale / 100; if (scale != 100) { applyscale = true; // recalculate coordinates and replace code-line } if (angle != 0.0) { applyrotation = true; // recalculate coordinates and replace code-line } // string newCode=""; bool codeWasChanged = false; string matchText = ""; double value, valx, valy, valz, vali, valj, newvali, newvalj; double?newvalx, newvaly, newvalz; // string tokens = "(X-?\\d+(.\\d+)?)|(Y-?\\d+(.\\d+)?)|(Z-?\\d+(.\\d+)?)|(I-?\\d+(.\\d+)?)|(J-?\\d+(.\\d+)?)"; string tokens = "(X[-\\d]*(.\\d+)?)|(Y[-\\d]*(.\\d+)?)|(Z[-\\d]*(.\\d+)?)|(I[-\\d]*(.\\d+)?)|(J[-\\d]*(.\\d+)?)"; // Index-assignment for m.Groups[index] 1 2 3 4 5 6 System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(tokens); System.Text.RegularExpressions.MatchCollection mc; string singleLine, origLine, tmpLine; valx = 0; valy = 0; valz = 0; vali = 0; valj = 0; newvalx = null; newvaly = null; newvalz = null; newvali = 0; newvalj = 0; // MessageBox.Show(fCTBCode.LinesCount.ToString()); for (int index = 0; index < GCode.Length; index++) { singleLine = GCode[index]; origLine = singleLine; if (singleLine.IndexOf(';') >= 0) { singleLine = singleLine.Substring(0, singleLine.IndexOf(';')); // remove comments } int c_start, c_end; if (((c_start = singleLine.IndexOf('(')) >= 0) && ((c_end = singleLine.IndexOf(')')) >= 0)) { string part1 = singleLine.Substring(0, c_start); string part2 = singleLine.Substring(c_end + 1); singleLine = part1 + part2; } receivedX = false; receivedY = false; receivedZ = false; receivedI = false; receivedJ = false; newvalx = null; newvaly = null; newvalz = null; newvali = 0; newvalj = 0; vali = 0; valj = 0; mc = rex.Matches(singleLine.ToUpper()); if ((singleLine.Length > 1) && (mc.Count > 0)) { int firstIndex = mc[0].Index; int lastIndex = mc[mc.Count - 1].Index; // singleLine.Length; if (firstIndex > 1) { tmpLine = singleLine.Substring(0, firstIndex - 1); } else { tmpLine = singleLine; } foreach (System.Text.RegularExpressions.Match m in mc) { int startIndex = m.Index; int StopIndex = m.Length; matchText = singleLine.Substring(startIndex, StopIndex); Double.TryParse(matchText.Substring(1), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out value); if (applyscale && !(m.Groups[5].Success || m.Groups[6].Success)) // don't scale Z { value = value * factor; } if (m.Groups[1].Success || m.Groups[2].Success) // X { valx = value - offsetX; newvalx = valx; receivedX = true; } if (m.Groups[3].Success || m.Groups[4].Success) // Y { valy = value - offsetY; newvaly = valy; receivedY = true; } if (m.Groups[5].Success || m.Groups[6].Success) // Z { valz = value; newvalz = value; receivedZ = true; } if (m.Groups[7].Success || m.Groups[8].Success) // I { vali = value; newvali = value; receivedI = true; } if (m.Groups[9].Success || m.Groups[10].Success) // J { valj = value; newvalj = value; receivedJ = true; } lastIndex = startIndex + StopIndex; } if ((applyrotation && (receivedX || receivedY))) { newvalx = valx * Math.Cos(angle * Math.PI / 180) - valy * Math.Sin(angle * Math.PI / 180); newvaly = valx * Math.Sin(angle * Math.PI / 180) + valy * Math.Cos(angle * Math.PI / 180); receivedX = true; receivedY = true; } if ((applyrotation && (receivedI || receivedJ))) { newvali = vali * Math.Cos(angle * Math.PI / 180) - valj * Math.Sin(angle * Math.PI / 180); newvalj = vali * Math.Sin(angle * Math.PI / 180) + valj * Math.Cos(angle * Math.PI / 180); receivedI = true; receivedJ = true; } if ((shiftToZero == translate.MirrorX) || (shiftToZero == translate.MirrorY)) // mirror xy { string tmp = tmpLine; string Gnr = gcode.getStrGCode('G', tmpLine); if (Gnr.Length > 1) { int cmdnr = Convert.ToInt16(Gnr.Substring(1)); if (cmdnr == 2) { tmp = tmpLine.Replace(Gnr, "G03"); } if (cmdnr == 3) { tmp = tmpLine.Replace(Gnr, "G02"); } tmpLine = tmp; } } if (shiftToZero == translate.MirrorX) // mirror x { if (newvalx != null) { newvalx = oldmaxx - newvalx; } newvali = -newvali; } if (shiftToZero == translate.MirrorY) // mirror y { if (newvaly != null) { newvaly = oldmaxy - newvaly; } newvalj = -newvalj; } if (applyscale || applyrotation || (shiftToZero > 0)) { newtext.Clear(); newtext.Append(tmpLine); // + string.Format(" X{0:0.0000} Y{1:0.0000}", newvalx, newvaly).Replace(',', '.'); if (receivedX) { newtext.AppendFormat(" X{0}", gcode.frmtNum((double)newvalx)); } if (receivedY) { newtext.AppendFormat(" Y{0}", gcode.frmtNum((double)newvaly)); } if (receivedZ) { newtext.AppendFormat(" Z{0}", gcode.frmtNum((double)newvalz)); } if (receivedI) { newtext.AppendFormat(" I{0}", gcode.frmtNum(newvali)); } if (receivedJ) { newtext.AppendFormat(" J{0}", gcode.frmtNum(newvalj)); } newtext.Append(origLine.Substring(lastIndex)); newtext.Replace(',', '.'); codeWasChanged = true; } if (receivedX || receivedY || receivedI || receivedJ || receivedZ) { addGCode(tmpLine, newvalx, newvaly, newvalz, newvali, newvalj); } } // end if (mc.Count > 0) // singleLine = origLine; //restore comments if (receivedX || receivedY || receivedI || receivedJ || receivedZ) { newCode.AppendLine(newtext.ToString()); } else { newCode.AppendLine(origLine); // singleLine); } } if (codeWasChanged) { return(newCode.ToString()); } // newCode.Append(oldCode); return(string.Join("\r", GCode));//.ToString(); }