Convert matrix index to gcode

#39 Save GCode in file
Convert a list of index to gcode string in an array
This commit is contained in:
Marc-Antoine Lafreniere
2019-02-20 13:48:37 -05:00
parent ef8e908c33
commit 8aee76eee2
6 changed files with 86 additions and 58 deletions

View File

@@ -5,31 +5,50 @@ from pcbdevice.models.Coordinates import Coordinate
class TestListToGCode(TestCase):
oneTrace = [Coordinate(1, 2),
Coordinate(1, 5),
Coordinate(2, 5),
Coordinate(2, 8)]
twoTrace = [Coordinate(1, 2),
Coordinate(1, 5),
Coordinate(5, 5),
Coordinate(5, 2),
Coordinate(1, 2),
Coordinate(-1, -1),
Coordinate(5, 4),
Coordinate(8, 4)]
threeTrace = [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)]
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))
self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize))
self.assertEqual(listToGCode(self.twoTrace, ySize, xSize), getExpected(self.twoTrace, ySize, xSize))
self.assertEqual(listToGCode(self.threeTrace, ySize, xSize), getExpected(self.threeTrace, ySize, xSize))
def test_listToGCodePixelSize(self):
xSize, ySize = 1, 4
coords = whenSingleTrace()
self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize))
self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize))
xSize, ySize = 4, 2
coords = whenSingleTrace()
self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize))
self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize))
xSize, ySize = 8, -1
coords = whenSingleTrace()
self.assertRaises(RuntimeError, lambda: listToGCode(coords, ySize, xSize))
self.assertRaises(RuntimeError, lambda: listToGCode(self.oneTrace, ySize, xSize))
def getExpected(coords, ySize, xSize):
header = ['G28', 'G90\n']
@@ -49,36 +68,3 @@ def getExpected(coords, ySize, xSize):
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)]

View File

@@ -0,0 +1,5 @@
This
is
a
test

View File

@@ -0,0 +1,5 @@
G28
G90
G0 Z3
G0 X15 Y45

View File

@@ -2,6 +2,7 @@ from unittest import TestCase
from pcbdevice.utils import TestUtils
from pcbdevice.utils.FileUtils import FileUtils
from pcbdevice.utils.TestUtils import readStringFile
resources = './pcbdevice/tests/resources/'
@@ -9,18 +10,35 @@ class TestFileUtils(TestCase):
def test_pbmToMatrix(self):
actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
expected = TestUtils.readIntFile(resources + 'formatted/test1.csv')
assert actual == expected
self.assertEqual(actual, expected)
def test_saveMatrixToFile(self):
actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv')
expected = TestUtils.readIntFile(resources + 'output/test1.csv')
assert actual == expected
self.assertEqual(actual, expected)
def test_saveStringListToFile(self):
stringList = ['This',
'is',
'a\n',
'test']
FileUtils.saveStringListToFile(stringList, resources + 'output/text1.txt')
self.assertEqual(readStringFile(resources + 'output/text1.txt'), readStringFile(resources+'expected/text1.txt'))
stringList = ['G28',
'G90',
'G0 Z3\n',
'G0 X15 Y45']
FileUtils.saveStringListToFile(stringList, resources + 'output/text2.txt')
self.assertEqual(readStringFile(resources + 'output/text2.txt'), readStringFile(resources + 'expected/text2.txt'))
def test_getPixelSize(self):
assert 10, 10 == FileUtils.getPixelSize(10, 10, 100, 100)
assert 1, 1 == FileUtils.getPixelSize(100, 100, 100, 100)
assert 10, 10 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'cm')
assert 10, 10 == FileUtils.getPixelSize(10, 10, 1, 1, unit = 'm')
assert 254, 254 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'in')
assert 10, 5 == FileUtils.getPixelSize(10, 10, 10, 20)
self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 100, 100))
self.assertEqual((1, 1), FileUtils.getPixelSize(100, 100, 100, 100))
self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 10, 10, unit = 'cm'))
self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 1, 1, unit = 'm'))
self.assertEqual((25.4, 25.4), FileUtils.getPixelSize(10, 10, 10, 10, unit = 'in'))
self.assertEqual((1, 2), FileUtils.getPixelSize(10, 10, 10, 20))

View File

@@ -29,6 +29,13 @@ class FileUtils:
f.write('\n')
f.close()
@staticmethod
def saveStringListToFile(stringList, filePath):
with open(filePath, 'w') as f:
for line in stringList:
f.write('%s\n' % line)
f.close()
@staticmethod
def getPixelSize(matHeight, matWidth, pcbHeight, pcbWidth, unit = 'mm'):
if unit == 'mm':

View File

@@ -12,3 +12,10 @@ def readIntFile(filePath):
completeFile.append(tempArray)
return completeFile
def readStringFile(filePath):
file = open(filePath, 'r')
lines = file.readlines()
file.close()
return lines