TclCommandOpenGerber.py 3.5 KB

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