Skip to content

Commit

Permalink
Version 1.7.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
svenhb authored Aug 18, 2024
1 parent 120f25e commit 5762e22
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 251 deletions.
52 changes: 33 additions & 19 deletions GRBL-Plotter/GCodeCreation/Graphic2GCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ You should have received a copy of the GNU General Public License
* 2024-01-07 l:393 f:CreateGCode if (useToolTable) also adapt width/diameter issue #370
* 2024-03-19 l:970 f:MoveToDashed avoid intermediate G0 coordinates
* 2024-04-13 l:438 f:ProcessPathLength new function
* 2024-06-22 l:1116 f:arc tangential
*/

using System;
Expand Down Expand Up @@ -94,7 +95,9 @@ public static class Graphic2GCode
private static double PathDashArrayDistance = 0;
private static bool PathDashArrayPenIsUp = false;

private static double GlobalPathLength = 0;
private static double GlobalPathLengthPD = 0;
private static double GlobalPathLengthPU = 0;
private static Point GlobalPathStart = new Point();

private static double setAux1FinalDistance = 0; // sum-up path distances 396
private static double setAux2FinalDistance = 0;
Expand All @@ -112,10 +115,8 @@ public static class Graphic2GCode

public static void CleanUp()
{
// Logger.Trace("CleanUp()");
gcodeString.Clear();
gcodeString.Length = 0;
finalGcodeString.Clear();
gcodeString?.Clear();
finalGcodeString?.Clear();
}

public static void Init()
Expand All @@ -138,7 +139,10 @@ public static void Init()
finalGcodeString.Clear();

PathCount = 0;
GlobalPathLength = 0;
GlobalPathLengthPD = 0;
GlobalPathLengthPU = 0;
GlobalPathStart = Graphic.GetStartPos();


FigureEndTagWasSet = true;
Gcode.Setup(true); // convertGraphics=true (repeat, inser sub) // initialize GCode creation (get stored settings for export)
Expand Down Expand Up @@ -211,7 +215,7 @@ internal static bool CreateGCode(List<Graphic.TileObject> tiledGraphic, List<str
if (multiImport) { Gcode.Comment(finalGcodeString, string.Format("{0}>", XmlMarker.CollectionEnd)); }

Gcode.JobEnd(finalGcodeString, "EndJob"); // Spindle / laser off
ProcessPathLength(GlobalPathLength);
ProcessPathLength(GlobalPathLengthPD, GlobalPathLengthPU);

return FinalGCode(graphicInfo.Title, graphicInfo.FilePath);
}
Expand Down Expand Up @@ -320,7 +324,7 @@ internal static bool CreateGCode(List<Graphic.GroupObject> completeGraphic, List
if (!useTiles)
{
Gcode.JobEnd(finalGcodeString, "EndJob"); // Spindle / laser off
ProcessPathLength(GlobalPathLength);
ProcessPathLength(GlobalPathLengthPD, GlobalPathLengthPU);
return FinalGCode(graphicInfo.Title, graphicInfo.FilePath);
}
else
Expand Down Expand Up @@ -424,7 +428,7 @@ internal static bool CreateGCode(List<Graphic.PathObject> completeGraphic, List<
finalGcodeString.Append(gcodeString);
if (multiImport) { Gcode.Comment(finalGcodeString, string.Format("{0}>", XmlMarker.CollectionEnd)); }
Gcode.JobEnd(finalGcodeString, "EndJob"); // Spindle / laser off
ProcessPathLength(GlobalPathLength);
ProcessPathLength(GlobalPathLengthPD, GlobalPathLengthPU);
return FinalGCode(graphicInfo.Title, graphicInfo.FilePath);
}
else
Expand All @@ -435,11 +439,12 @@ internal static bool CreateGCode(List<Graphic.PathObject> completeGraphic, List<
}
}

private static void ProcessPathLength(double val)
private static void ProcessPathLength(double valPD, double valPU)
{
//SetComment(string.Format("Path length: {0:0.00}", val));
Gcode.gcodeDistance=val;
Logger.Info("Path length: {0:0.00}",val);
Gcode.gcodeDistancePD=valPD;
Gcode.gcodeDistancePU = valPU;
Logger.Info("Path length PD:{0:0.00} PU:{0:0.00}", valPD, valPU);
}

