TclCommandImportSvg.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from tclCommands.TclCommand import *
  2. class TclCommandImportSvg(TclCommandSignaled):
  3. """
  4. Tcl shell command to import an SVG file as a Geometry Object.
  5. """
  6. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  7. aliases = ['import_svg']
  8. # dictionary of types from Tcl command, needs to be ordered
  9. arg_names = collections.OrderedDict([
  10. ('filename', str)
  11. ])
  12. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  13. option_types = collections.OrderedDict([
  14. ('type', str),
  15. ('outname', str)
  16. ])
  17. # array of mandatory options for current Tcl command: required = {'name','outname'}
  18. required = ['filename']
  19. # structured help for current command, args needs to be ordered
  20. help = {
  21. 'main': "Import an SVG file as a Geometry Object..",
  22. 'args': collections.OrderedDict([
  23. ('filename', 'Path to file to open.'),
  24. ('type', 'Import as gerber or geometry(default).'),
  25. ('outname', 'Name of the resulting Geometry object.')
  26. ]),
  27. 'examples': []
  28. }
  29. def execute(self, args, unnamed_args):
  30. """
  31. execute current TCL shell command
  32. :param args: array of known named arguments and options
  33. :param unnamed_args: array of other values which were passed into command
  34. without -somename and we do not have them in known arg_names
  35. :return: None or exception
  36. """
  37. # How the object should be initialized
  38. def obj_init(geo_obj, app_obj):
  39. if not isinstance(geo_obj, Geometry):
  40. self.raise_tcl_error('Expected Geometry or Gerber, got %s %s.' % (outname, type(geo_obj)))
  41. geo_obj.import_svg(filename)
  42. filename = args['filename']
  43. if 'outname' in args:
  44. outname = args['outname']
  45. else:
  46. outname = filename.split('/')[-1].split('\\')[-1]
  47. if 'type' in args:
  48. obj_type = args['type']
  49. else:
  50. obj_type = 'geometry'
  51. if obj_type != "geometry" and obj_type != "gerber":
  52. self.raise_tcl_error("Option type can be 'geopmetry' or 'gerber' only, got '%s'." % obj_type)
  53. with self.app.proc_container.new("Import SVG"):
  54. # Object creation
  55. self.app.new_object(obj_type, outname, obj_init)
  56. # Register recent file
  57. self.app.file_opened.emit("svg", filename)
  58. # GUI feedback
  59. self.app.inform.emit("Opened: " + filename)