public void DrawOnGraphics(Graphics g, Size size) { if (!mRange.MovingRange.ValidRange) { return; } GrblCommand.StatePositionBuilder spb = new GrblCommand.StatePositionBuilder(); ProgramRange.XYRange scaleRange = mRange.MovingRange; //Get scale factors for both directions. To preserve the aspect ratio, use the smaller scale factor. float zoom = scaleRange.Width > 0 && scaleRange.Height > 0 ? Math.Min((float)size.Width / (float)scaleRange.Width, (float)size.Height / (float)scaleRange.Height) * 0.95f : 1; ScaleAndPosition(g, size, scaleRange, zoom); DrawJobPreview(g, spb, zoom); DrawJobRange(g, size, zoom); }
private void ScaleAndPosition(Graphics g, Size s, ProgramRange.XYRange scaleRange, float zoom) { g.ResetTransform(); float margin = 10; CartesianQuadrant q = Quadrant; if (q == CartesianQuadrant.Unknown || q == CartesianQuadrant.I) { //Scale and invert Y g.ScaleTransform(zoom, -zoom, MatrixOrder.Append); //Translate to position bottom-left g.TranslateTransform(margin, s.Height - margin, MatrixOrder.Append); } else if (q == CartesianQuadrant.II) { //Scale and invert Y g.ScaleTransform(zoom, -zoom, MatrixOrder.Append); //Translate to position bottom-left g.TranslateTransform(s.Width - margin, s.Height - margin, MatrixOrder.Append); } else if (q == CartesianQuadrant.III) { //Scale and invert Y g.ScaleTransform(zoom, -zoom, MatrixOrder.Append); //Translate to position bottom-left g.TranslateTransform(s.Width - margin, margin, MatrixOrder.Append); } else if (q == CartesianQuadrant.IV) { //Scale and invert Y g.ScaleTransform(zoom, -zoom, MatrixOrder.Append); //Translate to position bottom-left g.TranslateTransform(margin, margin, MatrixOrder.Append); } else { //Translate to center of gravity of the image g.TranslateTransform(-scaleRange.Center.X, -scaleRange.Center.Y, MatrixOrder.Append); //Scale and invert Y g.ScaleTransform(zoom, -zoom, MatrixOrder.Append); //Translate to center over the drawing area. g.TranslateTransform(s.Width / 2, s.Height / 2, MatrixOrder.Append); } }
internal void DrawOnGraphics(Graphics g, Size s) { if (!mRange.MovingRange.ValidRange) { return; } GrblCommand.StatePositionBuilder spb = new GrblCommand.StatePositionBuilder(); ProgramRange.XYRange scaleRange = mRange.MovingRange; //Get scale factors for both directions. To preserve the aspect ratio, use the smaller scale factor. float zoom = scaleRange.Width > 0 && scaleRange.Height > 0 ? Math.Min((float)s.Width / (float)scaleRange.Width, (float)s.Height / (float)scaleRange.Height) * 0.95f : 1; bool firstline = true; //used to draw the first line in a different color ScaleAndPosition(g, s, scaleRange, zoom); foreach (GrblCommand cmd in list) { try { cmd.BuildHelper(); spb.AnalyzeCommand(cmd, false); if (spb.TrueMovement()) { Color linecolor = Color.FromArgb(spb.GetCurrentAlpha(mRange.SpindleRange), firstline ? ColorScheme.PreviewFirstMovement : spb.LaserBurning ? ColorScheme.PreviewLaserPower : ColorScheme.PreviewOtherMovement); using (Pen pen = GetPen(linecolor)) { pen.ScaleTransform(1 / zoom, 1 / zoom); if (!spb.LaserBurning) { pen.DashStyle = DashStyle.Dash; pen.DashPattern = new float[] { 1f, 1f }; } if (spb.G0G1 && cmd.IsLinearMovement && pen.Color.A > 0) { g.DrawLine(pen, new PointF((float)spb.X.Previous, (float)spb.Y.Previous), new PointF((float)spb.X.Number, (float)spb.Y.Number)); } else if (spb.G2G3 && cmd.IsArcMovement && pen.Color.A > 0) { PointF center = cmd.GetCenter((float)spb.X.Previous, (float)spb.Y.Previous); double cX = center.X; double cY = center.Y; double aX = (double)spb.X.Previous; double aY = (double)spb.Y.Previous; double bX = (double)spb.X.Number; double bY = (double)spb.Y.Number; double ray = cmd.GetArcRadius(); double rectX = cX - ray; double rectY = cY - ray; double rectW = 2 * ray; double rectH = 2 * ray; double aA = Tools.MathHelper.CalculateAngle(cX, cY, aX, aY); //180/Math.PI*Math.Atan2(y1-y0, x1-x0); double bA = Tools.MathHelper.CalculateAngle(cX, cY, bX, bY); //180/Math.PI*Math.Atan2(y2-y0, x2-x0); double sA = aA; //start angle double wA = Tools.MathHelper.AngularDistance(aA, bA, spb.G2); if (rectW > 0 && rectH > 0) { try { g.DrawArc(pen, (float)rectX, (float)rectY, (float)rectW, (float)rectH, (float)sA, (float)wA); } catch { System.Diagnostics.Debug.WriteLine(String.Format("Ex drwing arc: W{0} H{1}", rectW, rectH)); } } } } firstline = false; } } catch (Exception ex) { throw ex; } finally { cmd.DeleteHelper(); } } DrawJobRange(g, s, zoom); }
internal void DrawOnGraphics(Graphics g, Size s) { if (!mRange.MovingRange.ValidRange) { return; } GrblCommand.StatePositionBuilder spb = new GrblCommand.StatePositionBuilder(); ProgramRange.XYRange scaleRange = mRange.MovingRange; //Get scale factors for both directions. To preserve the aspect ratio, use the smaller scale factor. float zoom = scaleRange.Width > 0 && scaleRange.Height > 0 ? Math.Min((float)s.Width / (float)scaleRange.Width, (float)s.Height / (float)scaleRange.Height) * 0.95f : 1; bool firstline = true; //used to draw the first line in a different color ScaleAndPosition(g, s, scaleRange, zoom); foreach (GrblCommand cmd in list) { try { cmd.BuildHelper(); spb.AnalyzeCommand(cmd, false); if (spb.TrueMovement()) { Color linecolor = Color.FromArgb(spb.GetCurrentAlpha(mRange.SpindleRange), firstline ? ColorScheme.PreviewFirstMovement : spb.LaserBurning ? ColorScheme.PreviewLaserPower : ColorScheme.PreviewOtherMovement); using (Pen pen = GetPen(linecolor)) { pen.ScaleTransform(1 / zoom, 1 / zoom); if (!spb.LaserBurning) { pen.DashStyle = DashStyle.Dash; pen.DashPattern = new float[] { 1f, 1f }; } if (spb.G0G1 && cmd.IsLinearMovement && pen.Color.A > 0) { g.DrawLine(pen, new PointF((float)spb.X.Previous, (float)spb.Y.Previous), new PointF((float)spb.X.Number, (float)spb.Y.Number)); } else if (spb.G2G3 && cmd.IsArcMovement && pen.Color.A > 0) { GrblCommand.G2G3Helper ah = spb.GetArcHelper(cmd); if (ah.RectW > 0 && ah.RectH > 0) { try { g.DrawArc(pen, (float)ah.RectX, (float)ah.RectY, (float)ah.RectW, (float)ah.RectH, (float)(ah.StartAngle * 180 / Math.PI), (float)(ah.AngularWidth * 180 / Math.PI)); } catch { System.Diagnostics.Debug.WriteLine(String.Format("Ex drwing arc: W{0} H{1}", ah.RectW, ah.RectH)); } } } } firstline = false; } } catch (Exception ex) { throw ex; } finally { cmd.DeleteHelper(); } } DrawJobRange(g, s, zoom); }
private void Process(Graphics g, Size s) { bool supportPWM = (bool)Settings.GetObject("Support Hardware PWM", true); bool laserMode = (bool)Settings.GetObject("Laser Mode", false); Boolean analyze = (g == null); Boolean drawing = (g != null); if (drawing && !mRange.MovingRange.ValidRange) { return; } ProgramRange.XYRange scaleRange = mRange.MovingRange; //Get scale factors for both directions. To preserve the aspect ratio, use the smaller scale factor. float zoom = drawing && scaleRange.Width > 0 && scaleRange.Height > 0 ? Math.Min((float)s.Width / (float)scaleRange.Width, (float)s.Height / (float)scaleRange.Height) * 0.95f : 1; bool firstline = true; bool isLaserCutting = false; bool isLaserActive = false; decimal curX = 0; decimal curY = 0; decimal speed = 0; int curAlpha = 0; bool cw = false; //cw-ccw memo bool abs = false; //absolute-relative memo if (analyze) { mRange.ResetRange(); mRange.UpdateXYRange(0, 0, false); mTotalTravelOn = 0; mTotalTravelOff = 0; mEstimatedTimeOn = TimeSpan.Zero; mEstimatedTimeOff = TimeSpan.Zero; } if (drawing) { ScaleAndPosition(g, s, scaleRange, zoom); } if (drawing && !mRange.SpindleRange.ValidRange) //assign max alpha if no S range available { curAlpha = 255; } foreach (GrblCommand cmd in list) { try { cmd.BuildHelper(); TimeSpan delay = TimeSpan.Zero; if (cmd.IsLaserON) { isLaserActive = true; isLaserCutting = true; } else if (cmd.IsLaserOFF) { isLaserActive = false; isLaserCutting = false; } if (laserMode == true && isLaserActive == true) { if (cmd.IsRapidMovement == true) { isLaserCutting = false; } else { isLaserCutting = true; } } if (cmd.IsRelativeCoord) { abs = false; } if (cmd.IsAbsoluteCoord) { abs = true; } if (cmd.F != null) { speed = cmd.F.Number; } if (drawing && cmd.S != null) { if (mRange.SpindleRange.ValidRange) { curAlpha = (int)((cmd.S.Number - mRange.SpindleRange.S.Min) * 255 / (mRange.SpindleRange.S.Max - mRange.SpindleRange.S.Min)); } else { curAlpha = 255; } } if (analyze && cmd.S != null) { mRange.UpdateSRange(cmd.S.Number); } if (cmd.IsMovement && cmd.TrueMovement(curX, curY, abs)) { decimal newX = cmd.X != null ? (abs ? cmd.X.Number : curX + cmd.X.Number) : curX; decimal newY = cmd.Y != null ? (abs ? cmd.Y.Number : curY + cmd.Y.Number) : curY; if (analyze) { mRange.UpdateXYRange(newX, newY, isLaserCutting); decimal distance = 0; if (cmd.IsLinearMovement) { distance = Tools.MathHelper.LinearDistance(curX, curY, newX, newY); } else if (cmd.IsArcMovement) //arc of given radius { distance = Tools.MathHelper.ArcDistance(curX, curY, newX, newY, cmd.GetArcRadius()); } if (isLaserCutting) { mTotalTravelOn += distance; } else { mTotalTravelOff += distance; } if (distance != 0 && speed != 0) { delay = TimeSpan.FromMinutes((double)distance / (double)speed); } } if (drawing) { Color linecolor = firstline ? ColorScheme.PreviewFirstMovement : isLaserCutting ? ColorScheme.PreviewLaserPower : ColorScheme.PreviewOtherMovement; using (Pen pen = GetPen(linecolor)) { pen.ScaleTransform(1 / zoom, 1 / zoom); if (isLaserCutting) { pen.Color = Color.FromArgb(curAlpha, pen.Color); } if (!isLaserCutting) { if (supportPWM) { pen.Color = Color.FromArgb(150, pen.Color); } else { pen.Color = Color.FromArgb(50, pen.Color); } pen.DashStyle = DashStyle.Dash; pen.DashPattern = new float[] { 1f, 1f }; } if (cmd.IsLinearMovement) { g.DrawLine(pen, new PointF((float)curX, (float)curY), new PointF((float)newX, (float)newY)); } else if (cmd.IsArcMovement) { cw = cmd.IsCW(cw); PointF center = cmd.GetCenter((float)curX, (float)curY); double cX = center.X; double cY = center.Y; double aX = (double)curX; double aY = (double)curY; double bX = (double)newX; double bY = (double)newY; double ray = cmd.GetArcRadius(); double rectX = cX - ray; double rectY = cY - ray; double rectW = 2 * ray; double rectH = 2 * ray; double aA = Tools.MathHelper.CalculateAngle(cX, cY, aX, aY); //180/Math.PI*Math.Atan2(y1-y0, x1-x0); double bA = Tools.MathHelper.CalculateAngle(cX, cY, bX, bY); //180/Math.PI*Math.Atan2(y2-y0, x2-x0); double sA = aA; //start angle double wA = Tools.MathHelper.AngularDistance(aA, bA, cw); if (rectW > 0 && rectH > 0) { try { g.DrawArc(pen, (float)rectX, (float)rectY, (float)rectW, (float)rectH, (float)sA, (float)wA); } catch { System.Diagnostics.Debug.WriteLine(String.Format("Ex drwing arc: W{0} H{1}", rectW, rectH)); } } } } firstline = false; } curX = newX; curY = newY; } else if (cmd.IsPause) { if (analyze) { //TimeSpan delay = cmd.P != null ? TimeSpan.FromMilliseconds((double)cmd.P.Number) : cmd.S != null ? TimeSpan.FromSeconds((double)cmd.S.Number) : TimeSpan.Zero; //grbl seem to use both P and S as number of seconds delay = cmd.P != null?TimeSpan.FromSeconds((double)cmd.P.Number) : cmd.S != null?TimeSpan.FromSeconds((double)cmd.S.Number) : TimeSpan.Zero; } } if (isLaserCutting) { mEstimatedTimeOn += delay; } else { mEstimatedTimeOff += delay; } if (analyze) { cmd.SetOffset(mTotalTravelOn + mTotalTravelOff, mEstimatedTimeOn + mEstimatedTimeOff); } } catch (Exception ex) { throw ex; } finally { cmd.DeleteHelper(); } } if (drawing) { DrawJobRange(g, s, zoom); } }