Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make importers aware of bottom left coordinate system #635

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/de/thomas_oster/visicut/VisicutModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public GraphicFileImporter getGraphicFileImporter()

public PlfPart loadGraphicFile(File f, List<String> warnings) throws ImportException
{
return this.getGraphicFileImporter().importFile(f, warnings);
return this.getGraphicFileImporter().importFile(f, getSelectedLaserDevice().isOriginBottomLeft(), getSelectedLaserDevice().getLaserCutter().getBedHeight(), warnings);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
public abstract class AbstractImporter implements Importer
{

public PlfPart importFile(File inputFile, List<String> warnings) throws ImportException
public PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> 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<String> warnings) throws ImportException;
public abstract GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public GraphicFileImporter(String[] importerClasses)
}
}

public PlfPart importFile(File inputFile, List<String> warnings) throws ImportException
public PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
if (inputFile == null)
{
Expand All @@ -85,7 +85,7 @@ public PlfPart importFile(File inputFile, List<String> warnings) throws ImportEx
{
if (i.getFileFilter().accept(inputFile))
{
PlfPart gs = i.importFile(inputFile, warnings);
PlfPart gs = i.importFile(inputFile, originIsBottomLeft, bedHeightInMm, warnings);
return gs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> warnings) throws ImportException;
PlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
public class DXFImporter extends AbstractImporter
{

public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
GraphicSet result = new GraphicSet();
try
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private Rectangle2D getBoundingBox(File epsfile)
return result;
}

public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
Writer out = null;
try
Expand All @@ -123,7 +123,7 @@ public GraphicSet importSetFromFile(File inputFile, List<String> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ private double getCoordinate(String cmd)
}

@Override
public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class JPGPNGImporter extends AbstractImporter
{

public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String getDescription()
};
}

public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
GraphicSet result = new GraphicSet();
result.setBasicTransform(new AffineTransform());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ public File translateToParametricSvg(File inputFile) throws ParserConfigurationE
}

@Override
public ParametricPlfPart importFile(File inputFile, List<String> warnings) throws ImportException
public ParametricPlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
try
{
return super.importFile(translateToParametricSvg(inputFile), warnings);
return super.importFile(translateToParametricSvg(inputFile), originIsBottomLeft, bedHeightInMm, warnings);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Parameter> parseParameters(File inputFile, List<String> warnings) throws ParserConfigurationException, SAXException, IOException
{

Expand Down Expand Up @@ -178,10 +181,12 @@ public FileFilter getFileFilter()
return FILTER;
}

public ParametricPlfPart importFile(File inputFile, List<String> warnings) throws ImportException
public ParametricPlfPart importFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> warnings) throws ImportException
{
try
{
this.originIsBottomLeft = originIsBottomLeft;
this.bedHeightInMm = bedHeightInMm;
Map<String, Parameter> parameters = this.parseParameters(inputFile, warnings);
if (new File(inputFile.getAbsolutePath()+".parameters").exists())
{
Expand Down Expand Up @@ -246,7 +251,6 @@ public GraphicSet importSetFromFile(final File inputFile, final List<String> war
{
try
{

SVGImporter svg = new SVGImporter();
double resolution = svg.determineResolution(inputFile, warnings);
final PipedOutputStream out = new PipedOutputStream();
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void importNode(SVGElement e, List<GraphicObject> result, double svgReso
}
}

public GraphicSet importSetFromFile(InputStream in, String name, double svgResolution, final List<String> warnings) throws Exception
public GraphicSet importSetFromFile(InputStream in, String name, double svgResolution, boolean originIsBottomLeft, double bedHeightInMm, final List<String> warnings) throws Exception
{
Handler svgImportLoggerHandler = new Handler()
{
Expand All @@ -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
Expand Down Expand Up @@ -312,7 +312,7 @@ public double determineResolution(File f, List<String> 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
{
Expand Down Expand Up @@ -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);
Expand All @@ -362,12 +369,12 @@ private AffineTransform determineTransformation(SVGRoot root, double svgResoluti
}

@Override
public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException
public GraphicSet importSetFromFile(File inputFile, boolean originIsBottomLeft, double bedHeightInMm, List<String> 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)
Expand Down