TclCommandExportGcode.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. from camlib import CNCjob
  3. import collections
  4. class TclCommandExportGcode(TclCommandSignaled):
  5. """
  6. Tcl shell command to export gcode as tcl output for "set X [export_gcode ...]"
  7. Requires name to be available. It might still be in the
  8. making at the time this function is called, so check for
  9. promises and send to background if there are promises.
  10. This export may be captured and passed as preable
  11. to another "export_gcode" or "write_gcode" call to join G-Code.
  12. example:
  13. set_sys units MM
  14. new
  15. open_gerber tests/gerber_files/simple1.gbr -outname margin
  16. isolate margin -dia 3
  17. cncjob margin_iso
  18. cncjob margin_iso
  19. set EXPORT [export_gcode margin_iso_cnc]
  20. write_gcode margin_iso_cnc_1 /tmp/file.gcode ${EXPORT}
  21. """
  22. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  23. aliases = ['export_gcode']
  24. # dictionary of types from Tcl command, needs to be ordered
  25. arg_names = collections.OrderedDict([
  26. ('name', str),
  27. ('preamble', str),
  28. ('postamble', str)
  29. ])
  30. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  31. option_types = collections.OrderedDict()
  32. # array of mandatory options for current Tcl command: required = {'name','outname'}
  33. required = ['name']
  34. # structured help for current command, args needs to be ordered
  35. help = {
  36. 'main': "Export gcode into console output.",
  37. 'args': collections.OrderedDict([
  38. ('name', 'Name of the source Geometry object.'),
  39. ('preamble', 'Prepend GCODE.'),
  40. ('postamble', 'Append GCODE.')
  41. ]),
  42. 'examples': []
  43. }
  44. def execute(self, args, unnamed_args):
  45. """
  46. execute current TCL shell command
  47. :param args: array of known named arguments and options
  48. :param unnamed_args: array of other values which were passed into command
  49. without -somename and we do not have them in known arg_names
  50. :return: None or exception
  51. """
  52. name = args['name']
  53. obj = self.app.collection.get_by_name(name)
  54. if obj is None:
  55. self.raise_tcl_error("Object not found: %s" % name)
  56. if not isinstance(obj, CNCjob):
  57. self.raise_tcl_error('Expected CNCjob, got %s %s.' % (name, type(obj)))
  58. if self.app.collection.has_promises():
  59. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  60. del args['name']
  61. return obj.get_gcode(**args)