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 Render(List <GCodeToken> tokens) { var bbox = (DataContext as GrblViewModel).ProgramLimits; double lineThickness = bbox.MaxSize / 1000d; double arrowOffset = lineThickness * 30d; double labelOffset = lineThickness * 50d; ClearViewport(); if (isLatheMode == null) { if ((isLatheMode = model.LatheMode != LatheMode.Disabled) == true) { viewport.ModelUpDirection = new Vector3D(0d, -1d, 0d); if (tool != null) { tool.Normal = new Vector3D(1d, 0d, 0d); } } isDiameterMode = model.LatheMode == LatheMode.Diameter; } bool latheMode = isLatheMode == true; cutCount = 0; point0 = Machine.StartPosition; lastType = MoveType.None; if (tool != null) { tool.Height = Math.Min(6d, lineThickness * 100d); tool.TopRadius = tool.Height / 5d; } #region Canvas adorners if (ShowGrid) { double wh, h, wm = bbox.SizeX % TickSize, w = Math.Ceiling(bbox.SizeX - bbox.SizeX % TickSize + TickSize * 2d); if (model.LatheMode == LatheMode.Disabled) { wh = bbox.SizeY % TickSize; h = Math.Ceiling(bbox.SizeY - bbox.SizeY % TickSize + TickSize * 2d); Machine.Grid = new GridLinesVisual3D() { Center = new Point3D(boffset(bbox.SizeX, bbox.MinX, w, wm) - TickSize, boffset(bbox.SizeY, bbox.MinY, h, wh) - TickSize, 0d), MinorDistance = 2.5d, MajorDistance = TickSize, Width = h, Length = w, Thickness = 0.1d, Fill = AxisBrush }; } else { wh = bbox.SizeZ % TickSize; h = Math.Ceiling(bbox.SizeZ - bbox.SizeZ % TickSize + TickSize * 2d); Machine.Grid = new GridLinesVisual3D() { Center = new Point3D(boffset(bbox.SizeX, bbox.MinX, w, wm) - TickSize, 0d, boffset(bbox.SizeZ, bbox.MinZ, h, wh) - TickSize), MinorDistance = 2.5d, MajorDistance = TickSize, Width = w, Length = h, Thickness = lineThickness, Fill = AxisBrush, LengthDirection = new Vector3D(0d, 0d, 1d), Normal = new Vector3D(0d, 1d, 0d) }; } viewport.Children.Add(Machine.Grid); } if (ShowAxes) { Machine.Axes.Children.Add(new ArrowVisual3D() { Point2 = new Point3D(bbox.SizeX + arrowOffset, 0d, 0d), Diameter = lineThickness * 5, Fill = AxisBrush }); Machine.Axes.Children.Add(new BillboardTextVisual3D() { Text = "X", FontWeight = FontWeights.Bold, Foreground = AxisBrush, Position = new Point3D(bbox.SizeX + labelOffset, 0d, 0d) }); if (bbox.SizeY > 0d) { Machine.Axes.Children.Add(new ArrowVisual3D() { Point2 = new Point3D(0d, bbox.SizeY + arrowOffset, 0d), Diameter = lineThickness * 5d, Fill = AxisBrush }); Machine.Axes.Children.Add(new BillboardTextVisual3D() { Text = "Y", FontWeight = FontWeights.Bold, Foreground = AxisBrush, Position = new Point3D(0d, bbox.SizeY + labelOffset, 0d) }); } if (bbox.SizeZ > 0d) { Machine.Axes.Children.Add(new ArrowVisual3D() { Point1 = latheMode ? new Point3D(0d, 0d, bbox.MaxZ + arrowOffset) : new Point3D(0d, 0d, bbox.MinZ - arrowOffset), Point2 = latheMode ? new Point3D(0d, 0d, bbox.MinZ - arrowOffset) : new Point3D(0d, 0d, bbox.MaxZ + arrowOffset), Diameter = lineThickness * 5d, Fill = AxisBrush, }); Machine.Axes.Children.Add(new BillboardTextVisual3D() { Text = "Z", FontWeight = FontWeights.Bold, Foreground = AxisBrush, Position = new Point3D(0d, 0d, latheMode ? bbox.MinZ - labelOffset : bbox.MaxZ + labelOffset) }); } viewport.Children.Add(Machine.Axes); } if (ShowBoundingBox && bbox.SizeZ > 0d) { Machine.BoundingBox = new BoundingBoxWireFrameVisual3D() { BoundingBox = new Rect3D(bbox.MinX, bbox.MinY, bbox.MinZ, bbox.SizeX, Math.Max(0.001d, bbox.SizeY), bbox.SizeZ), Thickness = 1d, Color = Colors.LightGreen }; viewport.Children.Add(Machine.BoundingBox); } #endregion GCodeEmulator emu = new GCodeEmulator(true); emu.SetStartPosition(Machine.StartPosition); foreach (var cmd in emu.Execute(tokens)) { point0 = cmd.Start; switch (cmd.Token.Command) { case Commands.G0: if (cmd.IsRetract) { AddRetractMove(cmd.End); } else { AddRapidMove(cmd.End); } break; case Commands.G1: AddCutMove(cmd.End); break; case Commands.G2: case Commands.G3: #if DEBUG_ARC_BBOXES var bb = (cmd.Token as GCArc).GetBoundingBox(emu.Plane, point0.ToArray(), emu.DistanceMode == DistanceMode.Incremental); var abb = new BoundingBoxWireFrameVisual3D() { BoundingBox = new Rect3D(bb.Min[0], bb.Min[1], bb.Min[2], bb.Size[0], bb.Size[1], bb.Size[2]), Thickness = .5d, Color = Colors.Blue }; viewport.Children.Add(abb); #endif DrawArc(cmd.Token as GCArc, point0.ToArray(), emu.Plane, emu.DistanceMode == DistanceMode.Incremental); break; case Commands.G5: DrawSpline(cmd.Token as GCSpline, point0.ToArray()); break; } } Machine.RapidLines = rapidPoints; Machine.CutLines = linePoints; Machine.RetractLines = retractPoints; refreshCamera(bbox); }
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); } }