2019-01-03 19:25:08 +00:00
|
|
|
from tclCommands.TclCommand import TclCommandSignaled
|
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
import collections
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
class TclCommandWriteGCode(TclCommandSignaled):
|
|
|
|
"""
|
|
|
|
Tcl shell command to save the G-code of a CNC Job object to file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# array of all command aliases, to be able use
|
|
|
|
# old names for backward compatibility (add_poly, add_polygon)
|
|
|
|
aliases = ['write_gcode']
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Saves G-code of a CNC Job object to file.")
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered.
|
|
|
|
# For positional arguments
|
|
|
|
arg_names = collections.OrderedDict([
|
|
|
|
('name', str),
|
|
|
|
('filename', str)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered.
|
|
|
|
# For options like -optionname value
|
|
|
|
option_types = collections.OrderedDict([
|
|
|
|
('preamble', str),
|
2019-09-16 01:23:16 +00:00
|
|
|
('postamble', str),
|
2020-04-13 16:15:20 +00:00
|
|
|
('muted', str)
|
2019-01-03 19:25:08 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# array of mandatory options for current Tcl command: required = {'name','outname'}
|
|
|
|
required = ['name', 'filename']
|
|
|
|
|
|
|
|
# structured help for current command, args needs to be ordered
|
|
|
|
help = {
|
|
|
|
'main': "Saves G-code of a CNC Job object to file.",
|
|
|
|
'args': collections.OrderedDict([
|
2020-04-09 01:13:04 +00:00
|
|
|
('name', 'Source CNC Job object. Required.'),
|
|
|
|
('filename', 'Output filename. Required.'),
|
2019-01-03 19:25:08 +00:00
|
|
|
('preamble', 'Text to append at the beginning.'),
|
2019-09-16 01:23:16 +00:00
|
|
|
('postamble', 'Text to append at the end.'),
|
2020-04-13 16:15:20 +00:00
|
|
|
('muted', 'It will not put errors in the Shell or status bar. True (1) or False (0)')
|
2019-09-16 01:23:16 +00:00
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2019-08-25 21:40:35 +00:00
|
|
|
'examples': ["write_gcode name c:\\\\gcode_repo"]
|
2019-01-03 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
execute current TCL shell command
|
|
|
|
|
|
|
|
:param args: array of known named arguments and options
|
|
|
|
:param unnamed_args: array of other values which were passed into command
|
|
|
|
without -somename and we do not have them in known arg_names
|
|
|
|
:return: None or exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
Requires obj_name to be available. It might still be in the
|
|
|
|
making at the time this function is called, so check for
|
|
|
|
promises and send to background if there are promises.
|
|
|
|
"""
|
|
|
|
|
|
|
|
obj_name = args['name']
|
|
|
|
filename = args['filename']
|
|
|
|
|
|
|
|
preamble = args['preamble'] if 'preamble' in args else ''
|
|
|
|
postamble = args['postamble'] if 'postamble' in args else ''
|
|
|
|
|
2019-09-16 01:23:16 +00:00
|
|
|
if 'muted' in args:
|
2020-04-22 11:37:03 +00:00
|
|
|
try:
|
|
|
|
par = args['muted'].capitalize()
|
|
|
|
except AttributeError:
|
|
|
|
par = args['muted']
|
|
|
|
muted = bool(eval(par))
|
2019-09-16 14:20:56 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
muted = False
|
2019-09-16 01:23:16 +00:00
|
|
|
|
2020-04-22 11:37:03 +00:00
|
|
|
# This is not needed any more? All targets should be present.
|
2019-01-03 19:25:08 +00:00
|
|
|
# If there are promised objects, wait until all promises have been fulfilled.
|
|
|
|
# if self.collection.has_promises():
|
|
|
|
# def write_gcode_on_object(new_object):
|
|
|
|
# self.log.debug("write_gcode_on_object(): Disconnecting %s" % write_gcode_on_object)
|
|
|
|
# self.new_object_available.disconnect(write_gcode_on_object)
|
|
|
|
# write_gcode(obj_name, filename, preamble, postamble)
|
|
|
|
#
|
|
|
|
# # Try again when a new object becomes available.
|
|
|
|
# self.log.debug("write_gcode(): Collection has promises. Queued for %s." % obj_name)
|
|
|
|
# self.log.debug("write_gcode(): Queued function: %s" % write_gcode_on_object)
|
|
|
|
# self.new_object_available.connect(write_gcode_on_object)
|
|
|
|
#
|
|
|
|
# return
|
|
|
|
|
|
|
|
# self.log.debug("write_gcode(): No promises. Continuing for %s." % obj_name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
obj = self.app.collection.get_by_name(str(obj_name))
|
2019-11-11 00:35:42 +00:00
|
|
|
except Exception:
|
2020-04-13 16:15:20 +00:00
|
|
|
if muted is False:
|
2019-09-16 01:23:16 +00:00
|
|
|
return "Could not retrieve object: %s" % obj_name
|
|
|
|
else:
|
2019-09-16 23:58:01 +00:00
|
|
|
return "fail"
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
try:
|
2020-04-28 11:37:34 +00:00
|
|
|
obj.export_gcode(str(filename), str(preamble), str(postamble), from_tcl=True)
|
2019-01-03 19:25:08 +00:00
|
|
|
except Exception as e:
|
2020-04-13 16:15:20 +00:00
|
|
|
if muted is False:
|
2019-09-16 01:23:16 +00:00
|
|
|
return "Operation failed: %s" % str(e)
|
|
|
|
else:
|
|
|
|
return
|