TclCommandOpenGerber.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. from camlib import ParseError
  3. from FlatCAMObj import FlatCAMGerber
  4. import collections
  5. class TclCommandOpenGerber(TclCommandSignaled):
  6. """
  7. Tcl shell command to opens a Gerber file
  8. """
  9. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  10. aliases = ['open_gerber']
  11. description = '%s %s' % ("--", "Opens an Gerber file, parse it and create a Gerber object from it.")
  12. # dictionary of types from Tcl command, needs to be ordered
  13. arg_names = collections.OrderedDict([
  14. ('filename', str)
  15. ])
  16. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('outname', str)
  19. ])
  20. # array of mandatory options for current Tcl command: required = {'name','outname'}
  21. required = ['filename']
  22. # structured help for current command, args needs to be ordered
  23. help = {
  24. 'main': "Opens a Gerber file.",
  25. 'args': collections.OrderedDict([
  26. ('filename', 'Absolute path to file to open. Required.\n'
  27. 'WARNING: no spaces are allowed. If unsure enclose the entire path with quotes.'),
  28. ('outname', 'Name of the resulting Gerber object.')
  29. ]),
  30. 'examples': ["open_gerber gerber_object_path -outname bla",
  31. 'open_gerber "D:\\my_gerber_file with spaces in the name.GRB"']
  32. }
  33. def execute(self, args, unnamed_args):
  34. """
  35. execute current TCL shell command
  36. :param args: array of known named arguments and options
  37. :param unnamed_args: array of other values which were passed into command
  38. without -somename and we do not have them in known arg_names
  39. :return: None or exception
  40. """
  41. # How the object should be initialized
  42. def obj_init(gerber_obj, app_obj):
  43. if not isinstance(gerber_obj, FlatCAMGerber):
  44. self.raise_tcl_error('Expected FlatCAMGerber, got %s %s.' % (outname, type(gerber_obj)))
  45. # Opening the file happens here
  46. try:
  47. gerber_obj.parse_file(filename)
  48. except IOError:
  49. app_obj.inform.emit("[ERROR_NOTCL] Failed to open file: %s " % filename)
  50. self.raise_tcl_error('Failed to open file: %s' % filename)
  51. except ParseError as e:
  52. app_obj.inform.emit("[ERROR_NOTCL] Failed to parse file: %s, %s " % (filename, str(e)))
  53. self.log.error(str(e))
  54. return
  55. filename = args['filename']
  56. if ' ' in filename:
  57. return "The absolute path to the project file contain spaces which is not allowed.\n" \
  58. "Please enclose the path within quotes."
  59. if 'outname' in args:
  60. outname = args['outname']
  61. else:
  62. outname = filename.split('/')[-1].split('\\')[-1]
  63. if 'follow' in args:
  64. self.raise_tcl_error("The 'follow' parameter is obsolete. To create 'follow' geometry use the 'follow' "
  65. "parameter for the Tcl Command isolate()")
  66. with self.app.proc_container.new("Opening Gerber"):
  67. # Object creation
  68. self.app.new_object("gerber", outname, obj_init, plot=False)
  69. # Register recent file
  70. self.app.file_opened.emit("gerber", filename)
  71. # GUI feedback
  72. self.app.inform.emit("[success] Opened: " + filename)