public void Apply() { using (new UIUtils.WaitCursor()) { List <string> gc = GCodeParser.TokensToGCode(GCode.File.Tokens, true); GCode.File.AddBlock(string.Format("Compression applied: {0}", GCode.File.Model.FileName), Core.Action.New); foreach (string block in gc) { GCode.File.AddBlock(block, Core.Action.Add); } GCode.File.AddBlock("", Core.Action.End); } }
public void Apply() { double arcTolerance = GrblSettings.GetDouble(GrblSetting.ArcTolerance); GCodeEmulator emu = new GCodeEmulator(); List <GCodeToken> toolPath = new List <GCodeToken>(); uint lnr = 0, lnroffset = 0; using (new UIUtils.WaitCursor()) { toolPath.Add(new GCComment(Commands.Comment, 0, "Arcs to lines transform applied")); foreach (var cmd in emu.Execute(GCode.File.Tokens)) { switch (cmd.Token.Command) { case Commands.G2: case Commands.G3: { var arc = cmd.Token as GCArc; lnroffset++; lnr = arc.LineNumber; toolPath.Add(new GCComment(Commands.Comment, arc.LineNumber + lnroffset, "Arc to lines start: " + arc.ToString())); List <Point3D> points = arc.GeneratePoints(emu.Plane, ToPos(cmd.Start, emu.IsImperial), arcTolerance, emu.DistanceMode == DistanceMode.Incremental); // Dynamic resolution foreach (Point3D point in points) { lnroffset++; toolPath.Add(new GCLinearMotion(Commands.G1, arc.LineNumber + lnroffset, ToPos(point, emu.IsImperial), AxisFlags.XYZ)); } lnroffset++; toolPath.Add(new GCComment(Commands.Comment, arc.LineNumber + lnroffset, "Arc to lines end")); } break; case Commands.G5: { var spline = cmd.Token as GCSpline; lnroffset++; lnr = spline.LineNumber; toolPath.Add(new GCComment(Commands.Comment, spline.LineNumber + lnroffset, "Spline to lines start: " + spline.ToString())); List <Point3D> points = spline.GeneratePoints(ToPos(cmd.Start, emu.IsImperial), arcTolerance, emu.DistanceMode == DistanceMode.Incremental); // Dynamic resolution foreach (Point3D point in points) { lnroffset++; toolPath.Add(new GCLinearMotion(Commands.G1, spline.LineNumber + lnroffset, ToPos(point, emu.IsImperial), AxisFlags.XYZ)); } lnroffset++; toolPath.Add(new GCComment(Commands.Comment, lnr, "Spline to lines end")); } break; default: cmd.Token.LineNumber += lnroffset; toolPath.Add(cmd.Token); lnr = cmd.Token.LineNumber; break; } } List <string> gc = GCodeParser.TokensToGCode(toolPath, AppConfig.Settings.Base.AutoCompress); GCode.File.AddBlock(string.Format("Arcs to lines transform applied: {0}", GCode.File.Model.FileName), Core.Action.New); foreach (string block in gc) { GCode.File.AddBlock(block, Core.Action.Add); } GCode.File.AddBlock("", Core.Action.End); } }
public void ApplyRotation(double angle, Vector3 offset, bool compress = false) { uint G53lnr = 0; int precision = GCode.File.Decimals; GCPlane plane = new GCPlane(GrblParserState.Plane == Plane.XY ? Commands.G17 : Commands.G18, 0); DistanceMode distanceMode = GrblParserState.DistanceMode; Vector3 pos = new Vector3(Grbl.GrblViewModel.Position.X, Grbl.GrblViewModel.Position.Y, Grbl.GrblViewModel.Position.Z); List <GCodeToken> toolPath = new List <GCodeToken>(); toolPath.Add(new GCComment(Commands.Comment, 0, string.Format("{0} degree rotation applied", Math.Round(angle * 180d / Math.PI, 1).ToInvariantString()))); foreach (var token in GCode.File.Tokens) { switch (token.Command) { case Commands.G0: case Commands.G1: { var motion = token as GCLinearMotion; if (motion.AxisFlags != AxisFlags.None && G53lnr != token.LineNumber) { var target = ToAbsolute(pos, motion.Values); if (distanceMode == DistanceMode.Incremental) { if (!motion.AxisFlags.HasFlag(AxisFlags.X)) { target = new Vector3(0d, target.Y, 0d); } if (!motion.AxisFlags.HasFlag(AxisFlags.Y)) { target = new Vector3(target.X, 0d, 0d); } target = target.RotateZ(0d, 0d, angle).Round(precision); } else { target = target.RotateZ(offset.X, offset.Y, angle).Round(precision); } if (target.X != pos.X) { motion.AxisFlags |= AxisFlags.X; } if (target.Y != pos.Y) { motion.AxisFlags |= AxisFlags.Y; } if (distanceMode == DistanceMode.Incremental) { pos += target; } else { pos = target; } toolPath.Add(new GCLinearMotion(motion.Command, motion.LineNumber, target.Array, motion.AxisFlags)); } else { G53lnr = 0; toolPath.Add(new GCLinearMotion(motion.Command, motion.LineNumber, motion.Values, motion.AxisFlags)); } } break; case Commands.G2: case Commands.G3: { if (plane.Plane != Plane.XY) { throw new Exception(LibStrings.FindResource("HasG17G18Arcs")); } if ((token as GCArc).IsRadiusMode) // for now... { throw new Exception(LibStrings.FindResource("HasRadiusArcs")); } var arc = token as GCArc; Vector3 target = ToAbsolute(pos, arc.Values).RotateZ(offset.X, offset.Y, angle).Round(precision); Vector3 targetijk = arc.IsRadiusMode ? new Vector3(double.NaN, double.NaN, double.NaN) : new Vector3(arc.IJKvalues).RotateZ(0d, 0d, angle).Round(precision); if (pos.X != target.X) { arc.AxisFlags |= AxisFlags.X; } if (pos.Y != target.Y) { arc.AxisFlags |= AxisFlags.Y; } pos = target; toolPath.Add(new GCArc(arc.Command, arc.LineNumber, pos.Array, arc.AxisFlags, targetijk.Array, arc.IjkFlags, arc.R, arc.IJKMode)); } break; case Commands.G5: { var spline = token as GCSpline; pos = new Vector3(spline.X, spline.Y, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision); var ij = new Vector3(spline.I, spline.J, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision); var pq = new Vector3(spline.P, spline.Q, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision); toolPath.Add(new GCSpline(spline.Command, spline.LineNumber, pos.Array, spline.AxisFlags, new double[] { ij.X, ij.Y, pq.X, pq.Y })); } break; case Commands.G17: case Commands.G18: case Commands.G19: plane = token as GCPlane; toolPath.Add(token); break; case Commands.G53: G53lnr = token.LineNumber; // No rotation for next linear move toolPath.Add(token); break; case Commands.G73: case Commands.G81: case Commands.G82: case Commands.G83: case Commands.G85: case Commands.G86: case Commands.G89: { var drill = token as GCCannedDrill; var target = ToAbsolute(pos, drill.Values).RotateZ(offset.X, offset.Y, angle); if (pos.X != target.X) { drill.AxisFlags |= AxisFlags.X; } if (pos.Y != target.Y) { drill.AxisFlags |= AxisFlags.Y; } if (distanceMode == DistanceMode.Incremental) { pos = new Vector3(pos.X + target.X * drill.L, pos.Y + target.Y * drill.L, pos.Z + target.Z); } else { pos = target; } toolPath.Add(new GCCannedDrill(drill.Command, drill.LineNumber, target.Round(precision).Array, drill.AxisFlags, drill.R, drill.L, drill.P, drill.Q)); } break; case Commands.G90: case Commands.G91: distanceMode = (token as GCDistanceMode).DistanceMode; toolPath.Add(token); break; default: toolPath.Add(token); break; } } List <string> gc = GCodeParser.TokensToGCode(toolPath, compress); // GCodeParser.Save(@"C:\Users\terjeio\Desktop\Probing\file.nc", gc); GCode.File.AddBlock(string.Format("{0} degree rotation applied: {1}", Math.Round(angle * 180d / Math.PI, 1).ToInvariantString(), Grbl.GrblViewModel.FileName), Core.Action.New); foreach (string block in gc) { GCode.File.AddBlock(block, Core.Action.Add); } GCode.File.AddBlock("", Core.Action.End); }
public void ApplyHeightMap(ProbingViewModel model) { HeightMap map = model.HeightMap.Map; double segmentLength = Math.Min(map.GridX, map.GridY); int precision = model.Grbl.Precision; GCPlane plane = new GCPlane(GrblParserState.Plane == Plane.XY ? Commands.G17 : Commands.G18, 0); DistanceMode distanceMode = GrblParserState.DistanceMode; Vector3 pos = new Vector3(model.Grbl.Position.X, model.Grbl.Position.Y, model.Grbl.Position.Z); List <GCodeToken> newToolPath = new List <GCodeToken>(); uint lnr = 1; foreach (var token in GCode.File.Tokens) { switch (token.Command) { case Commands.G0: case Commands.G1: { var motion = token as GCLinearMotion; var m = new Line(motion.AxisFlags); m.Start = pos; m.End = pos = ToAbsolute(pos, motion.Values, distanceMode == DistanceMode.Incremental); m.Rapid = token.Command == Commands.G0; foreach (Motion subMotion in m.Split(segmentLength)) { Vector3 target = new Vector3(Math.Round(subMotion.End.X, precision), Math.Round(subMotion.End.Y, precision), Math.Round(subMotion.End.Z + map.InterpolateZ(subMotion.End.X, subMotion.End.Y), precision)); newToolPath.Add(new GCLinearMotion(motion.Command, lnr++, target.Array, motion.AxisFlags | AxisFlags.Z)); } } break; case Commands.G2: case Commands.G3: { if (plane.Plane != Plane.XY) { throw new Exception(LibStrings.FindResource("HasRadiusArcs")); } var arc = token as GCArc; double[] center = arc.GetCenter(plane, pos.Array); double[] ijk = new double[3]; Array.Copy(arc.IJKvalues, ijk, 3); var m = new Arc(); m.Start = pos; m.End = pos = ToAbsolute(pos, arc.Values, distanceMode == DistanceMode.Incremental); m.Direction = token.Command == Commands.G2 ? ArcDirection.CW : ArcDirection.CCW; m.U = center[0]; m.V = center[1]; m.Plane = ArcPlane.XY; foreach (Motion subMotion in m.Split(segmentLength)) { if (!arc.IsRadiusMode) { ijk[0] = Math.Round(center[0] - subMotion.Start.X, precision); ijk[1] = Math.Round(center[1] - subMotion.Start.Y, precision); } Vector3 target = new Vector3(Math.Round(subMotion.End.X, precision), Math.Round(subMotion.End.Y, precision), Math.Round(subMotion.End.Z + map.InterpolateZ(subMotion.End.X, subMotion.End.Y), precision)); newToolPath.Add(new GCArc(arc.Command, lnr++, target.Array, arc.AxisFlags | AxisFlags.Z, ijk, arc.IjkFlags, arc.R, arc.IJKMode)); } } break; case Commands.G17: case Commands.G18: case Commands.G19: plane = token as GCPlane; newToolPath.Add(token); break; case Commands.G90: case Commands.G91: distanceMode = (token as GCDistanceMode).DistanceMode; newToolPath.Add(token); break; default: newToolPath.Add(token); break; } } List <string> gc = GCodeParser.TokensToGCode(newToolPath, AppConfig.Settings.Base.AutoCompress); // GCodeParser.Save(@"C:\Users\terjeio\Desktop\Probing\file.nc", gc); GCode.File.AddBlock(string.Format("Heightmap applied: {0}", model.Grbl.FileName), Core.Action.New); foreach (string block in gc) { GCode.File.AddBlock(block, Core.Action.Add); } GCode.File.AddBlock("", Core.Action.End); model.HeightMapApplied = true; }
public void Apply() { if (new DragKnifeDialog(this) { Owner = Application.Current.MainWindow }.ShowDialog() != true) { return; } using (new UIUtils.WaitCursor()) { GCodeEmulator emu = new GCodeEmulator(); // emu.SetStartPosition(Machine.StartPosition); List <segment> polyLine = new List <segment>(); List <GCodeToken> newToolPath = new List <GCodeToken>(); newToolPath.Add(new GCComment(Commands.Comment, 0, "Drag knife transform applied")); StartDirection = new Vector3(1d, 0d, 0d); foreach (var cmd in emu.Execute(GCode.File.Tokens)) { switch (cmd.Token.Command) { case Commands.G0: if (polyLine.Count > 0) { polyLine[polyLine.Count - 1].Last = true; Transform(polyLine, newToolPath); polyLine.Clear(); } newToolPath.Add(cmd.Token); break; case Commands.G1: segment s = new segment(); s.P1 = new Vector3(cmd.Start.X, cmd.Start.Y, 0d); s.P2 = new Vector3(cmd.End.X, cmd.End.Y, 0d); if (!s.P1.Equals(s.P2)) { polyLine.Add(s); } break; default: newToolPath.Add(cmd.Token); break; } } List <string> gc = GCodeParser.TokensToGCode(newToolPath, AppConfig.Settings.Base.AutoCompress); // GCodeParser.Save(@"C:\Users\terjeio\Desktop\Probing\knife.nc", gc); GCode.File.AddBlock(string.Format("Drag knife transform applied: {0}", GCode.File.Model.FileName), CNC.Core.Action.New); foreach (string block in gc) { GCode.File.AddBlock(block, CNC.Core.Action.Add); } GCode.File.AddBlock("", CNC.Core.Action.End); } }