26
pcbdevice/gcode/GcodeBuilder.py
Normal file
26
pcbdevice/gcode/GcodeBuilder.py
Normal file
@@ -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
|
||||
0
pcbdevice/gcode/__init__.py
Normal file
0
pcbdevice/gcode/__init__.py
Normal file
@@ -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/'
|
||||
|
||||
19
pcbdevice/models/Coordinates.py
Normal file
19
pcbdevice/models/Coordinates.py
Normal file
@@ -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
|
||||
0
pcbdevice/models/__init__.py
Normal file
0
pcbdevice/models/__init__.py
Normal file
0
pcbdevice/tests/gcode/__init__.py
Normal file
0
pcbdevice/tests/gcode/__init__.py
Normal file
84
pcbdevice/tests/gcode/test_listToGcode.py
Normal file
84
pcbdevice/tests/gcode/test_listToGcode.py
Normal file
@@ -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)]
|
||||
Reference in New Issue
Block a user