TclCommandWriteGCode.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from tclCommands.TclCommand import *
  2. class TclCommandWriteGCode(TclCommandSignaled):
  3. """
  4. Tcl shell command to save the G-code of a CNC Job object to file.
  5. """
  6. # array of all command aliases, to be able use
  7. # old names for backward compatibility (add_poly, add_polygon)
  8. aliases = ['write_gcode']
  9. # Dictionary of types from Tcl command, needs to be ordered.
  10. # For positional arguments
  11. arg_names = collections.OrderedDict([
  12. ('name', str),
  13. ('filename', str)
  14. ])
  15. # Dictionary of types from Tcl command, needs to be ordered.
  16. # For options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('preamble', str),
  19. ('postamble', str)
  20. ])
  21. # array of mandatory options for current Tcl command: required = {'name','outname'}
  22. required = ['name', 'filename']
  23. # structured help for current command, args needs to be ordered
  24. help = {
  25. 'main': "Saves G-code of a CNC Job object to file.",
  26. 'args': collections.OrderedDict([
  27. ('name', 'Source CNC Job object.'),
  28. ('filename', 'Output filename.'),
  29. ('preamble', 'Text to append at the beginning.'),
  30. ('postamble', 'Text to append at the end.')
  31. ]),
  32. 'examples': []
  33. }
  34. def execute(self, args, unnamed_args):
  35. """
  36. execute current TCL shell command
  37. :param args: array of known named arguments and options
  38. :param unnamed_args: array of other values which were passed into command
  39. without -somename and we do not have them in known arg_names
  40. :return: None or exception
  41. """
  42. """
  43. Requires obj_name to be available. It might still be in the
  44. making at the time this function is called, so check for
  45. promises and send to background if there are promises.
  46. """
  47. obj_name = args['name']
  48. filename = args['filename']
  49. preamble = args['preamble'] if 'preamble' in args else ''
  50. postamble = args['postamble'] if 'postamble' in args else ''
  51. # TODO: This is not needed any more? All targets should be present.
  52. # If there are promised objects, wait until all promises have been fulfilled.
  53. # if self.collection.has_promises():
  54. # def write_gcode_on_object(new_object):
  55. # self.log.debug("write_gcode_on_object(): Disconnecting %s" % write_gcode_on_object)
  56. # self.new_object_available.disconnect(write_gcode_on_object)
  57. # write_gcode(obj_name, filename, preamble, postamble)
  58. #
  59. # # Try again when a new object becomes available.
  60. # self.log.debug("write_gcode(): Collection has promises. Queued for %s." % obj_name)
  61. # self.log.debug("write_gcode(): Queued function: %s" % write_gcode_on_object)
  62. # self.new_object_available.connect(write_gcode_on_object)
  63. #
  64. # return
  65. # self.log.debug("write_gcode(): No promises. Continuing for %s." % obj_name)
  66. try:
  67. obj = self.app.collection.get_by_name(str(obj_name))
  68. except:
  69. return "Could not retrieve object: %s" % obj_name
  70. try:
  71. obj.export_gcode(str(filename), str(preamble), str(postamble))
  72. except Exception as e:
  73. return "Operation failed: %s" % str(e)