//convert graphic to gcode ##################################################################
Expand All @@ -453,7 +458,7 @@ private static void ProcessPathObject(PathObject pathObject, Graphic.GraphicInfo

useAlternitveZ = Properties.Settings.Default.importDepthFromWidthRamp || graphicInfo.DxfImportZ;

GlobalPathLength += pathObject.PathLength;
GlobalPathLengthPD += pathObject.PathLength;


/* Create Dot */
Expand Down Expand Up @@ -495,6 +500,7 @@ private static void ProcessPathObject(PathObject pathObject, Graphic.GraphicInfo
pathObject.FigureId = StartPath(DotData, toolNr, toolCmt, "PD");
PenDown("PD");
StopPath("PU DOT");
GlobalPathStart = DotData.Start;

if (Properties.Settings.Default.importSVGCircleToDot && (Properties.Settings.Default.importCircleToDotScriptCount > 0) && (dotCounter >= Properties.Settings.Default.importCircleToDotScriptCount))
{
Expand Down Expand Up @@ -583,15 +589,18 @@ private static void ProcessPathObject(PathObject pathObject, Graphic.GraphicInfo
/* Create Line */
if (entity is GCodeLine)
{
// Logger.Trace("# MoveTo {0:0.0} Angle:{1:0.0}", entity.MoveTo, (entity.Angle * 180 / Math.PI));
MoveTo(entity.MoveTo, newZ, newS, entity.Angle, "");
}
else if (entity is GCodeArc ArcData)
{
/* Create Arc */
// Logger.Trace("# ArcTo {0:0.0} AngleStart:{1:0.0} Angle:{2:0.0}", ArcData.MoveTo, ArcData.AngleStart * 180 / Math.PI, ArcData.Angle * 180 / Math.PI);
Arc(ArcData.IsCW, ArcData.MoveTo, ArcData.CenterIJ, ArcData.AngleStart, ArcData.Angle);//, "");// entity.comment);
}
}
StopPath("PU");
GlobalPathStart = PathData.End;
if (graphicInfo.OptionSpecialWireBend)
{
InsertCode(Properties.Settings.Default.importGraphicWireBenderCodeCut);
Expand Down Expand Up @@ -787,6 +796,10 @@ private static StringBuilder GetFigureAttributes(PathObject pathObject)
private static bool overWriteId = false;
private static int StartPath(PathObject pathObject, int toolNr, string toolCmt, string penCmt = "")//string cmt)
{
double lengthPU = GcodeMath.DistancePointToPoint(GlobalPathStart, pathObject.Start);
// Logger.Trace("StartPath Length:{0:0.0} from:{1:0.0} to:{2:0.0}", lengthPU, GlobalPathStart, pathObject.Start);
GlobalPathLengthPU += lengthPU;

Point startRamp = pathObject.Start;
Point startPenDown = new Point(startRamp.X, startRamp.Y);
double angle = pathObject.StartAngle;
Expand Down Expand Up @@ -906,7 +919,7 @@ private static int StartPath(PathObject pathObject, int toolNr, string toolCmt,
Gcode.MoveToRapid(gcodeString, startRamp, "");
PenDown(penCmt);
}
if (logCoordinates) Logger.Trace(" StartPath at x{0:0.000} y{1:0.000} a={2:0.00}", startRamp.X, startRamp.Y, setangle);
if (logCoordinates) Logger.Trace(" StartPath G0 at x{0:0.000} y{1:0.000} a={2:0.00}", startRamp.X, startRamp.Y, setangle);

if (Properties.Settings.Default.importGraphicLeadInEnable) // importGraphicLeadOutEnable)
{
Expand Down Expand Up @@ -965,7 +978,7 @@ private static void MoveTo(Point coordxy, double newZ, double? newS, double tang
PenDown(cmt); // + " moveto" // also process tangetial axis
double setangle = 180 * tangAngle / Math.PI;

if (logCoordinates) Logger.Trace(" MoveTo X{0:0.000} Y{1:0.000} A{2:0.00} useAlternitveZ:{3} applyDashPattern:{4}", coordxy.X, coordxy.Y, setangle, useAlternitveZ, applyDashPattern);
if (logCoordinates) Logger.Trace(" MoveTo G1 X{0:0.000} Y{1:0.000} A{2:0.00} useAlternitveZ:{3} applyDashPattern:{4}", coordxy.X, coordxy.Y, setangle, useAlternitveZ, applyDashPattern);
Gcode.SetTangential(gcodeString, setangle, true);
Gcode.SetAux1DistanceCommand(setAux1FinalDistance); // Create command-snipped, to be added in G1 command (Graphic2GCodeMove.cs - Move)
Gcode.SetAux2DistanceCommand(setAux2FinalDistance);
Expand Down Expand Up @@ -1085,24 +1098,25 @@ private static void Arc(int gnr, double x, double y, double i, double j, double
Point coordxy = new Point(x, y);
Point center = new Point(lastGC.X + i, lastGC.Y + j);
double offset = +Math.PI / 2;
if (logCoordinates) Logger.Trace(" Start Arc G{0} X{1:0.000} Y{2:0.000} angle:{3:0.000} cx{4:0.000} cy{5:0.000} angle:{6:0.000}", gnr, x, y, (180 * tangStartRad / Math.PI), center.X, center.Y, (180 * tangEndRad / Math.PI));
if (logCoordinates) Logger.Trace(" ArcTo G{0} X{1:0.000} Y{2:0.000} aStart:{3:0.000} cx{4:0.000} cy{5:0.000} aEnd:{6:0.000}", gnr, x, y, (180 * tangStartRad / Math.PI), center.X, center.Y, (180 * tangEndRad / Math.PI));
if (gnr > 2) { offset = -offset; }

Gcode.SetTangential(gcodeString, 180 * tangStartRad / Math.PI, true);
Gcode.SetAux1DistanceCommand(setAux1FinalDistance);
Gcode.SetAux2DistanceCommand(setAux2FinalDistance);

if (logCoordinates) Logger.Trace(" Start Arc alpha{0:0.000} offset{1:0.000} ", 180 * tangStartRad / Math.PI, 180 * offset / Math.PI);
if (logCoordinates) Logger.Trace(" Start Arc alpha{0:0.000} offset{1:0.000} ", 180 * tangStartRad / Math.PI, 180 * offset / Math.PI);

PenDown(cmt + " from Arc");

if (GcodeMath.IsEqual(coordxy, lastGC)) // end = start position? Full circle!
{
if (gnr > 2)
tangEndRad += 2 * Math.PI + tangStartRad; // CW 360°
tangEndRad += 2 * Math.PI;// + tangStartRad; // CW 360°
else
tangEndRad -= 2 * Math.PI + tangStartRad; // CCW 360°
tangEndRad -= 2 * Math.PI;// + tangStartRad; // CCW 360°
}
if (logCoordinates) Logger.Trace(" End Arc alpha{0:0.000} offset{1:0.000} ", 180 * tangEndRad / Math.PI, 180 * offset / Math.PI);
Gcode.SetTangential(gcodeString, 180 * tangEndRad / Math.PI, false);
Gcode.Arc(gcodeString, gnr, x, y, i, j, cmt);

Expand Down
48 changes: 29 additions & 19 deletions GRBL-Plotter/GCodeCreation/Graphic2GCodeRelated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ You should have received a copy of the GNU General Public License
* 2024-04-13 l:1815 f:SetHeaderInfo add output of path length and new format for process time
* 2024-05-06 l:1817 f:GetHeader avoid timespan overflow
* 2024-05-07 l:643 f:Setup check GcodeSummary.MetadataUse instead of Properties.Settings.Default.importSVGMetaData)
* 2024-05-28 l:699 f:Setup gcodeAngleStep set min to 0.01
* 2024-07-08 l:2004 f:IntermediateZ - Z-Up at least on final pass
*/

using System;
Expand Down Expand Up @@ -506,7 +508,8 @@ public static partial class Gcode

private static int gcodeLines = 0; // counter for GCode lines
private static int gcodeFigureLines = 0; // counter for GCode lines
internal static double gcodeDistance = 0; // counter for GCode move distance
internal static double gcodeDistancePD = 0; // counter for GCode move distance
internal static double gcodeDistancePU = 0; // counter for GCode move distance
private static float gcodeFigureDistance = 0; // counter for GCode move distance

private static int gcodeSubroutineEnable = 0; // state subroutine
Expand Down Expand Up @@ -561,7 +564,7 @@ public static partial class Gcode
private static bool gcodeCompress = false; // reduce code by avoiding sending again same G-Nr and unchanged coordinates
public static bool GcodeRelative { get; set; } //= false; // calculate relative coordinates for G91
private static bool gcodeNoArcs = false; // replace arcs by line segments
private static float gcodeAngleStep = 0.1f;
private static double gcodeAngleStep = 0.1;
private static bool gcodeInsertSubroutineEnable = false;
private static bool gcodeInsertSubroutinePenUpDown = false;
private static int gcodeSubroutineCount = 0;
Expand Down Expand Up @@ -695,7 +698,7 @@ public static string GetSettings()
GcodeRelative = (convertGraphics && Properties.Settings.Default.importGCRelative || auxIsRelative);

gcodeNoArcs = convertGraphics && Properties.Settings.Default.importGCNoArcs;
gcodeAngleStep = (float)Properties.Settings.Default.importGCSegment;
gcodeAngleStep = Math.Max((double)Properties.Settings.Default.importGCSegment, 0.01);

gcodeInsertSubroutineEnable = convertGraphics && Properties.Settings.Default.importGCSubEnable;
gcodeInsertSubroutinePenUpDown = convertGraphics && Properties.Settings.Default.importGCSubPenUpDown;
Expand All @@ -704,7 +707,8 @@ public static string GetSettings()
LastMovewasG0 = true;

gcodeLines = 1; // counter for GCode lines
gcodeDistance = 0; // counter for GCode move distance
gcodeDistancePD = 0; // counter for GCode move distance
gcodeDistancePU = 0; // counter for GCode move distance
remainingC = (float)Properties.Settings.Default.importGCLineSegmentLength;

gcodeLineSegmentationEnable = false;
Expand Down Expand Up @@ -973,6 +977,7 @@ public static void SetPwm(StringBuilder gcodeValue, float pwm, float delay)

public static void JobStart(StringBuilder gcodeValue, string cmto)
{
bool penup = false;
if (gcodeValue != null)
{
string cmt = cmto;
Expand Down Expand Up @@ -1001,15 +1006,17 @@ public static void JobStart(StringBuilder gcodeValue, string cmto)
else
{
PenUp(gcodeValue, "PU");
penup = true;
}

if (GcodeZApply || gcodeSpindleToggle)
{
if (gcodeUseLasermode) // in jobStart
{
if (gcodeComments) cmt = " (" + cmto + " lasermode )";
else cmt = "";
gcodeValue.AppendFormat("M{0} S{1}{2}\r\n", GcodeSpindleCmd, 0, cmt); // switch on laser with power=0
else cmt = "(JobStart)";
if (!penup)
gcodeValue.AppendFormat("M{0} S{1}{2}\r\n", GcodeSpindleCmd, 0, cmt); // switch on laser with power=0
}
else
{
Expand Down Expand Up @@ -1555,7 +1562,9 @@ internal static void SplitArc(StringBuilder gcodeValue, int gnr, XyzabcuvwPoint
XyPoint p2 = new XyPoint(pos2.X, pos2.Y);
p1.Round(); p2.Round();
arcMove = GcodeMath.GetArcMoveProperties(p1, p2, i1, j2, (gnr == 2)); // 2020-04-14 add round()
double step = Math.Asin(gcodeAngleStep / arcMove.radius); // in RAD
double step = Math.Abs(Math.Asin(gcodeAngleStep / arcMove.radius)); // in RAD
if (step <= 0)
step = 0.1;
if (step > Math.Abs(arcMove.angleDiff))
step = Math.Abs(arcMove.angleDiff / 2);

Expand Down Expand Up @@ -1735,15 +1744,15 @@ public static void SetHeaderInfo(string title, float distance, float feed, int l
{
Logger.Trace("SetHeaderInfo title:{0} distance:{1} feed:{2} lines:{3} downUp:{4}", title, distance, feed, lines, downUp);
docTitle = title;
gcodeDistance = distance;
gcodeDistancePD = distance;
GcodeXYFeed = feed;
gcodeLines = lines;
gcodeDownUp = downUp;
GcodeZApply = true;
}
public static string GetHeader(string cmt, string source)
{
gcodeTime += gcodeDistance / GcodeXYFeed;
gcodeTime += gcodeDistancePD / GcodeXYFeed;
string header = string.Format("( {0} by GRBL-Plotter {1} )\r\n", cmt, MyApplication.GetVersion());
string header_end = headerData.ToString();
header_end += string.Format("({0} >)\r\n", XmlMarker.HeaderEnd);
Expand Down Expand Up @@ -1812,30 +1821,31 @@ public static string GetHeader(string cmt, string source)
Logger.Info("◆◆ Header: G-Code repetitions:{0}", Properties.Settings.Default.importRepeatCnt);
}

header += string.Format("( G-Code lines: {0} )\r\n", gcodeLines);
header += string.Format("( G-Code lines : {0} )\r\n", gcodeLines);
Logger.Info("◆◆ Header: G-Code lines:{0}", gcodeLines);

header += string.Format("( Pen Down/Up : {0} times )\r\n", gcodeDownUp);
header += string.Format("( Path length : {0:0.0} mm )\r\n", gcodeDistance);
header += string.Format("( Pen Down/Up PD/PU : {0} times )\r\n", gcodeDownUp);
header += string.Format("( Path length (PD) : {0:0.0} mm )\r\n", gcodeDistancePD);
header += string.Format("( Path length (PU) : {0:0.0} mm )\r\n", gcodeDistancePU);

try
{
TimeSpan t = TimeSpan.FromSeconds(gcodeTime * 60);
header += string.Format("( Duration ca.: {0:D2}:{1:D2}:{2:D2} h:m:s )\r\n", t.Hours, t.Minutes, t.Seconds);
header += string.Format("( Duration ca. : {0:D2}:{1:D2}:{2:D2} h:m:s )\r\n", t.Hours, t.Minutes, t.Seconds);
}
catch (Exception err)
{
header += string.Format("( Duration ca.: {0:0.0} min. )\r\n", gcodeTime);
header += string.Format("( Duration ca. : {0:0.0} min. )\r\n", gcodeTime);
}

if (gcodeSubroutineCount > 0)
{
header += string.Format("( Call to subs.: {0} )\r\n", gcodeSubroutineCount);
header += string.Format("( Call to subs. : {0} )\r\n", gcodeSubroutineCount);
Logger.Info("◆◆ Header: Subroutine calls:{0}", gcodeSubroutineCount);
}

stopwatch.Stop();
header += string.Format("( Conv. time : {0} )\r\n", stopwatch.Elapsed);
header += string.Format("( Conv. time : {0} )\r\n", stopwatch.Elapsed);

if (Properties.Settings.Default.importGCToolTableUse)
{
Expand Down Expand Up @@ -1923,7 +1933,7 @@ private static void Drill(StringBuilder gcodeString)

gcodeTime += gcodeFigureTime;
gcodeLines += gcodeFigureLines + 3;
gcodeDistance += gcodeFigureDistance;
gcodeDistancePD += gcodeFigureDistance;
}
figureString.Clear();
}
Expand Down Expand Up @@ -1994,7 +2004,7 @@ private static void IntermediateZ(StringBuilder gcodeString)
gcodeLines++;
}

if (!gcodeZNoUp)
if (!gcodeZNoUp || (zStep <= finalZ)) // Z-Up at least on final pass
{
if (gcodeUseLasermode) SpindleOff(gcodeString, "lasermode"); // send S0
gcodeString.AppendFormat("G{0} Z{1} {2}\r\n", FrmtCode(0), FrmtNum(GcodeZUp), ""); // Router up
Expand All @@ -2006,7 +2016,7 @@ private static void IntermediateZ(StringBuilder gcodeString)

gcodeTime += gcodeFigureTime;
gcodeLines += gcodeFigureLines + 3;
gcodeDistance += gcodeFigureDistance;
gcodeDistancePD += gcodeFigureDistance;
}
figureString.Clear();
}
Expand Down
Loading

0 comments on commit 5762e22

Please sign in to comment.