TclCommandWriteGCode.py 3.3 KB

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