Merge branch 'master' into #34-gcode_algo
This commit is contained in:
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
@@ -1,15 +1,29 @@
|
|||||||
from pcbdevice.utils.path import path
|
from pcbdevice.utils.path import path
|
||||||
from pcbdevice.utils.plotimg import plotPath
|
from pcbdevice.utils.plotimg import plotPath
|
||||||
from pcbdevice.utils.FileUtils import FileUtils
|
from pcbdevice.utils.FileUtils import FileUtils
|
||||||
|
import argparse
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Usage example
|
parser = argparse.ArgumentParser(prog = 'main.py')
|
||||||
|
parser.add_argument('-i', required = True, help = 'PCB image path')
|
||||||
|
parser.add_argument('-wi', required = True, type = int, help = 'Width of the PCB')
|
||||||
|
parser.add_argument('-he', required = True, type = int, help = 'Height of the PCB')
|
||||||
|
parser.add_argument('-u', required = False, help = 'PCB dimension unit')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
matrix, height, width = FileUtils.pbmToMatrix(args.i)
|
||||||
|
|
||||||
|
if args.u:
|
||||||
|
pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi, unit = args.u)
|
||||||
|
else:
|
||||||
|
pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi)
|
||||||
|
|
||||||
|
|
||||||
resourcesRawPath = 'tests/resources/raw/'
|
resourcesRawPath = 'tests/resources/raw/'
|
||||||
resourcesFormattedPath = 'tests/resources/formatted/'
|
resourcesFormattedPath = 'tests/resources/formatted/'
|
||||||
resourcesPathOutput = 'resources/pathoutput/'
|
resourcesPathOutput = 'resources/pathoutput/'
|
||||||
resourcesExpectedPath = 'tests/resources/expected/'
|
resourcesExpectedPath = 'tests/resources/expected/'
|
||||||
|
|
||||||
FileUtils.saveMatrixToFile(FileUtils.pbmToCsv(resourcesRawPath + 'test1ascii.pbm'), resourcesFormattedPath + 'test1.csv')
|
#FileUtils.saveMatrixToFile(FileUtils.pbmToMatrix(resourcesRawPath + 'test1ascii.pbm'), resourcesFormattedPath + 'test1.csv')
|
||||||
|
|
||||||
plotPath(path(FileUtils.pbmToCsv(resourcesRawPath + 'test100x100.pbm'), 5))
|
#plotPath(path(FileUtils.pbmToMatrix(resourcesRawPath + 'test100x100.pbm'), 5))
|
||||||
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
23
pcbdevice/resources/input/square.pbm
Normal file
23
pcbdevice/resources/input/square.pbm
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
P1
|
||||||
|
# Bob
|
||||||
|
20 20
|
||||||
|
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||||
|
0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0
|
||||||
|
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
|
||||||
|
0 0 2 0 1 1 1 1 1 1 1 1 1 1 1 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 2 2 2 2 2 2 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 2 2 2 2 2 2 2 2 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0
|
||||||
|
0 0 2 0 1 1 1 1 1 1 1 1 1 1 1 1 0 2 0 0
|
||||||
|
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
|
||||||
|
0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0
|
||||||
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)]
|
||||||
@@ -6,13 +6,21 @@ from pcbdevice.utils.FileUtils import FileUtils
|
|||||||
resources = './pcbdevice/tests/resources/'
|
resources = './pcbdevice/tests/resources/'
|
||||||
|
|
||||||
class TestFileUtils(TestCase):
|
class TestFileUtils(TestCase):
|
||||||
def test_pbmToCsv(self):
|
def test_pbmToMatrix(self):
|
||||||
actual = FileUtils.pbmToCsv(resources + 'raw/test1.pbm')
|
actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
|
||||||
expected = TestUtils.readIntFile(resources + 'formatted/test1.csv')
|
expected = TestUtils.readIntFile(resources + 'formatted/test1.csv')
|
||||||
assert actual == expected
|
assert actual == expected
|
||||||
|
|
||||||
def test_saveMatrixToFile(self):
|
def test_saveMatrixToFile(self):
|
||||||
actual = FileUtils.pbmToCsv(resources + 'raw/test1.pbm')
|
actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
|
||||||
FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv')
|
FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv')
|
||||||
expected = TestUtils.readIntFile(resources + 'output/test1.csv')
|
expected = TestUtils.readIntFile(resources + 'output/test1.csv')
|
||||||
assert actual == expected
|
assert actual == expected
|
||||||
|
|
||||||
|
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)
|
||||||
@@ -2,10 +2,10 @@ import math
|
|||||||
|
|
||||||
class FileUtils:
|
class FileUtils:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pbmToCsv(pbmFile, dimensionLineIndex = 2):
|
def pbmToMatrix(pbmFilePath, dimensionLineIndex = 2):
|
||||||
completeFile = []
|
completeFile = []
|
||||||
|
|
||||||
file = open(pbmFile, 'r')
|
file = open(pbmFilePath, 'r')
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
width, height = (int(val) for val in lines[dimensionLineIndex].split())
|
width, height = (int(val) for val in lines[dimensionLineIndex].split())
|
||||||
file.close()
|
file.close()
|
||||||
@@ -18,7 +18,7 @@ class FileUtils:
|
|||||||
for index, value in enumerate(completeFile):
|
for index, value in enumerate(completeFile):
|
||||||
matrix[int(math.floor(index / width))][index % width] = value
|
matrix[int(math.floor(index / width))][index % width] = value
|
||||||
|
|
||||||
return matrix
|
return matrix, height, width
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def saveMatrixToFile(matrix, filePath):
|
def saveMatrixToFile(matrix, filePath):
|
||||||
@@ -27,4 +27,17 @@ class FileUtils:
|
|||||||
for y in x:
|
for y in x:
|
||||||
f.write('%s ' % y )
|
f.write('%s ' % y )
|
||||||
f.write('\n')
|
f.write('\n')
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getPixelSize(matHeight, matWidth, pcbHeight, pcbWidth, unit = 'mm'):
|
||||||
|
if unit == 'mm':
|
||||||
|
return pcbHeight / matHeight, pcbWidth / matWidth
|
||||||
|
elif unit == 'cm':
|
||||||
|
return pcbHeight / matHeight * 10, pcbWidth / matWidth * 10
|
||||||
|
elif unit == 'm':
|
||||||
|
return pcbHeight / matHeight * 100, pcbWidth / matWidth * 100
|
||||||
|
elif unit == 'in':
|
||||||
|
return pcbHeight / matHeight * 25.4, pcbWidth / matWidth * 25.4
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Unit not handle')
|
||||||
Reference in New Issue
Block a user