From 45ae79d7192ab0d53b75069bdaa47707f4303abc Mon Sep 17 00:00:00 2001 From: Thomas Oster Date: Wed, 19 Jan 2022 21:13:57 +0100 Subject: [PATCH] Make importers aware of bottom left coordinate system --- .../de/thomas_oster/visicut/VisicutModel.java | 2 +- .../model/graphicelements/AbstractImporter.java | 6 +++--- .../graphicelements/GraphicFileImporter.java | 4 ++-- .../visicut/model/graphicelements/Importer.java | 2 +- .../graphicelements/dxfsupport/DXFImporter.java | 4 ++-- .../graphicelements/epssupport/EPSImporter.java | 4 ++-- .../gcodesupport/GCodeImporter.java | 3 ++- .../jpgpngsupport/JPGPNGImporter.java | 2 +- .../lssupport/LaserScriptImporter.java | 2 +- .../psvgsupport/PSVGImporter.java | 4 ++-- .../psvgsupport/ParametricSVGImporter.java | 10 +++++++--- .../graphicelements/svgsupport/SVGImporter.java | 17 ++++++++++++----- 12 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/thomas_oster/visicut/VisicutModel.java b/src/main/java/de/thomas_oster/visicut/VisicutModel.java index 818c376d1..e1d6fc09f 100644 --- a/src/main/java/de/thomas_oster/visicut/VisicutModel.java +++ b/src/main/java/de/thomas_oster/visicut/VisicutModel.java @@ -699,7 +699,7 @@ public GraphicFileImporter getGraphicFileImporter() public PlfPart loadGraphicFile(File f, List warnings) throws ImportException { - return this.getGraphicFileImporter().importFile(f, warnings); + return this.getGraphicFileImporter().importFile(f, getSelectedLaserDevice().isOriginBottomLeft(), getSelectedLaserDevice().getLaserCutter().getBedHeight(), warnings); } diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/AbstractImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/AbstractImporter.java index 55b19b7b1..271a8ecf9 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/AbstractImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/AbstractImporter.java @@ -30,14 +30,14 @@ public abstract class AbstractImporter implements Importer { - public PlfPart importFile(File inputFile, List warnings) throws ImportException + public PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { PlfPart p = new PlfPart(); p.setSourceFile(inputFile); - p.setGraphicObjects(this.importSetFromFile(inputFile, warnings)); + p.setGraphicObjects(this.importSetFromFile(inputFile, originIsBottomLeft, bedHeightInMm, warnings)); return p; } - public abstract GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException; + public abstract GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException; } diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/GraphicFileImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/GraphicFileImporter.java index e40dfddc8..8a6563589 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/GraphicFileImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/GraphicFileImporter.java @@ -69,7 +69,7 @@ public GraphicFileImporter(String[] importerClasses) } } - public PlfPart importFile(File inputFile, List warnings) throws ImportException + public PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { if (inputFile == null) { @@ -85,7 +85,7 @@ public PlfPart importFile(File inputFile, List warnings) throws ImportEx { if (i.getFileFilter().accept(inputFile)) { - PlfPart gs = i.importFile(inputFile, warnings); + PlfPart gs = i.importFile(inputFile, originIsBottomLeft, bedHeightInMm, warnings); return gs; } } diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/Importer.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/Importer.java index 6767595a8..fe280aa78 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/Importer.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/Importer.java @@ -38,6 +38,6 @@ public interface Importer * They will be displayed to the user, but no * further action is taken */ - PlfPart importFile(File inputFile, List warnings) throws ImportException; + PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException; } diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/dxfsupport/DXFImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/dxfsupport/DXFImporter.java index 8e0bc809e..1e05795cc 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/dxfsupport/DXFImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/dxfsupport/DXFImporter.java @@ -51,7 +51,7 @@ public class DXFImporter extends AbstractImporter { - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { GraphicSet result = new GraphicSet(); try @@ -92,7 +92,7 @@ public void run() }).start(); SVGImporter svgimp = new SVGImporter(); //TODO Check which resolution it exports and adapt it to mm - result = svgimp.importSetFromFile(in, inputFile.getName(), 1/Util.mm2inch(1), warnings); + result = svgimp.importSetFromFile(in, inputFile.getName(), 1/Util.mm2inch(1), originIsBottomLeft, bedHeightInMm, warnings); } catch (Exception ex) { diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/epssupport/EPSImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/epssupport/EPSImporter.java index a9377c59e..eb1763a03 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/epssupport/EPSImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/epssupport/EPSImporter.java @@ -96,7 +96,7 @@ private Rectangle2D getBoundingBox(File epsfile) return result; } - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { Writer out = null; try @@ -123,7 +123,7 @@ public GraphicSet importSetFromFile(File inputFile, List warnings) throw File tmp = File.createTempFile("temp", "svg"); tmp.deleteOnExit(); svgGenerator.stream(new FileWriter(tmp)); - GraphicSet result = new SVGImporter().importSetFromFile(tmp, warnings); + GraphicSet result = new SVGImporter().importSetFromFile(tmp, originIsBottomLeft, bedHeightInMm, warnings); //Assume the EPS has been created with 72DPI (from Inkscape) double px2mm = Util.inch2mm(1d/72d); result.setBasicTransform(AffineTransform.getScaleInstance(px2mm, px2mm)); diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/gcodesupport/GCodeImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/gcodesupport/GCodeImporter.java index 6d15478e6..bcc874009 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/gcodesupport/GCodeImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/gcodesupport/GCodeImporter.java @@ -94,10 +94,11 @@ private double getCoordinate(String cmd) } @Override - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { try { + //TODO if originIsBottomLeft replace y with bedHeight-y everywhere (?) BufferedReader reader = new BufferedReader(new FileReader(inputFile)); GraphicSet result = new GraphicSet(); GeneralPath resultingShape = new GeneralPath(); diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/jpgpngsupport/JPGPNGImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/jpgpngsupport/JPGPNGImporter.java index c84477d8b..df3519ad0 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/jpgpngsupport/JPGPNGImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/jpgpngsupport/JPGPNGImporter.java @@ -37,7 +37,7 @@ public class JPGPNGImporter extends AbstractImporter { - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { try { diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java index 8ff7a87a1..3eecdcf94 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java @@ -64,7 +64,7 @@ public String getDescription() }; } - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { GraphicSet result = new GraphicSet(); result.setBasicTransform(new AffineTransform()); diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/PSVGImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/PSVGImporter.java index f493efdd1..90f8e302e 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/PSVGImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/PSVGImporter.java @@ -186,11 +186,11 @@ public File translateToParametricSvg(File inputFile) throws ParserConfigurationE } @Override - public ParametricPlfPart importFile(File inputFile, List warnings) throws ImportException + public ParametricPlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { try { - return super.importFile(translateToParametricSvg(inputFile), warnings); + return super.importFile(translateToParametricSvg(inputFile), originIsBottomLeft, bedHeightInMm, warnings); } catch (Exception ex) { diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/ParametricSVGImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/ParametricSVGImporter.java index 6e7c5dd1c..634d3348d 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/ParametricSVGImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/psvgsupport/ParametricSVGImporter.java @@ -60,6 +60,9 @@ public class ParametricSVGImporter implements Importer private static FileFilter FILTER = new ExtensionFilter(".parametric.svg", "Parametric SVG files"); + private boolean originIsBottomLeft = false; + private double bedHeightInMm = 0; + public Map parseParameters(File inputFile, List warnings) throws ParserConfigurationException, SAXException, IOException { @@ -178,10 +181,12 @@ public FileFilter getFileFilter() return FILTER; } - public ParametricPlfPart importFile(File inputFile, List warnings) throws ImportException + public ParametricPlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { try { + this.originIsBottomLeft = originIsBottomLeft; + this.bedHeightInMm = bedHeightInMm; Map parameters = this.parseParameters(inputFile, warnings); if (new File(inputFile.getAbsolutePath()+".parameters").exists()) { @@ -246,7 +251,6 @@ public GraphicSet importSetFromFile(final File inputFile, final List war { try { - SVGImporter svg = new SVGImporter(); double resolution = svg.determineResolution(inputFile, warnings); final PipedOutputStream out = new PipedOutputStream(); @@ -269,7 +273,7 @@ public void run() } } }.start(); - return (new SVGImporter()).importSetFromFile(in, inputFile.getName(), resolution, warnings); + return (new SVGImporter()).importSetFromFile(in, inputFile.getName(), resolution, originIsBottomLeft, bedHeightInMm, warnings); } catch (Exception ex) { diff --git a/src/main/java/de/thomas_oster/visicut/model/graphicelements/svgsupport/SVGImporter.java b/src/main/java/de/thomas_oster/visicut/model/graphicelements/svgsupport/SVGImporter.java index 37dab4aea..d4dac28c0 100644 --- a/src/main/java/de/thomas_oster/visicut/model/graphicelements/svgsupport/SVGImporter.java +++ b/src/main/java/de/thomas_oster/visicut/model/graphicelements/svgsupport/SVGImporter.java @@ -112,7 +112,7 @@ private void importNode(SVGElement e, List result, double svgReso } } - public GraphicSet importSetFromFile(InputStream in, String name, double svgResolution, final List warnings) throws Exception + public GraphicSet importSetFromFile(InputStream in, String name, double svgResolution, boolean originIsBottomLeft, double bedHeightInMm, final List warnings) throws Exception { Handler svgImportLoggerHandler = new Handler() { @@ -137,7 +137,7 @@ public void close() throws SecurityException{} URI svg = u.loadSVG(in, Helper.toPathName(name)); root = u.getDiagram(svg).getRoot(); GraphicSet result = new GraphicSet(); - result.setBasicTransform(determineTransformation(root, svgResolution)); + result.setBasicTransform(determineTransformation(root, svgResolution, originIsBottomLeft, bedHeightInMm)); // The resulting transformation is the mapping of "SVG pixels" to real millimeters. // If viewBox, width and height are set, this scaling can be different from @@ -312,7 +312,7 @@ public double determineResolution(File f, List warnings) * SVG default is 90, but AI generates 72?? * Since inkscape 0.92 SVG default is 96 DPI. */ - private AffineTransform determineTransformation(SVGRoot root, double svgResolution) + private AffineTransform determineTransformation(SVGRoot root, double svgResolution, boolean originBottomLeft, double bedHeightInMm) { try { @@ -348,6 +348,13 @@ private AffineTransform determineTransformation(SVGRoot root, double svgResoluti if (width != 0 && height != 0 && root.getPres(sty.setName("viewBox"))) { float[] coords = sty.getFloatList(); + /** + * https://github.com/t-oster/VisiCut/issues/633 If origin is bottom left + * and a viewbox is given, use the bottom-left corner for placement. + */ + if (originBottomLeft && height < bedHeightInMm) { + y += bedHeightInMm - height; + } Rectangle2D coordinateBox = new Rectangle2D.Double(x,y,width,height); Rectangle2D viewBox = new Rectangle2D.Float(coords[0], coords[1], coords[2], coords[3]); return Helper.getTransform(viewBox, coordinateBox); @@ -362,12 +369,12 @@ private AffineTransform determineTransformation(SVGRoot root, double svgResoluti } @Override - public GraphicSet importSetFromFile(File inputFile, List warnings) throws ImportException + public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List warnings) throws ImportException { try { double svgResolution = determineResolution(inputFile, warnings); - GraphicSet result = this.importSetFromFile(new FileInputStream(inputFile), inputFile.getName(), svgResolution, warnings); + GraphicSet result = this.importSetFromFile(new FileInputStream(inputFile), inputFile.getName(), svgResolution, originIsBottomLeft, bedHeightInMm, warnings); return result; } catch (Exception ex)