From 55c97649059ebc8c7d007206c59897009d7ec264 Mon Sep 17 00:00:00 2001 From: pim Date: Tue, 28 May 2024 21:45:20 +0200 Subject: [PATCH] GenericGcodeDriver/Grbl: Fix for flip-setting also translating by bed width/height when startpoint is set --- .../drivers/GenericGcodeDriver.java | 28 +++++++++++-------- .../liblasercut/drivers/Grbl.java | 6 ++-- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/thomas_oster/liblasercut/drivers/GenericGcodeDriver.java b/src/main/java/de/thomas_oster/liblasercut/drivers/GenericGcodeDriver.java index 5c3b593e..8b611966 100644 --- a/src/main/java/de/thomas_oster/liblasercut/drivers/GenericGcodeDriver.java +++ b/src/main/java/de/thomas_oster/liblasercut/drivers/GenericGcodeDriver.java @@ -73,8 +73,6 @@ import java.text.DecimalFormat; import java.text.NumberFormat; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; /** * This class implements a driver for a generic GRBL GCode Lasercutter. @@ -479,19 +477,19 @@ protected String formatDouble(double value, int decimalPlaces) return coordinateFormat.format(value); } - protected void writeVectorGCode(VectorPart vp, double resolution) throws UnsupportedEncodingException, IOException { + protected void writeVectorGCode(VectorPart vp, double resolution, boolean flipShouldAlsoTranslate) throws UnsupportedEncodingException, IOException { for (VectorCommand cmd : vp.getCommandList()) { switch (cmd.getType()) { // TODO: x,y should be changed to double because GCode has infinite vector resolution anyway case MOVETO: int x = (int) cmd.getX(); int y = (int) cmd.getY(); - move(out, x, y, resolution); + move(out, x, y, resolution, flipShouldAlsoTranslate); break; case LINETO: x = (int) cmd.getX(); y = (int) cmd.getY(); - line(out, x, y, resolution); + line(out, x, y, resolution, flipShouldAlsoTranslate); break; case SETPROPERTY: FloatPowerSpeedFocusProperty p = (FloatPowerSpeedFocusProperty) cmd.getProperty(); @@ -529,9 +527,9 @@ protected void setFocus(PrintStream out, double focus) throws IOException { } } - protected void move(PrintStream out, double x, double y, double resolution) throws IOException { - x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); - y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); + protected void move(PrintStream out, double x, double y, double resolution, boolean flipShouldAlsoTranslate) throws IOException { + x = isFlipXaxis() ? (flipShouldAlsoTranslate ? getBedWidth() : 0) - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); + y = isFlipYaxis() ? (flipShouldAlsoTranslate ? getBedHeight() : 0) - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); currentSpeed = getTravel_speed(); if (blankLaserDuringRapids) @@ -545,9 +543,9 @@ protected void move(PrintStream out, double x, double y, double resolution) thro } } - protected void line(PrintStream out, double x, double y, double resolution) throws IOException { - x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); - y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); + protected void line(PrintStream out, double x, double y, double resolution, boolean flipShouldAlsoTranslate) throws IOException { + x = isFlipXaxis() ? (flipShouldAlsoTranslate ? getBedWidth() : 0) - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); + y = isFlipYaxis() ? (flipShouldAlsoTranslate ? getBedHeight() : 0) - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); String append = ""; if (nextPower != currentPower) @@ -982,6 +980,12 @@ public void sendJob(LaserJob job, ProgressListener pl, List warnings) th } public void writeJobCode(LaserJob job, ProgressListener pl) throws IOException { + + boolean flipShouldAlsoTranslate = true; + if (job.getTransformedOriginX() != 0 || job.getTransformedOriginY() != 0) { + flipShouldAlsoTranslate = false; + } + writeInitializationCode(); pl.progressChanged(this, 20); int i = 0; @@ -1000,7 +1004,7 @@ public void writeJobCode(LaserJob job, ProgressListener pl) throws IOException { { //TODO: in direct mode use progress listener to indicate progress //of individual job - writeVectorGCode((VectorPart) p, p.getDPI()); + writeVectorGCode((VectorPart) p, p.getDPI(), flipShouldAlsoTranslate); } i++; pl.progressChanged(this, 20 + (int) (i*(double) 60/max)); diff --git a/src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java b/src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java index 54ac7765..0480c9cc 100644 --- a/src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java +++ b/src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java @@ -163,9 +163,9 @@ protected String waitForIdentificationLine(ProgressListener pl) throws IOExcepti * Doesn't include travel speed since grbl ignores that anyway. */ @Override - protected void move(PrintStream out, double x, double y, double resolution) throws IOException { - x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); - y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); + protected void move(PrintStream out, double x, double y, double resolution, boolean flipShouldAlsoTranslate) throws IOException { + x = isFlipXaxis() ? (flipShouldAlsoTranslate ? getBedWidth() : 0) - Util.px2mm(x, resolution) : Util.px2mm(x, resolution); + y = isFlipYaxis() ? (flipShouldAlsoTranslate ? getBedHeight() : 0) - Util.px2mm(y, resolution) : Util.px2mm(y, resolution); currentSpeed = getTravel_speed(); if (blankLaserDuringRapids) {