TclCommandWriteGCode.py 3.1 KB

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