diff --git a/pcbdevice/gcode/GcodeBuilder.py b/pcbdevice/gcode/GcodeBuilder.py new file mode 100644 index 0000000..4042389 --- /dev/null +++ b/pcbdevice/gcode/GcodeBuilder.py @@ -0,0 +1,26 @@ +def listToGCode(listIndex, pHeight, pWidth): + gcodeCommand = [] + toolUp = True + + if pHeight <= 0 or pWidth <= 0: + raise RuntimeError('Pixel dimension error') + + # HEADER + gcodeCommand.append('G28') + gcodeCommand.append('G90\n') + + for coord in listIndex: + if coord.getX() == -1 and coord.getY() == -1: + gcodeCommand.append('G0 Z0') + toolUp = True + else: + gcodeCommand.append('G0 X' + str(coord.getX()*pWidth) + ' Y' + str(coord.getY()*pHeight)) + if toolUp: + gcodeCommand.append('G0 Z3') + toolUp = False + + # FOOTER + gcodeCommand.append('\nG0 Z0') + gcodeCommand.append('G28') + + return gcodeCommand \ No newline at end of file diff --git a/pcbdevice/gcode/__init__.py b/pcbdevice/gcode/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pcbdevice/main.py b/pcbdevice/main.py index 3c62d90..637ac52 100644 --- a/pcbdevice/main.py +++ b/pcbdevice/main.py @@ -17,11 +17,7 @@ if __name__ == "__main__": pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi, unit = args.u) else: pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi) - - - print(pxHeight, pxWidth) - - # Usage example + resourcesRawPath = 'tests/resources/raw/' resourcesFormattedPath = 'tests/resources/formatted/' diff --git a/pcbdevice/models/Coordinates.py b/pcbdevice/models/Coordinates.py new file mode 100644 index 0000000..1d9728e --- /dev/null +++ b/pcbdevice/models/Coordinates.py @@ -0,0 +1,19 @@ +class Coordinate: + _x = -1 + _y = -1 + + def __init__(self, x = -1, y = -1): + self._x = x + self._y = y + + def setX(self, x): + self._x = x + + def setY(self, y): + self._y = y + + def getX(self): + return self._x + + def getY(self): + return self._y \ No newline at end of file diff --git a/pcbdevice/models/__init__.py b/pcbdevice/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pcbdevice/tests/gcode/__init__.py b/pcbdevice/tests/gcode/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pcbdevice/tests/gcode/test_listToGcode.py b/pcbdevice/tests/gcode/test_listToGcode.py new file mode 100644 index 0000000..04571a2 --- /dev/null +++ b/pcbdevice/tests/gcode/test_listToGcode.py @@ -0,0 +1,84 @@ +from unittest import TestCase + +from pcbdevice.gcode.GcodeBuilder import listToGCode +from pcbdevice.models.Coordinates import Coordinate + + +class TestListToGCode(TestCase): + def test_listToGCodeMultipleTrace(self): + xSize, ySize = 2, 3 + + coords = whenSingleTrace() + self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + + coords = whenTwoTrace() + self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + + coords = whenThreeTrace() + self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + + def test_listToGCodePixelSize(self): + xSize, ySize = 1, 4 + coords = whenSingleTrace() + self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + + xSize, ySize = 4, 2 + coords = whenSingleTrace() + self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + + xSize, ySize = 8, -1 + coords = whenSingleTrace() + self.assertRaises(RuntimeError, lambda: listToGCode(coords, ySize, xSize)) + + +def getExpected(coords, ySize, xSize): + header = ['G28', 'G90\n'] + footer = ['\nG0 Z0', 'G28'] + + content = ['G0 X' + str(xSize * coords[0].getX()) + ' Y' + str(ySize * coords[0].getY()), + 'G0 Z3', + ] + + for index, coord in enumerate(coords): + if index > 0: + if coord.getX() != -1 and coord.getY() != -1: + content.append('G0 X' + str(xSize * coord.getX()) + ' Y' + str(ySize * coord.getY())) + if coords[index - 1].getX() == -1 and coords[index - 1].getX() == -1: + content.append('G0 Z3') + else: + content.append('G0 Z0') + + return header + content + footer + + +def whenSingleTrace(): + return [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(2, 5), + Coordinate(2, 8)] + + +def whenTwoTrace(): + return [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(5, 5), + Coordinate(5, 2), + Coordinate(1, 2), + Coordinate(-1, -1), + Coordinate(5, 4), + Coordinate(8, 4)] + + +def whenThreeTrace(): + return [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(5, 5), + Coordinate(5, 2), + Coordinate(1, 2), + Coordinate(-1, -1), + Coordinate(5, 4), + Coordinate(8, 4), + Coordinate(2, 9), + Coordinate(9, 45), + Coordinate(12, 12), + Coordinate(1, 10)] \ No newline at end of file