TclCommandWriteGCode.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ('muted', int)
  22. ])
  23. # array of mandatory options for current Tcl command: required = {'name','outname'}
  24. required = ['name', 'filename']
  25. # structured help for current command, args needs to be ordered
  26. help = {
  27. 'main': "Saves G-code of a CNC Job object to file.",
  28. 'args': collections.OrderedDict([
  29. ('name', 'Source CNC Job object.'),
  30. ('filename', 'Output filename.'),
  31. ('preamble', 'Text to append at the beginning.'),
  32. ('postamble', 'Text to append at the end.'),
  33. ('muted', 'It will not put errors in the Shell or status bar.')
  34. ]),
  35. 'examples': ["write_gcode name c:\\\\gcode_repo"]
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  39. execute current TCL shell command
  40. :param args: array of known named arguments and options
  41. :param unnamed_args: array of other values which were passed into command
  42. without -somename and we do not have them in known arg_names
  43. :return: None or exception
  44. """
  45. """
  46. Requires obj_name to be available. It might still be in the
  47. making at the time this function is called, so check for
  48. promises and send to background if there are promises.
  49. """
  50. obj_name = args['name']
  51. filename = args['filename']
  52. preamble = args['preamble'] if 'preamble' in args else ''
  53. postamble = args['postamble'] if 'postamble' in args else ''
  54. if 'muted' in args:
  55. muted = args['muted']
  56. else:
  57. muted = 0
  58. # TODO: This is not needed any more? All targets should be present.
  59. # If there are promised objects, wait until all promises have been fulfilled.
  60. # if self.collection.has_promises():
  61. # def write_gcode_on_object(new_object):
  62. # self.log.debug("write_gcode_on_object(): Disconnecting %s" % write_gcode_on_object)
  63. # self.new_object_available.disconnect(write_gcode_on_object)
  64. # write_gcode(obj_name, filename, preamble, postamble)
  65. #
  66. # # Try again when a new object becomes available.
  67. # self.log.debug("write_gcode(): Collection has promises. Queued for %s." % obj_name)
  68. # self.log.debug("write_gcode(): Queued function: %s" % write_gcode_on_object)
  69. # self.new_object_available.connect(write_gcode_on_object)
  70. #
  71. # return
  72. # self.log.debug("write_gcode(): No promises. Continuing for %s." % obj_name)
  73. try:
  74. obj = self.app.collection.get_by_name(str(obj_name))
  75. except:
  76. if muted == 0:
  77. return "Could not retrieve object: %s" % obj_name
  78. else:
  79. return "fail"
  80. try:
  81. obj.export_gcode(str(filename), str(preamble), str(postamble))
  82. except Exception as e:
  83. if not muted:
  84. return "Operation failed: %s" % str(e)
  85. else:
  86. return