mirror of
https://git.mirrors.martin98.com/https://github.com/petaflot/pygcode
synced 2025-07-28 14:52:00 +08:00
code for split & processing now much prettier
This commit is contained in:
parent
efeb76592d
commit
304413dfbe
@ -9,6 +9,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
for pygcode_lib_type in ('installed_lib', 'relative_lib'):
|
for pygcode_lib_type in ('installed_lib', 'relative_lib'):
|
||||||
try:
|
try:
|
||||||
@ -157,48 +158,61 @@ def gcodes2str(gcodes):
|
|||||||
return ' '.join("%s" % g for g in gcodes)
|
return ' '.join("%s" % g for g in gcodes)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def split_and_process(gcode_list, gcode_class, comment):
|
||||||
|
"""
|
||||||
|
Split gcodes by given class, yields given class instance
|
||||||
|
:param gcode_list: list of GCode instances
|
||||||
|
:param gcode_class: class inheriting from GCode (directly, or indirectly)
|
||||||
|
:param comment: Comment instance, or None
|
||||||
|
"""
|
||||||
|
(befores, (g,), afters) = split_gcodes(gcode_list, gcode_class)
|
||||||
|
# print & process those before gcode_class instance
|
||||||
|
if befores:
|
||||||
|
print(gcodes2str(befores))
|
||||||
|
machine.process_gcodes(*befores)
|
||||||
|
# yield, then process gcode_class instance
|
||||||
|
yield g
|
||||||
|
machine.process_gcodes(g)
|
||||||
|
# print & process those after gcode_class instance
|
||||||
|
if afters:
|
||||||
|
print(gcodes2str(afters))
|
||||||
|
machine.process_gcodes(*afters)
|
||||||
|
# print comment (if given)
|
||||||
|
if comment:
|
||||||
|
print(str(line.comment))
|
||||||
|
|
||||||
|
|
||||||
# =================== Process File ===================
|
# =================== Process File ===================
|
||||||
|
|
||||||
for line_str in args.infile[0].readlines():
|
for line_str in args.infile[0].readlines():
|
||||||
line = Line(line_str)
|
line = Line(line_str)
|
||||||
#if line.comment:
|
|
||||||
# print("===== %s" % line.comment.text)
|
|
||||||
|
|
||||||
|
# Effective G-Codes:
|
||||||
|
# fills in missing motion modal gcodes (using machine's current motion mode).
|
||||||
effective_gcodes = machine.block_modal_gcodes(line.block)
|
effective_gcodes = machine.block_modal_gcodes(line.block)
|
||||||
|
|
||||||
if args.arc_linearize and any(isinstance(g, GCodeArcMove) for g in effective_gcodes):
|
if args.arc_linearize and any(isinstance(g, GCodeArcMove) for g in effective_gcodes):
|
||||||
#print("---------> Found an Arc <----------")
|
with split_and_process(effective_gcodes, GCodeArcMove, line.comment) as arc:
|
||||||
(befores, (arc,), afters) = split_gcodes(effective_gcodes, GCodeArcMove)
|
linearize_params = {
|
||||||
# TODO: debug printing (for now)
|
'arc_gcode': arc,
|
||||||
if befores:
|
'start_pos': machine.pos,
|
||||||
print(gcodes2str(befores))
|
'plane': machine.mode.plane_selection,
|
||||||
machine.process_gcodes(*befores)
|
'method_class': args.arc_lin_method[arc.word],
|
||||||
#print("arc: %s" % str(arc))
|
'dist_mode': machine.mode.distance,
|
||||||
print(Comment("linearized: %r" % arc))
|
'arc_dist_mode': machine.mode.arc_ijk_distance,
|
||||||
linearize_params = {
|
'max_error': args.precision,
|
||||||
'arc_gcode': arc,
|
'decimal_places': 3,
|
||||||
'start_pos': machine.pos,
|
}
|
||||||
'plane': machine.mode.plane_selection,
|
for linear_gcode in omit_redundant_modes(linearize_arc(**linearize_params)):
|
||||||
'method_class': args.arc_lin_method[arc.word],
|
print(linear_gcode)
|
||||||
'dist_mode': machine.mode.distance,
|
|
||||||
'arc_dist_mode': machine.mode.arc_ijk_distance,
|
|
||||||
'max_error': args.precision,
|
|
||||||
'decimal_places': 3,
|
|
||||||
}
|
|
||||||
for linear_gcode in omit_redundant_modes(linearize_arc(**linearize_params)):
|
|
||||||
print(linear_gcode)
|
|
||||||
machine.process_gcodes(arc)
|
|
||||||
|
|
||||||
if afters:
|
|
||||||
print(gcodes2str(afters))
|
|
||||||
machine.process_gcodes(*afters)
|
|
||||||
if line.comment:
|
|
||||||
print(str(line.comment))
|
|
||||||
|
|
||||||
elif args.canned_simplify and any((g.word in args.canned_codes) for g in effective_gcodes):
|
elif args.canned_simplify and any((g.word in args.canned_codes) for g in effective_gcodes):
|
||||||
(befores, (canned,), afters) = split_gcodes(effective_gcodes, GCodeCannedCycle)
|
(befores, (canned,), afters) = split_gcodes(effective_gcodes, GCodeCannedCycle)
|
||||||
print(Comment('canning simplified: %r' % canned))
|
print(Comment('canning simplified: %r' % canned))
|
||||||
|
|
||||||
# TODO: simplify canned things
|
# TODO: simplify canned things
|
||||||
|
|
||||||
print(str(line))
|
print(str(line))
|
||||||
machine.process_block(line.block)
|
machine.process_block(line.block)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user