Skip to content

Commit

Permalink
Fix problem with opening XYZ files containing negative positions (#2649)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Dec 3, 2024
1 parent 10d694f commit cb42faa
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ This file is part of Universal Gcode Sender (UGS).
import javax.swing.filechooser.FileFilter;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.util.Optional;

/**
* A generic file dialog base
*/
public class FileDialogBase extends FileDialog {

public FileDialogBase(String directory) {
super((Frame) null);

Expand All @@ -42,6 +43,24 @@ public FileDialogBase(String directory) {
setDirectory(directory);
}

public void centerOn(Frame frame) {
if (frame == null) {
return;
}

pack();
setSize(800, 600);
validate();

double width = getBounds().getWidth();
double height = getBounds().getHeight();

Rectangle rect = frame.getBounds();
int x = (int) (rect.getCenterX() - (width / 2));
int y = (int) (rect.getCenterY() - (height / 2));
setLocation(new Point(x, y));
}

@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ This file is part of Universal Gcode Sender (UGS).
import java.awt.FileDialog;

public class FileOpenDialog extends FileDialogBase {
public FileOpenDialog() {
this("");
}
public FileOpenDialog(String directory) {
super(directory);
setMode(FileDialog.LOAD);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Will Winder
Copyright 2023-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -48,9 +48,9 @@ public static boolean shouldEraseProbedData() {
}

public static Position getMaxPosition(List<Position> positions) {
double maxX = Double.MIN_VALUE;
double maxY = Double.MIN_VALUE;
double maxZ = Double.MIN_VALUE;
double maxX = -Double.MAX_VALUE;
double maxY = -Double.MAX_VALUE;
double maxZ = -Double.MAX_VALUE;
UnitUtils.Units units = UnitUtils.Units.MM;
for (Position p : positions) {
units = p.getUnits();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Will Winder
Copyright 2023-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -19,36 +19,44 @@ This file is part of Universal Gcode Sender (UGS).
package com.willwinder.ugs.platform.surfacescanner.actions;

import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.platform.surfacescanner.io.XyzFileFilter;
import com.willwinder.ugs.platform.surfacescanner.SurfaceScanner;
import static com.willwinder.ugs.platform.surfacescanner.Utils.getMaxPosition;
import static com.willwinder.ugs.platform.surfacescanner.Utils.getMinPosition;
import static com.willwinder.ugs.platform.surfacescanner.Utils.shouldEraseProbedData;
import com.willwinder.ugs.platform.surfacescanner.io.XyzFileFilter;
import com.willwinder.ugs.platform.surfacescanner.io.XyzSurfaceReader;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStatusEvent;
import com.willwinder.universalgcodesender.uielements.FileOpenDialog;
import com.willwinder.universalgcodesender.uielements.helpers.FilenameFilterAdapter;
import com.willwinder.universalgcodesender.utils.AutoLevelSettings;
import com.willwinder.universalgcodesender.utils.GUIHelpers;
import com.willwinder.universalgcodesender.utils.MathUtils;
import org.openide.util.ImageUtilities;
import org.openide.windows.WindowManager;

import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

import static com.willwinder.ugs.platform.surfacescanner.Utils.*;

public class OpenScannedSurfaceAction extends AbstractAction implements UGSEventListener {
public static final String ICON_BASE = "com/willwinder/ugs/platform/surfacescanner/icons/open.svg";

private final SurfaceScanner surfaceScanner;
private final BackendAPI backend;
private final FileOpenDialog fileOpenDialog;

public OpenScannedSurfaceAction(SurfaceScanner surfaceScanner) {
this.fileOpenDialog = new FileOpenDialog();
this.backend = CentralLookup.getDefault().lookup(BackendAPI.class);
this.backend.addUGSEventListener(this);
this.surfaceScanner = surfaceScanner;
Expand All @@ -61,16 +69,6 @@ public OpenScannedSurfaceAction(SurfaceScanner surfaceScanner) {
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON_BASE, false));
}

private static Optional<File> chooseOpenFile() {
fileChooser.setFileFilter(new XyzFileFilter());
int result = fileChooser.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION) {
return Optional.empty();
}

File selectedFile = fileChooser.getSelectedFile();
return Optional.of(selectedFile);
}

@Override
public boolean isEnabled() {
Expand All @@ -83,9 +81,11 @@ public void actionPerformed(ActionEvent e) {
return;
}

BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
Optional<File> file = chooseOpenFile();
if (!file.isPresent()) {
fileOpenDialog.setFilenameFilter(new FilenameFilterAdapter(new XyzFileFilter()));
fileOpenDialog.centerOn(WindowManager.getDefault().getMainWindow());
fileOpenDialog.setVisible(true);
Optional<File> file = fileOpenDialog.getSelectedFile();
if (file.isEmpty()) {
return;
}

Expand All @@ -95,7 +95,8 @@ public void actionPerformed(ActionEvent e) {

updateSettings(backend.getSettings().getAutoLevelSettings(), positions);
updatePoints(positions);
} catch (IOException ex) {
} catch (IOException | NumberFormatException ex) {
GUIHelpers.displayErrorDialog("Could not read the scanned point file, check the log file for more information.");
throw new RuntimeException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Will Winder
Copyright 2023-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -45,7 +45,7 @@ private void updateState() {
putValue(Action.SHORT_DESCRIPTION, title);
putValue("iconBase", icon);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(icon, false));
putValue(Action.SELECTED_KEY, renderable.isEnabled());
putValue(Action.SELECTED_KEY, !renderable.isEnabled());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Will Winder
Copyright 2023-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -24,7 +24,7 @@ This file is part of Universal Gcode Sender (UGS).
public class XyzFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
return f.isFile() && f.getName().endsWith(".xyz");
return f.isFile() && (f.getName().endsWith(".xyz") || f.getName().endsWith(".txt"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.willwinder.ugs.platform.surfacescanner;

import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UnitUtils;
import static org.junit.Assert.*;
import org.junit.Test;

import java.util.List;

public class UtilsTest {
@Test
public void getMaxPosition_shouldReturnTheMaxPositionEvenIfNegative() {
Position maxPosition = Utils.getMaxPosition(
List.of(
new Position(-1, -2, -3, UnitUtils.Units.MM),
new Position(-3, -2, -1, UnitUtils.Units.MM)));

assertEquals(new Position(-1, -2, -1, UnitUtils.Units.MM), maxPosition);
}

@Test
public void getMinPosition_shouldReturnTheMinPositionEvenIfNegative() {
Position minPosition = Utils.getMinPosition(
List.of(
new Position(-1, -2, -3, UnitUtils.Units.MM),
new Position(-3, -2, -1, UnitUtils.Units.MM)));

assertEquals(new Position(-3, -2, -3, UnitUtils.Units.MM), minPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This file is part of Universal Gcode Sender (UGS).
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.windows.WindowManager;

import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
Expand Down Expand Up @@ -83,6 +84,7 @@ public boolean isEnabled() {
@Override
public void actionPerformed(ActionEvent e) {
fileOpenDialog.setFilenameFilter(fileFilterService.getFilenameFilters());
fileOpenDialog.centerOn(WindowManager.getDefault().getMainWindow());
fileOpenDialog.setVisible(true);
fileOpenDialog.getSelectedFile().ifPresent(this::openFile);
}
Expand Down

0 comments on commit cb42faa

Please sign in to comment.