TclCommandExportGcode.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 preamble
  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. description = '%s %s' % ("--", "Return Gcode into console output.")
  25. # dictionary of types from Tcl command, needs to be ordered
  26. arg_names = collections.OrderedDict([
  27. ('name', str),
  28. ('preamble', str),
  29. ('postamble', str),
  30. ])
  31. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  32. option_types = collections.OrderedDict()
  33. # array of mandatory options for current Tcl command: required = {'name','outname'}
  34. required = ['name']
  35. # structured help for current command, args needs to be ordered
  36. help = {
  37. 'main': "Export gcode into console output.",
  38. 'args': collections.OrderedDict([
  39. ('name', 'Name of the source Geometry object. Required.'),
  40. ('preamble', 'Prepend GCode to the original GCode.'),
  41. ('postamble', 'Append GCode o the original GCode.'),
  42. ]),
  43. 'examples': ['export_gcode geo_name -preamble "G01 X10 Y10" -postamble "G00 X20 Y20\nM04"']
  44. }
  45. def execute(self, args, unnamed_args):
  46. """
  47. execute current TCL shell command
  48. :param args: array of known named arguments and options
  49. :param unnamed_args: array of other values which were passed into command
  50. without -somename and we do not have them in known arg_names
  51. :return: None or exception
  52. """
  53. name = args['name']
  54. obj = self.app.collection.get_by_name(name)
  55. if obj is None:
  56. self.raise_tcl_error("Object not found: %s" % name)
  57. if not isinstance(obj, CNCjob):
  58. self.raise_tcl_error('Expected CNCjob, got %s %s.' % (name, type(obj)))
  59. if self.app.collection.has_promises():
  60. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  61. del args['name']
  62. modified_gcode = obj.get_gcode(**args)
  63. return