improved output formatting
This commit is contained in:
parent
9447c747b5
commit
27b5a9d8a5
1 changed files with 54 additions and 32 deletions
|
@ -50,10 +50,14 @@ class GCodeFilter(object):
|
|||
if self.update_position(line):
|
||||
result = []
|
||||
handler = LineHandler(line)
|
||||
for pos in self.transform_position():
|
||||
changed_axes = self.target_pos.get_changed_axes(pos)
|
||||
yield handler.get_line(pos, changed_axes)
|
||||
self.target_pos.update(pos)
|
||||
positions = list(self.transform_position())
|
||||
if not positions:
|
||||
yield handler.get_line(None, None)
|
||||
else:
|
||||
for pos in positions:
|
||||
changed_axes = self.target_pos.get_changed_axes(pos)
|
||||
yield handler.get_line(pos, changed_axes)
|
||||
self.target_pos.update(pos)
|
||||
else:
|
||||
# no coordinate given
|
||||
yield line
|
||||
|
@ -63,12 +67,12 @@ class PositionHandler(object):
|
|||
|
||||
def __init__(self):
|
||||
self.pos = tuple([0] * len(AXIS))
|
||||
self.pos_stack = [tuple(self.pos)]
|
||||
self.stack = [tuple(self.pos)]
|
||||
|
||||
def push(self, position):
|
||||
self.pos_stack.insert(0, tuple(position))
|
||||
if len(self.pos_stack) > 10:
|
||||
self.pos_stack.pop(-1)
|
||||
self.stack.insert(0, tuple(position))
|
||||
if len(self.stack) > 10:
|
||||
self.stack.pop(-1)
|
||||
|
||||
def update(self, position):
|
||||
new_pos = tuple(position)
|
||||
|
@ -106,20 +110,32 @@ class LineHandler(object):
|
|||
return token
|
||||
|
||||
def get_line(self, position, axes):
|
||||
result = []
|
||||
self._processed = []
|
||||
self._position = position
|
||||
self._axes = axes
|
||||
line = re.sub(AXIS_REGEX, self.replace_value, self.line, re.I)
|
||||
if not self._processed:
|
||||
# no axis definition
|
||||
line = self.line
|
||||
if position is None:
|
||||
# remove all coordinates from the line
|
||||
# (e.g. for a modal switch ("G0 X..") outside of a crop space)
|
||||
line = re.sub(AXIS_REGEX, "", self.line, re.I)
|
||||
for i in range(10):
|
||||
line = line.replace(" ", " ")
|
||||
if line.strip():
|
||||
return line.rstrip() + self._suffix
|
||||
else:
|
||||
# invalid line - skip it
|
||||
return None
|
||||
else:
|
||||
# at least one axis was defined
|
||||
for i, axis in axes:
|
||||
if not i in self._processed:
|
||||
line += " %s%s" % (axis, str(position[i]))
|
||||
return line + self._suffix
|
||||
result = []
|
||||
self._processed = []
|
||||
self._position = position
|
||||
self._axes = axes
|
||||
line = re.sub(AXIS_REGEX, self.replace_value, self.line, re.I)
|
||||
if not self._processed:
|
||||
# no axis definition
|
||||
line = self.line
|
||||
else:
|
||||
# at least one axis was defined
|
||||
for i, axis in axes:
|
||||
if not i in self._processed:
|
||||
line += " %s%s" % (axis, str(position[i]))
|
||||
return line + self._suffix
|
||||
|
||||
|
||||
class ShiftFilter(GCodeFilter):
|
||||
|
@ -170,7 +186,7 @@ class DensifyFilter(GCodeFilter):
|
|||
return tuple(target)
|
||||
|
||||
def transform_position(self):
|
||||
stack = self.source_pos.pos_stack
|
||||
stack = self.source_pos.stack
|
||||
if (len(stack) > 2) and self.is_inside(stack[0]) and \
|
||||
self.is_inside(stack[1]) and \
|
||||
self._densify_is_valid(stack[0], stack[1]):
|
||||
|
@ -207,29 +223,33 @@ class DensifyFilter(GCodeFilter):
|
|||
class CropFilter(GCodeFilter):
|
||||
|
||||
def transform_position(self):
|
||||
is_inside = self.is_inside(self.pos)
|
||||
was_inside = self.pos_stack[1]
|
||||
stack = self.source_pos.stack
|
||||
is_inside = self.is_inside(stack[0])
|
||||
was_inside = (len(stack) < 2) or self.is_inside(stack[1])
|
||||
result = None
|
||||
if is_inside and was_inside:
|
||||
# inside -> inside
|
||||
yield self.pos
|
||||
yield stack[0]
|
||||
elif not is_inside and not was_inside:
|
||||
# outside -> outside
|
||||
pass
|
||||
else:
|
||||
# outside -> inside OR inside -> outside
|
||||
border_pos = self._recurse_border_position(self._previous_position,
|
||||
pos, self.is_inside)
|
||||
# the border position is always the first step
|
||||
yield border_pos
|
||||
if len(stack) >= 2:
|
||||
border_pos = self._recurse_border_position(stack[1], stack[0])
|
||||
# adapt the precision to the precision of the input
|
||||
border_pos = tuple([value.quantize(template)
|
||||
for value, template in zip(border_pos, stack[0])])
|
||||
# the border position is always the first step
|
||||
yield border_pos
|
||||
# omit the current position if we are outside
|
||||
if is_inside:
|
||||
yield self.pos
|
||||
yield stack[0]
|
||||
|
||||
def _recurse_border_position(self, p1, p2, depth_limit=20):
|
||||
""" simple and stupid: bisections between p1 and p2
|
||||
"""
|
||||
p_middle = tuple([0.5 * (axis1 + axis2)
|
||||
p_middle = tuple([d(0.5) * (axis1 + axis2)
|
||||
for axis1, axis2 in zip(p1, p2)])
|
||||
if depth_limit < 0:
|
||||
return p_middle
|
||||
|
@ -286,5 +306,7 @@ if __name__ == "__main__":
|
|||
sys.exit(1)
|
||||
for line in infile.readlines():
|
||||
for out_line in gcode_filter.get_processed_lines(line):
|
||||
outfile.write(out_line)
|
||||
# omit empty lines
|
||||
if not out_line is None:
|
||||
outfile.write(out_line)
|
||||
|
||||
|
|
Loading…
Reference in a new issue