TclCommandOpenSVG.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. import collections
  3. class TclCommandOpenSVG(TclCommandSignaled):
  4. """
  5. Tcl shell command to import an SVG file as a Geometry Object.
  6. """
  7. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  8. aliases = ['open_svg']
  9. description = '%s %s' % ("--", "Open a SVG file as a Geometry (or Gerber) Object.")
  10. # dictionary of types from Tcl command, needs to be ordered
  11. arg_names = collections.OrderedDict([
  12. ('filename', str)
  13. ])
  14. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ('type', str),
  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': "Open a SVG file as a Geometry (or Gerber) Object.",
  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. ('type', 'Open as a Gerber or Geometry (default) object. Values can be: "geometry" or "gerber"'),
  28. ('outname', 'Name of the resulting Geometry object.')
  29. ]),
  30. 'examples': ['open_svg D:\\my_beautiful_svg_file.SVG']
  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(geo_obj, app_obj):
  42. # if geo_obj.kind != 'geometry' or geo_obj.kind != 'gerber':
  43. # self.raise_tcl_error('Expected Geometry or Gerber, got %s %s.' % (outname, type(geo_obj)))
  44. geo_obj.import_svg(filename, obj_type, units=units)
  45. filename = args['filename']
  46. if 'outname' in args:
  47. outname = args['outname']
  48. else:
  49. outname = filename.split('/')[-1].split('\\')[-1]
  50. if 'type' in args:
  51. obj_type = args['type'].lower()
  52. else:
  53. obj_type = 'geometry'
  54. if obj_type != "geometry" and obj_type != "gerber":
  55. self.raise_tcl_error("Option type can be 'geometry' or 'gerber' only, got '%s'." % obj_type)
  56. units = self.app.defaults['units'].upper()
  57. with self.app.proc_container.new("Import SVG"):
  58. # Object creation
  59. ret_val = self.app.app_obj.new_object(obj_type, outname, obj_init, plot=False)
  60. if ret_val == 'fail':
  61. filename = self.app.defaults['global_tcl_path'] + '/' + outname
  62. ret_val = self.app.app_obj.new_object(obj_type, outname, obj_init, plot=False)
  63. self.app.shell.append_output(
  64. "No path provided or path is wrong. Using the default Path... \n")
  65. if ret_val == 'fail':
  66. return "Failed. The OpenSVG command was used but could not open the SVG file"
  67. # Register recent file
  68. self.app.file_opened.emit("svg", filename)
  69. # GUI feedback
  70. self.app.inform.emit("Opened: " + filename)