Rename modules
Rename modules with better name and move the arduino code to its own folder
This commit is contained in:
35
gcodeextractor/gcode/GcodeBuilder.py
Normal file
35
gcodeextractor/gcode/GcodeBuilder.py
Normal file
@@ -0,0 +1,35 @@
|
||||
def listToGCode(listIndex, pHeight, pWidth):
|
||||
"""
|
||||
Convert a list of matrix coordinate in a list of GCode commands
|
||||
|
||||
:param listIndex: List of coordinate
|
||||
:param pHeight: Pixel height in mm
|
||||
:param pWidth: Pixel width in mm
|
||||
:return: List of all the GCode commands
|
||||
"""
|
||||
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(round(coord.getX()*pWidth, 2)) + ' Y' + str(round(coord.getY()*pHeight, 2)))
|
||||
if toolUp:
|
||||
gcodeCommand.append('G0 Z3')
|
||||
toolUp = False
|
||||
|
||||
# FOOTER
|
||||
gcodeCommand.append('\nG0 Z0')
|
||||
gcodeCommand.append('G28')
|
||||
gcodeCommand.append('M18')
|
||||
|
||||
return gcodeCommand
|
||||
101
gcodeextractor/gcode/GcodeCreator.py
Normal file
101
gcodeextractor/gcode/GcodeCreator.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from gcodeextractor.models.Coordinates import Coordinate
|
||||
|
||||
|
||||
def findEndOfLine(image, direction, line, column, sequence):
|
||||
"""
|
||||
Start from a pixel and finds the end of a line of pixel in the direction specified
|
||||
:param image: image the sequence is created from
|
||||
:param direction: direction to go to find the end of line
|
||||
:param line: line index of the pixel to apply the function
|
||||
:param column: column index of the pixel to apply the function
|
||||
:param sequence: list of coordinates to append the end of line coordinates
|
||||
:return:
|
||||
"""
|
||||
|
||||
distance = 0
|
||||
|
||||
if direction == 0:
|
||||
while (line - distance) >= 0 and image[line - distance][column] == 2:
|
||||
image[line - distance][column] = 3
|
||||
distance += 1
|
||||
sequence.append(Coordinate(column, line - (distance - 1)))
|
||||
image[line - (distance-1)][column] = 2
|
||||
|
||||
elif direction == 1:
|
||||
while (column + distance) < len(image[line]) and image[line][column + distance] == 2:
|
||||
image[line][column + distance] = 3
|
||||
distance += 1
|
||||
sequence.append(Coordinate(column + (distance - 1), line))
|
||||
image[line][column + (distance-1)] = 2
|
||||
|
||||
elif direction == 2:
|
||||
while (line + distance) < len(image) and image[line + distance][column] == 2:
|
||||
image[line + distance][column] = 3
|
||||
distance += 1
|
||||
sequence.append(Coordinate(column, line + (distance - 1)))
|
||||
image[line + (distance-1)][column] = 2
|
||||
|
||||
elif direction == 3:
|
||||
while (column - distance) >= 0 and image[line][column - distance] == 2:
|
||||
image[line][column - distance] = 3
|
||||
distance += 1
|
||||
sequence.append(Coordinate(column - (distance - 1), line))
|
||||
image[line][column - (distance-1)] = 2
|
||||
|
||||
elif direction == -1:
|
||||
image[line][column] = 3
|
||||
|
||||
return distance
|
||||
|
||||
|
||||
def findDirection(image, line, column):
|
||||
"""
|
||||
Looks for a nearby pixel to choose a direction to start a line
|
||||
:param image: image the sequence is created from
|
||||
:param line: line index of the pixel to apply the function
|
||||
:param column: column index of the pixel to apply the function
|
||||
:return: direction of a nearby pixel, up = 0, right = 1, down = 2, left = 3, no pixel = -1. priorities is this order
|
||||
"""
|
||||
|
||||
if line != 0:
|
||||
if image[line-1][column] == 2:
|
||||
return 0
|
||||
|
||||
if column != (len(image[0]) - 1):
|
||||
if image[line][column+1] == 2:
|
||||
return 1
|
||||
|
||||
if line != (len(image)-1):
|
||||
if image[line+1][column] == 2:
|
||||
return 2
|
||||
|
||||
if column != 0:
|
||||
if image[line][column-1] == 2:
|
||||
return 3
|
||||
|
||||
return -1
|
||||
|
||||
|
||||
def createSequence(image):
|
||||
"""
|
||||
Create a sequence of index coordinates from a path matrix
|
||||
:param image: matrix of the path the tool needs to take
|
||||
:return: Array of class coordinates (X , Y)
|
||||
"""
|
||||
width = len(image[0])
|
||||
height = len(image)
|
||||
sequence = []
|
||||
|
||||
for line in range(height):
|
||||
for column in range(width):
|
||||
|
||||
if image[line][column] == 2:
|
||||
sequence.append(Coordinate(-1, -1))
|
||||
sequence.append(Coordinate(column, line))
|
||||
direction = 4
|
||||
while direction > -1:
|
||||
x, y = sequence[-1].getX(), sequence[-1].getY()
|
||||
direction = findDirection(image, y, x)
|
||||
findEndOfLine(image, direction, y, x, sequence)
|
||||
|
||||
return sequence
|
||||
0
gcodeextractor/gcode/__init__.py
Normal file
0
gcodeextractor/gcode/__init__.py
Normal file
89
gcodeextractor/gcode/path.py
Normal file
89
gcodeextractor/gcode/path.py
Normal file
@@ -0,0 +1,89 @@
|
||||
def scanHorizontal(image, rTool):
|
||||
"""
|
||||
:param image: image to apply the scan
|
||||
:param rTool: tool radius
|
||||
:return: adds 2's where there should be a path for the tool, looking at the image only horizontally
|
||||
"""
|
||||
|
||||
width = len(image[0])
|
||||
height = len(image)
|
||||
|
||||
for line in range(height):
|
||||
for column in range(width):
|
||||
|
||||
if column > 0:
|
||||
if image[line][column] == 1 and image[line][column - 1] != 1:
|
||||
for px in range(2 * rTool + 1):
|
||||
if column - rTool >= 0 and 0 <= line - rTool + px < height:
|
||||
if image[line - rTool + px][column - rTool] == 0:
|
||||
image[line - rTool + px][column - rTool] = 2
|
||||
|
||||
if column < width - 1:
|
||||
if image[line][column] == 1 and image[line][column + 1] != 1:
|
||||
for px in range(2 * rTool + 1):
|
||||
if column + rTool < width and 0 <= line - rTool + px < height:
|
||||
if image[line - rTool + px][column + rTool] == 0:
|
||||
image[line - rTool + px][column + rTool] = 2
|
||||
|
||||
return image
|
||||
|
||||
|
||||
def scanVertical(image, rTool):
|
||||
"""
|
||||
:param image: image to apply the scan
|
||||
:param rTool: tool radius
|
||||
:return: adds 2's where there should be a path for the tool, looking at the image only vertically
|
||||
"""
|
||||
|
||||
width = len(image[0])
|
||||
height = len(image)
|
||||
|
||||
for line in range(height):
|
||||
for column in range(width):
|
||||
|
||||
if line > 0:
|
||||
if image[line][column] == 1 and image[line - 1][column] != 1:
|
||||
for px in range(2 * rTool + 1):
|
||||
if line - rTool >= 0 and 0 <= column - rTool + px < width:
|
||||
if image[line - rTool][column - rTool + px] == 0:
|
||||
image[line - rTool][column - rTool + px] = 2
|
||||
|
||||
if line < height - 1:
|
||||
if image[line][column] == 1 and image[line + 1][column] != 1:
|
||||
for px in range(2 * rTool + 1):
|
||||
if line + rTool < height and 0 <= column - rTool + px < width:
|
||||
if image[line + rTool][column - rTool + px] == 0:
|
||||
image[line + rTool][column - rTool + px] = 2
|
||||
|
||||
return image
|
||||
|
||||
def twoRemoving(image, rTool):
|
||||
"""
|
||||
:param image: image to apply the scan
|
||||
:param rTool: tool radius
|
||||
:return: removes unnecessary twos to leave a path of only one pixel large
|
||||
"""
|
||||
|
||||
width = len(image[0])
|
||||
height = len(image)
|
||||
|
||||
for line in range(height):
|
||||
for column in range(width):
|
||||
|
||||
if image[line][column] == 1:
|
||||
for pixelx in range(1, 2 * rTool):
|
||||
for pixely in range(1, 2*rTool):
|
||||
if 0 <= line - rTool + pixelx < height and 0 <= column - rTool + pixely < width:
|
||||
if image[line - rTool + pixelx][column - rTool + pixely] == 2:
|
||||
image[line - rTool + pixelx][column - rTool + pixely] = 0
|
||||
|
||||
print("image width = " + str(width))
|
||||
print("image height = " + str(height))
|
||||
|
||||
return image
|
||||
|
||||
|
||||
def path(image, rTool):
|
||||
return twoRemoving(scanVertical(scanHorizontal(image, rTool), rTool), rTool)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user