From dc47a8f56261cd1fc83ba01c004d3548e5c587f0 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 24 Jan 2024 21:04:16 +0100 Subject: [PATCH] Version 1.7.1.1 --- GRBL-Plotter/GCodeCreation/Graphic2GCode.cs | 24 +++++++--- .../GCodeCreation/Graphic2GCodeRelated.cs | 3 +- .../GCodeCreation/GraphicCollectData.cs | 32 ++++++++++---- .../GCodeCreation/GraphicGenerateHatchFill.cs | 44 +++++++++++++------ GRBL-Plotter/GCodeCreation/ToolTable.cs | 15 ++++++- GRBL-Plotter/GCodeCreation/XmlMarker.cs | 18 ++++---- 6 files changed, 98 insertions(+), 38 deletions(-) diff --git a/GRBL-Plotter/GCodeCreation/Graphic2GCode.cs b/GRBL-Plotter/GCodeCreation/Graphic2GCode.cs index c4d2dfb7e..9e4cb7336 100644 --- a/GRBL-Plotter/GCodeCreation/Graphic2GCode.cs +++ b/GRBL-Plotter/GCodeCreation/Graphic2GCode.cs @@ -1,7 +1,7 @@ /* GRBL-Plotter. Another GCode sender for GRBL. This file is part of the GRBL-Plotter application. - Copyright (C) 2019-2023 Sven Hasemann contact: svenhb@web.de + Copyright (C) 2019-2024 Sven Hasemann contact: svenhb@web.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -44,6 +44,8 @@ You should have received a copy of the GNU General Public License * 2023-09-14 f:CreateGCode add CollectionStart /-End Tags * 2023-11-03 l:357 f:CreateGCode (figure) add proforma figure-tag if not figureEnable * 2023-11-27 l:465 f:ProcessPathObject call subroutine only if needed + * 2024-01-03 l:360 f:CreateGCode check if (completeGraphic.Count > 0) + * 2024-01-07 l:393 f:CreateGCode if (useToolTable) also adapt width/diameter issue #370 */ using System; @@ -286,7 +288,9 @@ internal static bool CreateGCode(List completeGraphic, List if (Properties.Settings.Default.importGCToolDefNrUse) toolToUse = (int)Properties.Settings.Default.importGCToolDefNr; string toolColor = ToolTable.GetToolColor(toolToUse); + double toolWidth = ToolTable.GetToolDiameter(toolToUse); // 2024-01-07 #370 pathObject.Info.GroupAttributes[(int)GroupOption.ByColor] = toolColor; + pathObject.Info.GroupAttributes[(int)GroupOption.ByWidth] = string.Format("{0:0.000}",toolWidth); // 2024-01-07 #370 } ProcessPathObject(pathObject, graphicInfo, -1, ""); // create Dot or Path GCode, but no tool change } @@ -352,12 +356,18 @@ internal static bool CreateGCode(List completeGraphic, List< int toolNr; string toolName; string toolColor; + double toolWidth; if (completeGraphic == null) return false; if (!graphicInfo.FigureEnable) // proforma figure tag { - string figColor = string.Format(" PenColor=\"{0}\"", completeGraphic[0].Info.GroupAttributes[(int)GroupOption.ByColor]); - string figWidth = string.Format(" PenWidth=\"{0}\"", completeGraphic[0].Info.GroupAttributes[(int)GroupOption.ByWidth]); + string figColor = " PenColor=\"blue\""; + string figWidth = " PenWidth=\"0.999\""; + if (completeGraphic.Count > 0) + { + figColor = string.Format(" PenColor=\"{0}\"", completeGraphic[0].Info.GroupAttributes[(int)GroupOption.ByColor]); + figWidth = string.Format(" PenWidth=\"{0}\"", completeGraphic[0].Info.GroupAttributes[(int)GroupOption.ByWidth]); + } Gcode.Comment(gcodeString, string.Format("{0} Id=\"{1}\" {2} {3}>", XmlMarker.FigureStart, 0, figColor, figWidth)); } @@ -379,9 +389,13 @@ internal static bool CreateGCode(List completeGraphic, List< toolName = ToolTable.GetToolName(toolToUse); toolColor = ToolTable.GetToolColor(toolToUse); // 2021-08-26 before toolNr + toolWidth = ToolTable.GetToolDiameter(toolToUse); // 2024-01-07 #370 if (useToolTable) // 2021-08-26 #217 + { pathObject.Info.GroupAttributes[(int)GroupOption.ByColor] = toolColor; + pathObject.Info.GroupAttributes[(int)GroupOption.ByWidth] = string.Format("{0:0.000}", toolWidth); // 2024-01-07 #370 + } if (logEnable) Logger.Trace("CreateGCode2 toolNr:{0} name:{1}", toolToUse, toolName); // add tool change after
setZ:{3:0.00}", graphicInfo.PenWidthMin, graphicInfo.PenWidthMax, DotData.OptZ, newZ); newZ = Math.Max(origZ, newZ); // don't go deeper than set Z @@ -451,7 +465,7 @@ private static void ProcessPathObject(PathObject pathObject, Graphic.GraphicInfo } if (graphicInfo.OptionSFromWidth) { - // dotCounter++; + // dotCounter++; double newS = CalculateSFromRange(graphicInfo.PenWidthMin, graphicInfo.PenWidthMax, DotData.OptZ); if (logEnable) Logger.Trace("--ProcessPathObject: penWidth:{0:0.00} -> setS:{1:0.00}", DotData.OptZ, newS); Gcode.GcodePwmDown = Gcode.GcodeSpindleSpeed = (float)newS; //??? diff --git a/GRBL-Plotter/GCodeCreation/Graphic2GCodeRelated.cs b/GRBL-Plotter/GCodeCreation/Graphic2GCodeRelated.cs index bcee9e231..e47fe054d 100644 --- a/GRBL-Plotter/GCodeCreation/Graphic2GCodeRelated.cs +++ b/GRBL-Plotter/GCodeCreation/Graphic2GCodeRelated.cs @@ -1236,7 +1236,8 @@ public static void PenUp(StringBuilder gcodeValue, string cmto) gcodeValue.AppendFormat(" {0}\r\n", comment); //(" (PU)"); // mark pen up } - private static double lastx, lasty, lastz; + private static double lastx, lasty; + public static double lastz; private static float lastg, lastf; public static void SetLastxyz(double lx, double ly, double lz) { lastx = lx; lasty = ly; lastz = lz; } diff --git a/GRBL-Plotter/GCodeCreation/GraphicCollectData.cs b/GRBL-Plotter/GCodeCreation/GraphicCollectData.cs index 83f7a4690..5be70235a 100644 --- a/GRBL-Plotter/GCodeCreation/GraphicCollectData.cs +++ b/GRBL-Plotter/GCodeCreation/GraphicCollectData.cs @@ -140,18 +140,30 @@ public static void CleanUp() { Logger.Trace("---- CleanUp() before WorkingSet: {0} kb Total Memory: {1:N0} kb", System.Environment.WorkingSet / 1024, GC.GetTotalMemory(false) / 1024); Graphic2GCode.CleanUp(); - completeGraphic = null; - finalPathList = null; - tileGraphicAll = null; - groupedGraphic = null; - headerInfo = null; - headerMessage = null; - backgroundWorker = null; // will be set by GCodeFromxxx - backgroundEvent = null; - GCode = null; + ClearList(completeGraphic); // = null; + ClearList(finalPathList); // = null; + ClearList(tileGraphicAll); // = null; + ClearList(groupedGraphic); // = null; + headerInfo.Clear(); // = null; + headerMessage.Clear(); // = null; + //backgroundWorker = null; // will be set by GCodeFromxxx + //backgroundEvent = null; + GCode.Clear(); // = null; GC.Collect(); Logger.Trace("---- CleanUp() after WorkingSet: {0} kb Total Memory: {1:N0} kb", System.Environment.WorkingSet / 1024, GC.GetTotalMemory(true) / 1024); } + private static void ClearList(List lista) + { + lista.Clear(); + int identificador = GC.GetGeneration(lista); + GC.Collect(identificador, GCCollectionMode.Forced); + } + private static void ClearList(List lista) + { + lista.Clear(); + int identificador = GC.GetGeneration(lista); + GC.Collect(identificador, GCCollectionMode.Forced); + } #region notifications public static bool SizeOk() @@ -700,6 +712,8 @@ public static void SetHeaderMessage(string txt) // 1st-digit: 1-Import, 2-CollectData, 3-Gernerate GCode // 2nd-digit: 1-SVG, 2-DXF, 3-HPGL, if (logProperties) Logger.Trace("Set HeaderMessage '{0}'", txt); + if (headerMessage == null) + return; if (headerMessage.Count > 0) { for (int i = 0; i < headerMessage.Count; i++) // don't add if already existing diff --git a/GRBL-Plotter/GCodeCreation/GraphicGenerateHatchFill.cs b/GRBL-Plotter/GCodeCreation/GraphicGenerateHatchFill.cs index ece34c38d..7569b85ef 100644 --- a/GRBL-Plotter/GCodeCreation/GraphicGenerateHatchFill.cs +++ b/GRBL-Plotter/GCodeCreation/GraphicGenerateHatchFill.cs @@ -1,7 +1,7 @@ /* GRBL-Plotter. Another GCode sender for GRBL. This file is part of the GRBL-Plotter application. - Copyright (C) 2015-2023 Sven Hasemann contact: svenhb@web.de + Copyright (C) 2015-2024 Sven Hasemann contact: svenhb@web.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,11 +23,13 @@ You should have received a copy of the GNU General Public License * 2023-01-15 line 90 bug-fix in log-output * 2023-08-14 l:48 f: ShrinkPaths Hatch fill extension: shrink enveloping path , delete enveloping path (Vers 1.7.0.1) * 2023-11-07 l:369 f:ClipLineByPolygone if distance is too small, delete both intersection point + * 2023-12-29 l:113 f:HatchFill do correct copy from tmpPath2 to tmpPath */ using System; using System.Collections.Generic; +using System.Linq; using System.Windows; namespace GrblPlotter @@ -96,19 +98,23 @@ internal static void HatchFill(List graphicToFill) { Logger.Trace("no dim"); continue; } // collect paths of same id, process if id changes - if (logModification && (index < (maxObject - 1))) Logger.Trace(" Add to PathData ID:{0} nextIsSameHatch:{1} max:{2} index:{3} id_now:{4} id_next:{5} fill:{6}", PathData.Info.Id, nextIsSameHatch, maxObject, index, graphicToFill[index].Info.Id, graphicToFill[index + 1].Info.Id, fill); + if (logModification && (index < (maxObject - 1))) Logger.Trace(" Add to PathData1 ID:{0} nextIsSameHatch:{1} max:{2} index:{3} id_now:{4} id_next:{5} fill:{6} inset2:{7} tmpPath.count:{8}", PathData.Info.Id, nextIsSameHatch, maxObject, index, graphicToFill[index].Info.Id, graphicToFill[index + 1].Info.Id, fill, inset2, tmpPath.Count); if (nextIsSameHatch) { indexToDelete.Add(index); continue; } + //if (logModification && (index < (maxObject - 1))) Logger.Trace(" Add to PathData2 ID:{0} nextIsSameHatch:{1} max:{2} index:{3} id_now:{4} id_next:{5} fill:{6} inset2:{7} tmpPath.count:{8}", PathData.Info.Id, nextIsSameHatch, maxObject, index, graphicToFill[index].Info.Id, graphicToFill[index + 1].Info.Id, fill, inset2, tmpPath.Count); + if (inset2) { - if (!ShrinkPaths(tmpPath, insetVal)) + if (!ShrinkPaths(tmpPath, insetVal)) { ShrinkPaths(tmpPath2, -insetVal); - tmpPath = tmpPath2; // collect paths + tmpPath.Clear(); + foreach (PathObject tmp in tmpPath2) + tmpPath.Add(tmp.Copy()); } } @@ -122,9 +128,15 @@ internal static void HatchFill(List graphicToFill) // process single hatch lines - shorten to match inside polygone finalPattern.Clear(); + //if (logModification && (index < (maxObject - 1))) Logger.Trace(" Add to PathData3 hatchPattern count:{0} tmpPath count:{1}", hatchPattern.Count, tmpPath.Count); foreach (Point[] hatchLine in hatchPattern) - { ClipLineByPolygone(hatchLine[0], hatchLine[1], tmpPath, finalPattern); } + { + //Logger.Trace(" 0:{0} 1:{1} ", hatchLine[0], hatchLine[1]); + ClipLineByPolygone(hatchLine[0], hatchLine[1], tmpPath, finalPattern); + } + + //if (logModification && (index < (maxObject - 1))) Logger.Trace(" Add to PathData4 finalPattern count:{0} tmpPath count:{1}", finalPattern.Count, tmpPath.Count); // add processed hatch lines to final graphic AddLinesToGraphic(finalPattern, PathData); @@ -279,7 +291,7 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path for (int k = 1; k < ipath.Path.Count; k++) { p4 = ipath.Path[k].MoveTo; - intersect = CalculateIntersection(p1, p2, p3, p4); + intersect = CalculateIntersection(p1, p2, p3, p4); // p1/p2 = hatch line, p3/p4 = part of object path return intersection 0-1 or -1 if no intersection if ((0.0 <= intersect) && (intersect <= 1.0)) { @@ -338,7 +350,7 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path } else d_and_a.Add(new IntersectionInfo(intersect, 123456.0, 123456.0));// # Mark for complete hatch excision, hatch is parallel to segment - } + }// shrink else d_and_a.Add(new IntersectionInfo(intersect, 0, 0));// # zero length to be removed from hatch } @@ -348,8 +360,9 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path } //# Return now if there were no intersections if (d_and_a.Count == 0) + { return; - + } // d_and_a.sort() - by s d_and_a.Sort((x, y) => x.s.CompareTo(y.s)); @@ -359,6 +372,7 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path for (int i=1; i < d_and_a.Count; i++) { + //Logger.Trace(" s1:{0:0.000} slast:{1:0.000} diff:{2:0.000}", d_and_a[i].s , last.s, Math.Abs(d_and_a[i].s - last.s)); if ((Math.Abs(d_and_a[i].s - last.s)) > 0.0000000001) { d_and_a[i_last] = last = d_and_a[i]; // different positions - take over @@ -366,13 +380,15 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path } else { - d_and_a[--i_last] = last = d_and_a[i]; // same positions - skip both 2023-11-07 - } + d_and_a[--i_last] = last = d_and_a[i]; // same positions - skip both 2023-11-07 + } } - d_and_a = d_and_a.GetRange(0, i_last); + d_and_a = d_and_a.GetRange(0, i_last); // correct size if (d_and_a.Count < 2) + { return; + } int j = 0; @@ -396,7 +412,7 @@ private static void ClipLineByPolygone(Point p1, Point p2, List path f_length_to_be_removed_from_pt1 = d_and_a[j].s2; f_length_to_be_removed_from_pt2 = d_and_a[j + 1].s1; if ((f_initial_hatch_length - (f_length_to_be_removed_from_pt1 + f_length_to_be_removed_from_pt2)) <= f_min_allowed_hatch_length) - { } // # Just don't insert it into the hatch list + { Logger.Trace("Just don't insert"); } // # Just don't insert it into the hatch list else { Point pt1 = RelativeControlPointPosition(f_length_to_be_removed_from_pt1, x2 - x1, y2 - y1, x1, y1); @@ -441,7 +457,7 @@ private static Point RelativeControlPointPosition(double distance, double f_delt private static bool ShrinkPaths(List paths, double distance) { // Check dimension before and after shrink, to decide if distance must be inverteed - Logger.Info(" ShrinkPaths count:{0} distance:{1}", paths.Count, distance); + if (logModification) Logger.Info(" ShrinkPaths count:{0} distance:{1}", paths.Count, distance); Dimensions dimBefore = new Dimensions(); Dimensions dimAfter = new Dimensions(); @@ -453,7 +469,7 @@ private static bool ShrinkPaths(List paths, double distance) } double dx = dimBefore.dimx - dimAfter.dimx; double dy = dimBefore.dimy - dimAfter.dimy; - Logger.Info(" ShrinkPaths dx:{0:0.00} dy:{1:0.00}", dx, dy); + if (logModification) Logger.Info(" ShrinkPaths dx:{0:0.00} dy:{1:0.00}", dx, dy); return ((dx > 0) || (dy > 0)); } private static void ShrinkPath(PathObject path, double distance) diff --git a/GRBL-Plotter/GCodeCreation/ToolTable.cs b/GRBL-Plotter/GCodeCreation/ToolTable.cs index 39c9c6ed4..52b209674 100644 --- a/GRBL-Plotter/GCodeCreation/ToolTable.cs +++ b/GRBL-Plotter/GCodeCreation/ToolTable.cs @@ -1,7 +1,7 @@ /* GRBL-Plotter. Another GCode sender for GRBL. This file is part of the GRBL-Plotter application. - Copyright (C) 2015-2023 Sven Hasemann contact: svenhb@web.de + Copyright (C) 2015-2024 Sven Hasemann contact: svenhb@web.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,6 +29,7 @@ You should have received a copy of the GNU General Public License * 2022-07-29 Init add try catch * 2023-03-03 add xmlAtrribute * 2023-03-08 l:87 f:ToolProp add WriteAttributes ReadAttributes for XML data + * 2024-01-07 l:181 f:GetToolDiameter needed to fix issue #370 */ using System; @@ -177,6 +178,18 @@ public static string GetToolColor(int index) return "000000"; // return black } + public static double GetToolDiameter(int index) + { + foreach (ToolProp tool in toolTableArray) + { + if (index == tool.Toolnr) + { + return tool.Diameter; + } + } + return 1.23; + } + public static int GetIndexByToolNR(int toolNr) { for (int i = 0; i < toolTableArray.Count; i++) diff --git a/GRBL-Plotter/GCodeCreation/XmlMarker.cs b/GRBL-Plotter/GCodeCreation/XmlMarker.cs index 3da62495d..4a184ba3b 100644 --- a/GRBL-Plotter/GCodeCreation/XmlMarker.cs +++ b/GRBL-Plotter/GCodeCreation/XmlMarker.cs @@ -1,7 +1,7 @@ /* GRBL-Plotter. Another GCode sender for GRBL. This file is part of the GRBL-Plotter application. - Copyright (C) 2019-2023 Sven Hasemann contact: svenhb@web.de + Copyright (C) 2019-2024 Sven Hasemann contact: svenhb@web.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ You should have received a copy of the GNU General Public License * 2023-07-03 l:338 f:GetAttributeValue check if remaining length is enough * 2023-07-31 new functions FindInsertPositionFigure/Group/Next * 2023-11-15 add figurePenColorAnyCount + * 2024-01-14 l:452 f:FinishFigure change check if (tmpFigure.PenColor.Contains("none")) */ using System; @@ -445,13 +446,14 @@ public static void FinishFigure(int lineEnd) { tmpFigure.LineEnd = lineEnd; tmpFigure.MyIndex = listFigures.Count; - - if (tmpFigure.PenColor != "") - figurePenColorAnyCount++; - - if (tmpFigure.PenColor.Contains("none")) - figurePenColorNoneCount++; - + + if (!string.IsNullOrEmpty(tmpFigure.PenColor)) + { + figurePenColorAnyCount++; + + if (tmpFigure.PenColor.Contains("none")) + figurePenColorNoneCount++; + } listFigures.Add(tmpFigure); footer.LineStart = footer.LineEnd = Math.Max(footer.LineStart, lineEnd); // highest block-line = start of footer }